ares_set_servers_csv: fixed IPv6 address parsing
[platform/upstream/c-ares.git] / ares_parse_txt_reply.c
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 int
48 ares_parse_txt_reply (const unsigned char *abuf, int alen,
49                       struct ares_txt_reply **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_reply *txt_head = NULL;
59   struct ares_txt_reply *txt_last = NULL;
60   struct ares_txt_reply *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       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               ++strptr;
137
138               /* Allocate storage for this TXT answer appending it to the list */
139               txt_curr = ares_malloc_data(ARES_DATATYPE_TXT_REPLY);
140               if (!txt_curr)
141                 {
142                   status = ARES_ENOMEM;
143                   break;
144                 }
145               if (txt_last)
146                 {
147                   txt_last->next = txt_curr;
148                 }
149               else
150                 {
151                   txt_head = txt_curr;
152                 }
153               txt_last = txt_curr;
154
155               txt_curr->length = substr_len;
156               txt_curr->txt = malloc (substr_len + 1/* Including null byte */);
157               if (txt_curr->txt == NULL)
158                 {
159                   status = ARES_ENOMEM;
160                   break;
161                 }
162               memcpy ((char *) txt_curr->txt, strptr, substr_len);
163
164               /* Make sure we NULL-terminate */
165               txt_curr->txt[substr_len] = 0;
166
167               strptr += substr_len;
168             }
169         }
170
171       /* Don't lose memory in the next iteration */
172       free (rr_name);
173       rr_name = NULL;
174
175       /* Move on to the next record */
176       aptr += rr_len;
177     }
178
179   if (hostname)
180     free (hostname);
181   if (rr_name)
182     free (rr_name);
183
184   /* clean up on error */
185   if (status != ARES_SUCCESS)
186     {
187       if (txt_head)
188         ares_free_data (txt_head);
189       return status;
190     }
191
192   /* everything looks fine, return the data */
193   *txt_out = txt_head;
194
195   return ARES_SUCCESS;
196 }