1 /* Copyright (C) 2019 by Andrew Selivanov
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 #include "ares_setup.h"
18 #ifdef HAVE_NETINET_IN_H
19 # include <netinet/in.h>
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
32 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
33 # include <arpa/nameser_compat.h>
46 #include "ares_private.h"
48 int ares__parse_into_addrinfo2(const unsigned char *abuf,
50 char **question_hostname,
51 struct ares_addrinfo *ai)
53 unsigned int qdcount, ancount;
54 int status, i, rr_type, rr_class, rr_len, rr_ttl;
55 int got_a = 0, got_aaaa = 0, got_cname = 0;
57 const unsigned char *aptr;
58 char *hostname, *rr_name = NULL, *rr_data;
59 struct ares_addrinfo_cname *cname, *cnames = NULL;
60 struct ares_addrinfo_node *node, *nodes = NULL;
61 struct sockaddr_in *sin;
62 struct sockaddr_in6 *sin6;
64 *question_hostname = NULL;
66 /* Give up if abuf doesn't have room for a header. */
70 /* Fetch the question and answer count from the header. */
71 qdcount = DNS_HEADER_QDCOUNT(abuf);
72 ancount = DNS_HEADER_ANCOUNT(abuf);
77 /* Expand the name from the question, and skip past the question. */
78 aptr = abuf + HFIXEDSZ;
79 status = ares__expand_name_for_response(aptr, abuf, alen, question_hostname, &len);
80 if (status != ARES_SUCCESS)
82 if (aptr + len + QFIXEDSZ > abuf + alen)
87 hostname = *question_hostname;
89 aptr += len + QFIXEDSZ;
91 /* Examine each answer resource record (RR) in turn. */
92 for (i = 0; i < (int)ancount; i++)
94 /* Decode the RR up to the data field. */
95 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
96 if (status != ARES_SUCCESS)
103 if (aptr + RRFIXEDSZ > abuf + alen)
105 status = ARES_EBADRESP;
108 rr_type = DNS_RR_TYPE(aptr);
109 rr_class = DNS_RR_CLASS(aptr);
110 rr_len = DNS_RR_LEN(aptr);
111 rr_ttl = DNS_RR_TTL(aptr);
113 if (aptr + rr_len > abuf + alen)
115 status = ARES_EBADRESP;
119 if (rr_class == C_IN && rr_type == T_A
120 && rr_len == sizeof(struct in_addr)
121 && strcasecmp(rr_name, hostname) == 0)
124 if (aptr + sizeof(struct in_addr) > abuf + alen)
125 { /* LCOV_EXCL_START: already checked above */
126 status = ARES_EBADRESP;
128 } /* LCOV_EXCL_STOP */
130 node = ares__append_addrinfo_node(&nodes);
133 status = ARES_ENOMEM;
137 sin = ares_malloc(sizeof(struct sockaddr_in));
140 status = ARES_ENOMEM;
143 memset(sin, 0, sizeof(struct sockaddr_in));
144 memcpy(&sin->sin_addr.s_addr, aptr, sizeof(struct in_addr));
145 sin->sin_family = AF_INET;
147 node->ai_addr = (struct sockaddr *)sin;
148 node->ai_family = AF_INET;
149 node->ai_addrlen = sizeof(struct sockaddr_in);
151 node->ai_ttl = rr_ttl;
153 status = ARES_SUCCESS;
155 else if (rr_class == C_IN && rr_type == T_AAAA
156 && rr_len == sizeof(struct ares_in6_addr)
157 && strcasecmp(rr_name, hostname) == 0)
160 if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
161 { /* LCOV_EXCL_START: already checked above */
162 status = ARES_EBADRESP;
164 } /* LCOV_EXCL_STOP */
166 node = ares__append_addrinfo_node(&nodes);
169 status = ARES_ENOMEM;
173 sin6 = ares_malloc(sizeof(struct sockaddr_in6));
176 status = ARES_ENOMEM;
180 memset(sin6, 0, sizeof(struct sockaddr_in6));
181 memcpy(&sin6->sin6_addr.s6_addr, aptr, sizeof(struct ares_in6_addr));
182 sin6->sin6_family = AF_INET6;
184 node->ai_addr = (struct sockaddr *)sin6;
185 node->ai_family = AF_INET6;
186 node->ai_addrlen = sizeof(struct sockaddr_in6);
188 node->ai_ttl = rr_ttl;
190 status = ARES_SUCCESS;
193 if (rr_class == C_IN && rr_type == T_CNAME)
196 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
198 if (status != ARES_SUCCESS)
203 /* Decode the RR data and replace the hostname with it. */
204 /* SA: Seems wrong as it introduses order dependency. */
207 cname = ares__append_addrinfo_cname(&cnames);
210 status = ARES_ENOMEM;
215 cname->alias = rr_name;
216 cname->name = rr_data;
225 if (aptr > abuf + alen)
226 { /* LCOV_EXCL_START: already checked above */
227 status = ARES_EBADRESP;
229 } /* LCOV_EXCL_STOP */
232 if (status == ARES_SUCCESS)
234 ares__addrinfo_cat_nodes(&ai->nodes, nodes);
237 ares__addrinfo_cat_cnames(&ai->cnames, cnames);
240 else if (got_a == 0 && got_aaaa == 0)
242 /* the check for naliases to be zero is to make sure CNAME responses
243 don't get caught here */
244 status = ARES_ENODATA;
252 ares__freeaddrinfo_cnames(cnames);
253 ares__freeaddrinfo_nodes(nodes);
257 int ares__parse_into_addrinfo(const unsigned char *abuf,
259 struct ares_addrinfo *ai)
262 char *question_hostname;
263 status = ares__parse_into_addrinfo2(abuf, alen, &question_hostname, ai);
264 ares_free(question_hostname);