2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
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.
18 #include "ares_setup.h"
20 #ifdef HAVE_NETINET_IN_H
21 # include <netinet/in.h>
26 #ifdef HAVE_ARPA_INET_H
27 # include <arpa/inet.h>
29 #ifdef HAVE_ARPA_NAMESER_H
30 # include <arpa/nameser.h>
34 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
35 # include <arpa/nameser_compat.h>
44 #include "ares_data.h"
45 #include "ares_private.h"
48 ares__parse_txt_reply (const unsigned char *abuf, int alen,
49 int ex, void **txt_out)
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;
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;
62 /* Set *txt_out to NULL for all failure cases. */
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);
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)
83 if (aptr + len + QFIXEDSZ > abuf + alen)
88 aptr += len + QFIXEDSZ;
90 /* Examine each answer resource record (RR) in turn. */
91 for (i = 0; i < ancount; i++)
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)
100 if (aptr + RRFIXEDSZ > abuf + alen)
102 status = ARES_EBADRESP;
105 rr_type = DNS_RR_TYPE (aptr);
106 rr_class = DNS_RR_CLASS (aptr);
107 rr_len = DNS_RR_LEN (aptr);
109 if (aptr + rr_len > abuf + alen)
111 status = ARES_EBADRESP;
115 /* Check if we are really looking at a TXT record */
116 if (rr_class == C_IN && rr_type == T_TXT)
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.
127 while (strptr < (aptr + rr_len))
129 substr_len = (unsigned char)*strptr;
130 if (strptr + substr_len + 1 > aptr + rr_len)
132 status = ARES_EBADRESP;
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);
141 status = ARES_ENOMEM;
146 txt_last->next = txt_curr;
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)
160 status = ARES_ENOMEM;
165 memcpy ((char *) txt_curr->txt, strptr, substr_len);
167 /* Make sure we NULL-terminate */
168 txt_curr->txt[substr_len] = 0;
170 strptr += substr_len;
174 /* Propagate any failures */
175 if (status != ARES_SUCCESS)
180 /* Don't lose memory in the next iteration */
184 /* Move on to the next record */
189 ares_free (hostname);
193 /* clean up on error */
194 if (status != ARES_SUCCESS)
197 ares_free_data (txt_head);
201 /* everything looks fine, return the data */
208 ares_parse_txt_reply (const unsigned char *abuf, int alen,
209 struct ares_txt_reply **txt_out)
211 return ares__parse_txt_reply(abuf, alen, 0, (void **) txt_out);
216 ares_parse_txt_reply_ext (const unsigned char *abuf, int alen,
217 struct ares_txt_ext **txt_out)
219 return ares__parse_txt_reply(abuf, alen, 1, (void **) txt_out);