1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
4 *
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.
16 */
17
18 #include "ares_setup.h"
19
20 #ifdef HAVE_NETINET_IN_H
21 # include <netinet/in.h>
22 #endif
23 #ifdef HAVE_NETDB_H
24 # include <netdb.h>
25 #endif
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
28 #endif
29 #ifdef HAVE_ARPA_NAMESER_H
30 # include <arpa/nameser.h>
31 #else
32 # include "nameser.h"
33 #endif
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 # include <arpa/nameser_compat.h>
36 #endif
37
38 #ifdef HAVE_STRINGS_H
39 # include <strings.h>
40 #endif
41
42 #include "ares.h"
43 #include "ares_dns.h"
44 #include "ares_data.h"
45 #include "ares_private.h"
46
47 static int
ares__parse_txt_reply(const unsigned char * abuf,int alen,int ex,void ** txt_out)48 ares__parse_txt_reply (const unsigned char *abuf, int alen,
49 int ex, void **txt_out)
50 {
51 size_t substr_len;
52 unsigned int qdcount, ancount, i;
53 const unsigned char *aptr;
54 const unsigned char *strptr;
55 int status, rr_type, rr_class, rr_len;
56 long len;
57 char *hostname = NULL, *rr_name = NULL;
58 struct ares_txt_ext *txt_head = NULL;
59 struct ares_txt_ext *txt_last = NULL;
60 struct ares_txt_ext *txt_curr;
61
62 /* Set *txt_out to NULL for all failure cases. */
63 *txt_out = NULL;
64
65 /* Give up if abuf doesn't have room for a header. */
66 if (alen < HFIXEDSZ)
67 return ARES_EBADRESP;
68
69 /* Fetch the question and answer count from the header. */
70 qdcount = DNS_HEADER_QDCOUNT (abuf);
71 ancount = DNS_HEADER_ANCOUNT (abuf);
72 if (qdcount != 1)
73 return ARES_EBADRESP;
74 if (ancount == 0)
75 return ARES_ENODATA;
76
77 /* Expand the name from the question, and skip past the question. */
78 aptr = abuf + HFIXEDSZ;
79 status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
80 if (status != ARES_SUCCESS)
81 return status;
82
83 if (aptr + len + QFIXEDSZ > abuf + alen)
84 {
85 ares_free (hostname);
86 return ARES_EBADRESP;
87 }
88 aptr += len + QFIXEDSZ;
89
90 /* Examine each answer resource record (RR) in turn. */
91 for (i = 0; i < ancount; i++)
92 {
93 /* Decode the RR up to the data field. */
94 status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
95 if (status != ARES_SUCCESS)
96 {
97 break;
98 }
99 aptr += len;
100 if (aptr + RRFIXEDSZ > abuf + alen)
101 {
102 status = ARES_EBADRESP;
103 break;
104 }
105 rr_type = DNS_RR_TYPE (aptr);
106 rr_class = DNS_RR_CLASS (aptr);
107 rr_len = DNS_RR_LEN (aptr);
108 aptr += RRFIXEDSZ;
109 if (aptr + rr_len > abuf + alen)
110 {
111 status = ARES_EBADRESP;
112 break;
113 }
114
115 /* Check if we are really looking at a TXT record */
116 if (rr_class == C_IN && rr_type == T_TXT)
117 {
118 /*
119 * There may be multiple substrings in a single TXT record. Each
120 * substring may be up to 255 characters in length, with a
121 * "length byte" indicating the size of the substring payload.
122 * RDATA contains both the length-bytes and payloads of all
123 * substrings contained therein.
124 */
125
126 strptr = aptr;
127 while (strptr < (aptr + rr_len))
128 {
129 substr_len = (unsigned char)*strptr;
130 if (strptr + substr_len + 1 > aptr + rr_len)
131 {
132 status = ARES_EBADRESP;
133 break;
134 }
135
136 /* Allocate storage for this TXT answer appending it to the list */
137 txt_curr = ares_malloc_data(ex ? ARES_DATATYPE_TXT_EXT :
138 ARES_DATATYPE_TXT_REPLY);
139 if (!txt_curr)
140 {
141 status = ARES_ENOMEM;
142 break;
143 }
144 if (txt_last)
145 {
146 txt_last->next = txt_curr;
147 }
148 else
149 {
150 txt_head = txt_curr;
151 }
152 txt_last = txt_curr;
153
154 if (ex)
155 txt_curr->record_start = (strptr == aptr);
156 txt_curr->length = substr_len;
157 txt_curr->txt = ares_malloc (substr_len + 1/* Including null byte */);
158 if (txt_curr->txt == NULL)
159 {
160 status = ARES_ENOMEM;
161 break;
162 }
163
164 ++strptr;
165 memcpy ((char *) txt_curr->txt, strptr, substr_len);
166
167 /* Make sure we NULL-terminate */
168 txt_curr->txt[substr_len] = 0;
169
170 strptr += substr_len;
171 }
172 }
173
174 /* Propagate any failures */
175 if (status != ARES_SUCCESS)
176 {
177 break;
178 }
179
180 /* Don't lose memory in the next iteration */
181 ares_free (rr_name);
182 rr_name = NULL;
183
184 /* Move on to the next record */
185 aptr += rr_len;
186 }
187
188 if (hostname)
189 ares_free (hostname);
190 if (rr_name)
191 ares_free (rr_name);
192
193 /* clean up on error */
194 if (status != ARES_SUCCESS)
195 {
196 if (txt_head)
197 ares_free_data (txt_head);
198 return status;
199 }
200
201 /* everything looks fine, return the data */
202 *txt_out = txt_head;
203
204 return ARES_SUCCESS;
205 }
206
207 int
ares_parse_txt_reply(const unsigned char * abuf,int alen,struct ares_txt_reply ** txt_out)208 ares_parse_txt_reply (const unsigned char *abuf, int alen,
209 struct ares_txt_reply **txt_out)
210 {
211 return ares__parse_txt_reply(abuf, alen, 0, (void **) txt_out);
212 }
213
214
215 int
ares_parse_txt_reply_ext(const unsigned char * abuf,int alen,struct ares_txt_ext ** txt_out)216 ares_parse_txt_reply_ext (const unsigned char *abuf, int alen,
217 struct ares_txt_ext **txt_out)
218 {
219 return ares__parse_txt_reply(abuf, alen, 1, (void **) txt_out);
220 }
221