3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
6 * Permission to use, copy, modify, and distribute this
7 * software and its documentation for any purpose and without
8 * fee is hereby granted, provided that the above copyright
9 * notice appear in all copies and that both that copyright
10 * notice and this permission notice appear in supporting
11 * documentation, and that the name of M.I.T. not be used in
12 * advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission.
14 * M.I.T. makes no representations about the suitability of
15 * this software for any purpose. It is provided "as is"
16 * without express or implied warranty.
19 #include "ares_setup.h"
21 #ifdef HAVE_SYS_SOCKET_H
22 # include <sys/socket.h>
24 #ifdef HAVE_NETINET_IN_H
25 # include <netinet/in.h>
30 #ifdef HAVE_ARPA_INET_H
31 # include <arpa/inet.h>
33 #ifdef HAVE_ARPA_NAMESER_H
34 # include <arpa/nameser.h>
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 # include <arpa/nameser_compat.h>
51 #include "ares_data.h"
52 #include "ares_private.h"
55 ares_parse_txt_reply (const unsigned char *abuf, int alen,
56 struct ares_txt_reply **txt_out)
58 size_t substr_len, str_len;
59 unsigned int qdcount, ancount, i;
60 const unsigned char *aptr;
61 const unsigned char *strptr;
62 int status, rr_type, rr_class, rr_len;
64 char *hostname = NULL, *rr_name = NULL;
65 struct ares_txt_reply *txt_head = NULL;
66 struct ares_txt_reply *txt_last = NULL;
67 struct ares_txt_reply *txt_curr;
69 /* Set *txt_out to NULL for all failure cases. */
72 /* Give up if abuf doesn't have room for a header. */
76 /* Fetch the question and answer count from the header. */
77 qdcount = DNS_HEADER_QDCOUNT (abuf);
78 ancount = DNS_HEADER_ANCOUNT (abuf);
84 /* Expand the name from the question, and skip past the question. */
85 aptr = abuf + HFIXEDSZ;
86 status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
87 if (status != ARES_SUCCESS)
90 if (aptr + len + QFIXEDSZ > abuf + alen)
95 aptr += len + QFIXEDSZ;
97 /* Examine each answer resource record (RR) in turn. */
98 for (i = 0; i < ancount; i++)
100 /* Decode the RR up to the data field. */
101 status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
102 if (status != ARES_SUCCESS)
107 if (aptr + RRFIXEDSZ > abuf + alen)
109 status = ARES_EBADRESP;
112 rr_type = DNS_RR_TYPE (aptr);
113 rr_class = DNS_RR_CLASS (aptr);
114 rr_len = DNS_RR_LEN (aptr);
117 /* Check if we are really looking at a TXT record */
118 if (rr_class == C_IN && rr_type == T_TXT)
120 /* Allocate storage for this SRV answer appending it to the list */
121 txt_curr = ares_malloc_data(ARES_DATATYPE_TXT_REPLY);
124 status = ARES_ENOMEM;
129 txt_last->next = txt_curr;
138 * There may be multiple substrings in a single TXT record. Each
139 * substring may be up to 255 characters in length, with a
140 * "length byte" indicating the size of the substring payload.
141 * RDATA contains both the length-bytes and payloads of all
142 * substrings contained therein.
145 /* Compute total length to allow a single memory allocation */
147 while (strptr < (aptr + rr_len))
149 substr_len = (unsigned char)*strptr;
150 txt_curr->length += substr_len;
151 strptr += substr_len + 1;
154 /* Including null byte */
155 txt_curr->txt = malloc (txt_curr->length + 1);
156 if (txt_curr->txt == NULL)
158 status = ARES_ENOMEM;
162 /* Step through the list of substrings, concatenating them */
165 while (strptr < (aptr + rr_len))
167 substr_len = (unsigned char)*strptr;
169 memcpy ((char *) txt_curr->txt + str_len, strptr, substr_len);
170 str_len += substr_len;
171 strptr += substr_len;
173 /* Make sure we NULL-terminate */
174 *((char *) txt_curr->txt + txt_curr->length) = '\0';
176 /* Move on to the next record */
180 /* Don't lose memory in the next iteration */
190 /* clean up on error */
191 if (status != ARES_SUCCESS)
194 ares_free_data (txt_head);
198 /* everything looks fine, return the data */