Jakub Hrozek modified ares_parse_srv_reply() and ares_parse_txt_reply() API
[platform/upstream/c-ares.git] / ares_parse_txt_reply.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  * Copyright (C) 2009 by Jakub Hrozek <jhrozek@redhat.com>
5  *
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.
17  */
18
19 #include "ares_setup.h"
20
21 #ifdef HAVE_SYS_SOCKET_H
22 #  include <sys/socket.h>
23 #endif
24 #ifdef HAVE_NETINET_IN_H
25 #  include <netinet/in.h>
26 #endif
27 #ifdef HAVE_NETDB_H
28 #  include <netdb.h>
29 #endif
30 #ifdef HAVE_ARPA_INET_H
31 #  include <arpa/inet.h>
32 #endif
33 #ifdef HAVE_ARPA_NAMESER_H
34 #  include <arpa/nameser.h>
35 #else
36 #  include "nameser.h"
37 #endif
38 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
39 #  include <arpa/nameser_compat.h>
40 #endif
41
42 #ifdef HAVE_STRINGS_H
43 #  include <strings.h>
44 #endif
45
46 #include <stdlib.h>
47 #include <string.h>
48
49 #include "ares.h"
50 #include "ares_dns.h"
51 #include "ares_data.h"
52 #include "ares_private.h"
53
54 int
55 ares_parse_txt_reply (const unsigned char *abuf, int alen,
56                       struct ares_txt_reply **txt_out)
57 {
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;
63   long 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;
68
69   /* Set *txt_out to NULL for all failure cases. */
70   *txt_out = NULL;
71
72   /* Give up if abuf doesn't have room for a header. */
73   if (alen < HFIXEDSZ)
74     return ARES_EBADRESP;
75
76   /* Fetch the question and answer count from the header. */
77   qdcount = DNS_HEADER_QDCOUNT (abuf);
78   ancount = DNS_HEADER_ANCOUNT (abuf);
79   if (qdcount != 1)
80     return ARES_EBADRESP;
81   if (ancount == 0)
82     return ARES_ENODATA;
83
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)
88     return status;
89
90   if (aptr + len + QFIXEDSZ > abuf + alen)
91     {
92       free (hostname);
93       return ARES_EBADRESP;
94     }
95   aptr += len + QFIXEDSZ;
96
97   /* Examine each answer resource record (RR) in turn. */
98   for (i = 0; i < ancount; i++)
99     {
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)
103         {
104           break;
105         }
106       aptr += len;
107       if (aptr + RRFIXEDSZ > abuf + alen)
108         {
109           status = ARES_EBADRESP;
110           break;
111         }
112       rr_type = DNS_RR_TYPE (aptr);
113       rr_class = DNS_RR_CLASS (aptr);
114       rr_len = DNS_RR_LEN (aptr);
115       aptr += RRFIXEDSZ;
116
117       /* Check if we are really looking at a TXT record */
118       if (rr_class == C_IN && rr_type == T_TXT)
119         {
120           /* Allocate storage for this SRV answer appending it to the list */
121           txt_curr = ares_malloc_data(ARES_DATATYPE_TXT_REPLY);
122           if (!txt_curr)
123             {
124               status = ARES_ENOMEM;
125               break;
126             }
127           if (txt_last)
128             {
129               txt_last->next = txt_curr;
130             }
131           else
132             {
133               txt_head = txt_curr;
134             }
135           txt_last = txt_curr;
136
137           /*
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.
143            */
144
145           /* Compute total length to allow a single memory allocation */
146           strptr = aptr;
147           while (strptr < (aptr + rr_len))
148             {
149               substr_len = (unsigned char)*strptr;
150               txt_curr->length += substr_len;
151               strptr += substr_len + 1;
152             }
153
154           /* Including null byte */
155           txt_curr->txt = malloc (txt_curr->length + 1);
156           if (txt_curr->txt == NULL)
157             {
158               status = ARES_ENOMEM;
159               break;
160             }
161
162           /* Step through the list of substrings, concatenating them */
163           str_len = 0;
164           strptr = aptr;
165           while (strptr < (aptr + rr_len))
166             {
167               substr_len = (unsigned char)*strptr;
168               strptr++;
169               memcpy ((char *) txt_curr->txt + str_len, strptr, substr_len);
170               str_len += substr_len;
171               strptr += substr_len;
172             }
173           /* Make sure we NULL-terminate */
174           *((char *) txt_curr->txt + txt_curr->length) = '\0';
175
176           /* Move on to the next record */
177           aptr += rr_len;
178         }
179
180       /* Don't lose memory in the next iteration */
181       free (rr_name);
182       rr_name = NULL;
183     }
184
185   if (hostname)
186     free (hostname);
187   if (rr_name)
188     free (rr_name);
189
190   /* clean up on error */
191   if (status != ARES_SUCCESS)
192     {
193       if (txt_head)
194         ares_free_data (txt_head);
195       return status;
196     }
197
198   /* everything looks fine, return the data */
199   *txt_out = txt_head;
200
201   return ARES_SUCCESS;
202 }