ares.h: there is no ares_free_soa function
[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, str_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
110       /* Check if we are really looking at a TXT record */
111       if (rr_class == C_IN && rr_type == T_TXT)
112         {
113           /* Allocate storage for this TXT answer appending it to the list */
114           txt_curr = ares_malloc_data(ARES_DATATYPE_TXT_REPLY);
115           if (!txt_curr)
116             {
117               status = ARES_ENOMEM;
118               break;
119             }
120           if (txt_last)
121             {
122               txt_last->next = txt_curr;
123             }
124           else
125             {
126               txt_head = txt_curr;
127             }
128           txt_last = txt_curr;
129
130           /*
131            * There may be multiple substrings in a single TXT record. Each
132            * substring may be up to 255 characters in length, with a
133            * "length byte" indicating the size of the substring payload.
134            * RDATA contains both the length-bytes and payloads of all
135            * substrings contained therein.
136            */
137
138           /* Compute total length to allow a single memory allocation */
139           strptr = aptr;
140           while (strptr < (aptr + rr_len))
141             {
142               substr_len = (unsigned char)*strptr;
143               txt_curr->length += substr_len;
144               strptr += substr_len + 1;
145             }
146
147           /* Including null byte */
148           txt_curr->txt = malloc (txt_curr->length + 1);
149           if (txt_curr->txt == NULL)
150             {
151               status = ARES_ENOMEM;
152               break;
153             }
154
155           /* Step through the list of substrings, concatenating them */
156           str_len = 0;
157           strptr = aptr;
158           while (strptr < (aptr + rr_len))
159             {
160               substr_len = (unsigned char)*strptr;
161               strptr++;
162               memcpy ((char *) txt_curr->txt + str_len, strptr, substr_len);
163               str_len += substr_len;
164               strptr += substr_len;
165             }
166           /* Make sure we NULL-terminate */
167           *((char *) txt_curr->txt + txt_curr->length) = '\0';
168         }
169
170       /* Don't lose memory in the next iteration */
171       free (rr_name);
172       rr_name = NULL;
173
174       /* Move on to the next record */
175       aptr += rr_len;
176     }
177
178   if (hostname)
179     free (hostname);
180   if (rr_name)
181     free (rr_name);
182
183   /* clean up on error */
184   if (status != ARES_SUCCESS)
185     {
186       if (txt_head)
187         ares_free_data (txt_head);
188       return status;
189     }
190
191   /* everything looks fine, return the data */
192   *txt_out = txt_head;
193
194   return ARES_SUCCESS;
195 }