ares_parse_txt_reply.c
Go to the documentation of this file.
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 
30 #include "ares_nameser.h"
31 
32 #ifdef HAVE_STRINGS_H
33 # include <strings.h>
34 #endif
35 
36 #include "ares.h"
37 #include "ares_dns.h"
38 #include "ares_data.h"
39 #include "ares_private.h"
40 
41 static int
42 ares__parse_txt_reply (const unsigned char *abuf, int alen,
43  int ex, void **txt_out)
44 {
45  size_t substr_len;
46  unsigned int qdcount, ancount, i;
47  const unsigned char *aptr;
48  const unsigned char *strptr;
49  int status, rr_type, rr_class, rr_len;
50  long len;
51  char *hostname = NULL, *rr_name = NULL;
52  struct ares_txt_ext *txt_head = NULL;
53  struct ares_txt_ext *txt_last = NULL;
54  struct ares_txt_ext *txt_curr;
55 
56  /* Set *txt_out to NULL for all failure cases. */
57  *txt_out = NULL;
58 
59  /* Give up if abuf doesn't have room for a header. */
60  if (alen < HFIXEDSZ)
61  return ARES_EBADRESP;
62 
63  /* Fetch the question and answer count from the header. */
64  qdcount = DNS_HEADER_QDCOUNT (abuf);
65  ancount = DNS_HEADER_ANCOUNT (abuf);
66  if (qdcount != 1)
67  return ARES_EBADRESP;
68  if (ancount == 0)
69  return ARES_ENODATA;
70 
71  /* Expand the name from the question, and skip past the question. */
72  aptr = abuf + HFIXEDSZ;
73  status = ares_expand_name (aptr, abuf, alen, &hostname, &len);
74  if (status != ARES_SUCCESS)
75  return status;
76 
77  if (aptr + len + QFIXEDSZ > abuf + alen)
78  {
79  ares_free (hostname);
80  return ARES_EBADRESP;
81  }
82  aptr += len + QFIXEDSZ;
83 
84  /* Examine each answer resource record (RR) in turn. */
85  for (i = 0; i < ancount; i++)
86  {
87  /* Decode the RR up to the data field. */
88  status = ares_expand_name (aptr, abuf, alen, &rr_name, &len);
89  if (status != ARES_SUCCESS)
90  {
91  break;
92  }
93  aptr += len;
94  if (aptr + RRFIXEDSZ > abuf + alen)
95  {
97  break;
98  }
99  rr_type = DNS_RR_TYPE (aptr);
100  rr_class = DNS_RR_CLASS (aptr);
101  rr_len = DNS_RR_LEN (aptr);
102  aptr += RRFIXEDSZ;
103  if (aptr + rr_len > abuf + alen)
104  {
106  break;
107  }
108 
109  /* Check if we are really looking at a TXT record */
110  if ((rr_class == C_IN || rr_class == C_CHAOS) && rr_type == T_TXT)
111  {
112  /*
113  * There may be multiple substrings in a single TXT record. Each
114  * substring may be up to 255 characters in length, with a
115  * "length byte" indicating the size of the substring payload.
116  * RDATA contains both the length-bytes and payloads of all
117  * substrings contained therein.
118  */
119 
120  strptr = aptr;
121  while (strptr < (aptr + rr_len))
122  {
123  substr_len = (unsigned char)*strptr;
124  if (strptr + substr_len + 1 > aptr + rr_len)
125  {
127  break;
128  }
129 
130  /* Allocate storage for this TXT answer appending it to the list */
133  if (!txt_curr)
134  {
136  break;
137  }
138  if (txt_last)
139  {
140  txt_last->next = txt_curr;
141  }
142  else
143  {
144  txt_head = txt_curr;
145  }
146  txt_last = txt_curr;
147 
148  if (ex)
149  txt_curr->record_start = (strptr == aptr);
150  txt_curr->length = substr_len;
151  txt_curr->txt = ares_malloc (substr_len + 1/* Including null byte */);
152  if (txt_curr->txt == NULL)
153  {
155  break;
156  }
157 
158  ++strptr;
159  memcpy ((char *) txt_curr->txt, strptr, substr_len);
160 
161  /* Make sure we NULL-terminate */
162  txt_curr->txt[substr_len] = 0;
163 
164  strptr += substr_len;
165  }
166  }
167 
168  /* Propagate any failures */
169  if (status != ARES_SUCCESS)
170  {
171  break;
172  }
173 
174  /* Don't lose memory in the next iteration */
175  ares_free (rr_name);
176  rr_name = NULL;
177 
178  /* Move on to the next record */
179  aptr += rr_len;
180  }
181 
182  if (hostname)
183  ares_free (hostname);
184  if (rr_name)
185  ares_free (rr_name);
186 
187  /* clean up on error */
188  if (status != ARES_SUCCESS)
189  {
190  if (txt_head)
191  ares_free_data (txt_head);
192  return status;
193  }
194 
195  /* everything looks fine, return the data */
196  *txt_out = txt_head;
197 
198  return ARES_SUCCESS;
199 }
200 
201 int
202 ares_parse_txt_reply (const unsigned char *abuf, int alen,
203  struct ares_txt_reply **txt_out)
204 {
205  return ares__parse_txt_reply(abuf, alen, 0, (void **) txt_out);
206 }
207 
208 
209 int
210 ares_parse_txt_reply_ext (const unsigned char *abuf, int alen,
211  struct ares_txt_ext **txt_out)
212 {
213  return ares__parse_txt_reply(abuf, alen, 1, (void **) txt_out);
214 }
ARES_ENOMEM
#define ARES_ENOMEM
Definition: ares.h:117
DNS_RR_LEN
#define DNS_RR_LEN(r)
Definition: ares_dns.h:104
ares.h
ares_parse_txt_reply
int ares_parse_txt_reply(const unsigned char *abuf, int alen, struct ares_txt_reply **txt_out)
Definition: ares_parse_txt_reply.c:202
ares_dns.h
status
absl::Status status
Definition: rls.cc:251
ARES_DATATYPE_TXT_REPLY
@ ARES_DATATYPE_TXT_REPLY
Definition: ares_data.h:20
DNS_RR_TYPE
#define DNS_RR_TYPE(r)
Definition: ares_dns.h:101
T_TXT
#define T_TXT
Definition: ares_nameser.h:355
ex
OPENSSL_EXPORT X509_EXTENSION * ex
Definition: x509.h:1418
ares__parse_txt_reply
static int ares__parse_txt_reply(const unsigned char *abuf, int alen, int ex, void **txt_out)
Definition: ares_parse_txt_reply.c:42
ares_txt_ext
Definition: ares.h:561
ARES_EBADRESP
#define ARES_EBADRESP
Definition: ares.h:112
ares_expand_name
CARES_EXTERN int ares_expand_name(const unsigned char *encoded, const unsigned char *abuf, int alen, char **s, long *enclen)
Definition: ares_expand_name.c:203
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ares_txt_reply
Definition: ares.h:553
ares_malloc
void *(* ares_malloc)(size_t size)=default_malloc
Definition: ares_library_init.c:58
ares_data.h
ARES_ENODATA
#define ARES_ENODATA
Definition: ares.h:101
DNS_RR_CLASS
#define DNS_RR_CLASS(r)
Definition: ares_dns.h:102
ares_txt_ext::record_start
unsigned char record_start
Definition: ares.h:567
C_CHAOS
#define C_CHAOS
Definition: ares_nameser.h:295
RRFIXEDSZ
#define RRFIXEDSZ
Definition: ares_nameser.h:211
ARES_SUCCESS
#define ARES_SUCCESS
Definition: ares.h:98
DNS_HEADER_QDCOUNT
#define DNS_HEADER_QDCOUNT(h)
Definition: ares_dns.h:72
ares_setup.h
ares_parse_txt_reply_ext
int ares_parse_txt_reply_ext(const unsigned char *abuf, int alen, struct ares_txt_ext **txt_out)
Definition: ares_parse_txt_reply.c:210
ares_txt_ext::next
struct ares_txt_ext * next
Definition: ares.h:562
ares_free_data
CARES_EXTERN void ares_free_data(void *dataptr)
Definition: ares_data.c:41
C_IN
#define C_IN
Definition: ares_nameser.h:292
ARES_DATATYPE_TXT_EXT
@ ARES_DATATYPE_TXT_EXT
Definition: ares_data.h:21
ares_malloc_data
void * ares_malloc_data(ares_datatype type)
Definition: ares_data.c:153
ares_free
void(* ares_free)(void *ptr)=default_free
Definition: ares_library_init.c:60
DNS_HEADER_ANCOUNT
#define DNS_HEADER_ANCOUNT(h)
Definition: ares_dns.h:73
ares_private.h
QFIXEDSZ
#define QFIXEDSZ
Definition: ares_nameser.h:207
HFIXEDSZ
#define HFIXEDSZ
Definition: ares_nameser.h:203
ares_txt_ext::length
size_t length
Definition: ares.h:564
ares_txt_ext::txt
unsigned char * txt
Definition: ares.h:563
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
ares_nameser.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:43