1 /* Copyright (C) 2019 by Andrew Selivanov
2 *
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.
14 */
15
16 #include "ares_setup.h"
17
18 #ifdef HAVE_NETINET_IN_H
19 # include <netinet/in.h>
20 #endif
21 #ifdef HAVE_NETDB_H
22 # include <netdb.h>
23 #endif
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
26 #endif
27 #ifdef HAVE_ARPA_NAMESER_H
28 # include <arpa/nameser.h>
29 #else
30 # include "nameser.h"
31 #endif
32 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
33 # include <arpa/nameser_compat.h>
34 #endif
35
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif
39
40 #ifdef HAVE_LIMITS_H
41 # include <limits.h>
42 #endif
43
44 #include "ares.h"
45 #include "ares_dns.h"
46 #include "ares_private.h"
47
ares__parse_into_addrinfo2(const unsigned char * abuf,int alen,char ** question_hostname,struct ares_addrinfo * ai)48 int ares__parse_into_addrinfo2(const unsigned char *abuf,
49 int alen,
50 char **question_hostname,
51 struct ares_addrinfo *ai)
52 {
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;
56 long len;
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;
63
64 *question_hostname = NULL;
65
66 /* Give up if abuf doesn't have room for a header. */
67 if (alen < HFIXEDSZ)
68 return ARES_EBADRESP;
69
70 /* Fetch the question and answer count from the header. */
71 qdcount = DNS_HEADER_QDCOUNT(abuf);
72 ancount = DNS_HEADER_ANCOUNT(abuf);
73 if (qdcount != 1)
74 return ARES_EBADRESP;
75
76
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)
81 return status;
82 if (aptr + len + QFIXEDSZ > abuf + alen)
83 {
84 return ARES_EBADRESP;
85 }
86
87 hostname = *question_hostname;
88
89 aptr += len + QFIXEDSZ;
90
91 /* Examine each answer resource record (RR) in turn. */
92 for (i = 0; i < (int)ancount; i++)
93 {
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)
97 {
98 rr_name = NULL;
99 goto failed_stat;
100 }
101
102 aptr += len;
103 if (aptr + RRFIXEDSZ > abuf + alen)
104 {
105 status = ARES_EBADRESP;
106 goto failed_stat;
107 }
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);
112 aptr += RRFIXEDSZ;
113 if (aptr + rr_len > abuf + alen)
114 {
115 status = ARES_EBADRESP;
116 goto failed_stat;
117 }
118
119 if (rr_class == C_IN && rr_type == T_A
120 && rr_len == sizeof(struct in_addr)
121 && strcasecmp(rr_name, hostname) == 0)
122 {
123 got_a = 1;
124 if (aptr + sizeof(struct in_addr) > abuf + alen)
125 { /* LCOV_EXCL_START: already checked above */
126 status = ARES_EBADRESP;
127 goto failed_stat;
128 } /* LCOV_EXCL_STOP */
129
130 node = ares__append_addrinfo_node(&nodes);
131 if (!node)
132 {
133 status = ARES_ENOMEM;
134 goto failed_stat;
135 }
136
137 sin = ares_malloc(sizeof(struct sockaddr_in));
138 if (!sin)
139 {
140 status = ARES_ENOMEM;
141 goto failed_stat;
142 }
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;
146
147 node->ai_addr = (struct sockaddr *)sin;
148 node->ai_family = AF_INET;
149 node->ai_addrlen = sizeof(struct sockaddr_in);
150
151 node->ai_ttl = rr_ttl;
152
153 status = ARES_SUCCESS;
154 }
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)
158 {
159 got_aaaa = 1;
160 if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
161 { /* LCOV_EXCL_START: already checked above */
162 status = ARES_EBADRESP;
163 goto failed_stat;
164 } /* LCOV_EXCL_STOP */
165
166 node = ares__append_addrinfo_node(&nodes);
167 if (!node)
168 {
169 status = ARES_ENOMEM;
170 goto failed_stat;
171 }
172
173 sin6 = ares_malloc(sizeof(struct sockaddr_in6));
174 if (!sin6)
175 {
176 status = ARES_ENOMEM;
177 goto failed_stat;
178 }
179
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;
183
184 node->ai_addr = (struct sockaddr *)sin6;
185 node->ai_family = AF_INET6;
186 node->ai_addrlen = sizeof(struct sockaddr_in6);
187
188 node->ai_ttl = rr_ttl;
189
190 status = ARES_SUCCESS;
191 }
192
193 if (rr_class == C_IN && rr_type == T_CNAME)
194 {
195 got_cname = 1;
196 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
197 &len);
198 if (status != ARES_SUCCESS)
199 {
200 goto failed_stat;
201 }
202
203 /* Decode the RR data and replace the hostname with it. */
204 /* SA: Seems wrong as it introduses order dependency. */
205 hostname = rr_data;
206
207 cname = ares__append_addrinfo_cname(&cnames);
208 if (!cname)
209 {
210 status = ARES_ENOMEM;
211 ares_free(rr_data);
212 goto failed_stat;
213 }
214 cname->ttl = rr_ttl;
215 cname->alias = rr_name;
216 cname->name = rr_data;
217 }
218 else
219 {
220 ares_free(rr_name);
221 }
222
223
224 aptr += rr_len;
225 if (aptr > abuf + alen)
226 { /* LCOV_EXCL_START: already checked above */
227 status = ARES_EBADRESP;
228 goto failed_stat;
229 } /* LCOV_EXCL_STOP */
230 }
231
232 if (status == ARES_SUCCESS)
233 {
234 ares__addrinfo_cat_nodes(&ai->nodes, nodes);
235 if (got_cname)
236 {
237 ares__addrinfo_cat_cnames(&ai->cnames, cnames);
238 return status;
239 }
240 else if (got_a == 0 && got_aaaa == 0)
241 {
242 /* the check for naliases to be zero is to make sure CNAME responses
243 don't get caught here */
244 status = ARES_ENODATA;
245 }
246 }
247
248 return status;
249
250 failed_stat:
251 ares_free(rr_name);
252 ares__freeaddrinfo_cnames(cnames);
253 ares__freeaddrinfo_nodes(nodes);
254 return status;
255 }
256
ares__parse_into_addrinfo(const unsigned char * abuf,int alen,struct ares_addrinfo * ai)257 int ares__parse_into_addrinfo(const unsigned char *abuf,
258 int alen,
259 struct ares_addrinfo *ai)
260 {
261 int status;
262 char *question_hostname;
263 status = ares__parse_into_addrinfo2(abuf, alen, &question_hostname, ai);
264 ares_free(question_hostname);
265 return status;
266 }
267