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
28 #include "ares_nameser.h"
29
30 #ifdef HAVE_STRINGS_H
31 # include <strings.h>
32 #endif
33
34 #ifdef HAVE_LIMITS_H
35 # include <limits.h>
36 #endif
37
38 #include "ares.h"
39 #include "ares_dns.h"
40 #include "ares_private.h"
41
ares__parse_into_addrinfo(const unsigned char * abuf,int alen,int cname_only_is_enodata,unsigned short port,struct ares_addrinfo * ai)42 int ares__parse_into_addrinfo(const unsigned char *abuf,
43 int alen, int cname_only_is_enodata,
44 unsigned short port,
45 struct ares_addrinfo *ai)
46 {
47 unsigned int qdcount, ancount;
48 int status, i, rr_type, rr_class, rr_len, rr_ttl;
49 int got_a = 0, got_aaaa = 0, got_cname = 0;
50 long len;
51 const unsigned char *aptr;
52 char *question_hostname = NULL;
53 char *hostname, *rr_name = NULL, *rr_data;
54 struct ares_addrinfo_cname *cname, *cnames = NULL;
55 struct ares_addrinfo_node *nodes = NULL;
56
57 /* Give up if abuf doesn't have room for a header. */
58 if (alen < HFIXEDSZ)
59 return ARES_EBADRESP;
60
61 /* Fetch the question and answer count from the header. */
62 qdcount = DNS_HEADER_QDCOUNT(abuf);
63 ancount = DNS_HEADER_ANCOUNT(abuf);
64 if (qdcount != 1)
65 return ARES_EBADRESP;
66
67
68 /* Expand the name from the question, and skip past the question. */
69 aptr = abuf + HFIXEDSZ;
70 status = ares__expand_name_for_response(aptr, abuf, alen, &question_hostname, &len, 0);
71 if (status != ARES_SUCCESS)
72 return status;
73 if (aptr + len + QFIXEDSZ > abuf + alen)
74 {
75 status = ARES_EBADRESP;
76 goto failed_stat;
77 }
78
79 hostname = question_hostname;
80
81 aptr += len + QFIXEDSZ;
82
83 /* Examine each answer resource record (RR) in turn. */
84 for (i = 0; i < (int)ancount; i++)
85 {
86 /* Decode the RR up to the data field. */
87 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len, 0);
88 if (status != ARES_SUCCESS)
89 {
90 rr_name = NULL;
91 goto failed_stat;
92 }
93
94 aptr += len;
95 if (aptr + RRFIXEDSZ > abuf + alen)
96 {
97 status = ARES_EBADRESP;
98 goto failed_stat;
99 }
100 rr_type = DNS_RR_TYPE(aptr);
101 rr_class = DNS_RR_CLASS(aptr);
102 rr_len = DNS_RR_LEN(aptr);
103 rr_ttl = DNS_RR_TTL(aptr);
104 aptr += RRFIXEDSZ;
105 if (aptr + rr_len > abuf + alen)
106 {
107 status = ARES_EBADRESP;
108 goto failed_stat;
109 }
110
111 if (rr_class == C_IN && rr_type == T_A
112 && rr_len == sizeof(struct in_addr)
113 && strcasecmp(rr_name, hostname) == 0)
114 {
115 got_a = 1;
116 if (aptr + sizeof(struct in_addr) > abuf + alen)
117 { /* LCOV_EXCL_START: already checked above */
118 status = ARES_EBADRESP;
119 goto failed_stat;
120 } /* LCOV_EXCL_STOP */
121
122 status = ares_append_ai_node(AF_INET, port, rr_ttl, aptr, &nodes);
123 if (status != ARES_SUCCESS)
124 goto failed_stat;
125 }
126 else if (rr_class == C_IN && rr_type == T_AAAA
127 && rr_len == sizeof(struct ares_in6_addr)
128 && strcasecmp(rr_name, hostname) == 0)
129 {
130 got_aaaa = 1;
131 if (aptr + sizeof(struct ares_in6_addr) > abuf + alen)
132 { /* LCOV_EXCL_START: already checked above */
133 status = ARES_EBADRESP;
134 goto failed_stat;
135 } /* LCOV_EXCL_STOP */
136
137 status = ares_append_ai_node(AF_INET6, port, rr_ttl, aptr, &nodes);
138 if (status != ARES_SUCCESS)
139 goto failed_stat;
140 }
141
142 if (rr_class == C_IN && rr_type == T_CNAME)
143 {
144 got_cname = 1;
145 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
146 &len, 1);
147 if (status != ARES_SUCCESS)
148 {
149 goto failed_stat;
150 }
151
152 /* Decode the RR data and replace the hostname with it. */
153 /* SA: Seems wrong as it introduses order dependency. */
154 hostname = rr_data;
155
156 cname = ares__append_addrinfo_cname(&cnames);
157 if (!cname)
158 {
159 status = ARES_ENOMEM;
160 ares_free(rr_data);
161 goto failed_stat;
162 }
163 cname->ttl = rr_ttl;
164 cname->alias = rr_name;
165 cname->name = rr_data;
166 rr_name = NULL;
167 }
168 else
169 {
170 /* rr_name is only saved for cname */
171 ares_free(rr_name);
172 rr_name = NULL;
173 }
174
175
176 aptr += rr_len;
177 if (aptr > abuf + alen)
178 { /* LCOV_EXCL_START: already checked above */
179 status = ARES_EBADRESP;
180 goto failed_stat;
181 } /* LCOV_EXCL_STOP */
182 }
183
184 if (status == ARES_SUCCESS)
185 {
186 if (!got_a && !got_aaaa)
187 {
188 if (!got_cname || (got_cname && cname_only_is_enodata))
189 {
190 status = ARES_ENODATA;
191 goto failed_stat;
192 }
193 }
194
195 /* save the question hostname as ai->name */
196 if (ai->name == NULL || strcasecmp(ai->name, question_hostname) != 0)
197 {
198 ares_free(ai->name);
199 ai->name = ares_strdup(question_hostname);
200 if (!ai->name)
201 {
202 status = ARES_ENOMEM;
203 goto failed_stat;
204 }
205 }
206
207 if (got_a || got_aaaa)
208 {
209 ares__addrinfo_cat_nodes(&ai->nodes, nodes);
210 nodes = NULL;
211 }
212
213 if (got_cname)
214 {
215 ares__addrinfo_cat_cnames(&ai->cnames, cnames);
216 cnames = NULL;
217 }
218 }
219
220 ares_free(question_hostname);
221 return status;
222
223 failed_stat:
224 ares_free(question_hostname);
225 ares_free(rr_name);
226 ares__freeaddrinfo_cnames(cnames);
227 ares__freeaddrinfo_nodes(nodes);
228 return status;
229 }
230