Create ares_build.h when buidling from Git.
[platform/upstream/c-ares.git] / ares_parse_a_reply.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
4  * Permission to use, copy, modify, and distribute this
5  * software and its documentation for any purpose and without
6  * fee is hereby granted, provided that the above copyright
7  * notice appear in all copies and that both that copyright
8  * notice and this permission notice appear in supporting
9  * documentation, and that the name of M.I.T. not be used in
10  * advertising or publicity pertaining to distribution of the
11  * software without specific, written prior permission.
12  * M.I.T. makes no representations about the suitability of
13  * this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  */
16
17 #include "ares_setup.h"
18
19 #ifdef HAVE_NETINET_IN_H
20 #  include <netinet/in.h>
21 #endif
22 #ifdef HAVE_NETDB_H
23 #  include <netdb.h>
24 #endif
25 #ifdef HAVE_ARPA_INET_H
26 #  include <arpa/inet.h>
27 #endif
28 #ifdef HAVE_ARPA_NAMESER_H
29 #  include <arpa/nameser.h>
30 #else
31 #  include "nameser.h"
32 #endif
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 #  include <arpa/nameser_compat.h>
35 #endif
36
37 #ifdef HAVE_STRINGS_H
38 #  include <strings.h>
39 #endif
40
41 #ifdef HAVE_LIMITS_H
42 #  include <limits.h>
43 #endif
44
45 #include "ares.h"
46 #include "ares_dns.h"
47 #include "ares_private.h"
48
49 int ares_parse_a_reply(const unsigned char *abuf, int alen,
50                        struct hostent **host,
51                        struct ares_addrttl *addrttls, int *naddrttls)
52 {
53   unsigned int qdcount, ancount;
54   int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
55   int cname_ttl = INT_MAX;  /* the TTL imposed by the CNAME chain */
56   int naliases;
57   long len;
58   const unsigned char *aptr;
59   char *hostname, *rr_name, *rr_data, **aliases;
60   struct in_addr *addrs;
61   struct hostent *hostent;
62   const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
63
64   /* Set *host to NULL for all failure cases. */
65   if (host)
66     *host = NULL;
67   /* Same with *naddrttls. */
68   if (naddrttls)
69     *naddrttls = 0;
70
71   /* Give up if abuf doesn't have room for a header. */
72   if (alen < HFIXEDSZ)
73     return ARES_EBADRESP;
74
75   /* Fetch the question and answer count from the header. */
76   qdcount = DNS_HEADER_QDCOUNT(abuf);
77   ancount = DNS_HEADER_ANCOUNT(abuf);
78   if (qdcount != 1)
79     return ARES_EBADRESP;
80
81   /* Expand the name from the question, and skip past the question. */
82   aptr = abuf + HFIXEDSZ;
83   status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
84   if (status != ARES_SUCCESS)
85     return status;
86   if (aptr + len + QFIXEDSZ > abuf + alen)
87     {
88       free(hostname);
89       return ARES_EBADRESP;
90     }
91   aptr += len + QFIXEDSZ;
92
93   if (host)
94     {
95       /* Allocate addresses and aliases; ancount gives an upper bound for
96          both. */
97       addrs = malloc(ancount * sizeof(struct in_addr));
98       if (!addrs)
99         {
100           free(hostname);
101           return ARES_ENOMEM;
102         }
103       aliases = malloc((ancount + 1) * sizeof(char *));
104       if (!aliases)
105         {
106           free(hostname);
107           free(addrs);
108           return ARES_ENOMEM;
109         }
110     }
111   else
112     {
113       addrs = NULL;
114       aliases = NULL;
115     }
116
117   naddrs = 0;
118   naliases = 0;
119
120   /* Examine each answer resource record (RR) in turn. */
121   for (i = 0; i < (int)ancount; i++)
122     {
123       /* Decode the RR up to the data field. */
124       status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
125       if (status != ARES_SUCCESS)
126         break;
127       aptr += len;
128       if (aptr + RRFIXEDSZ > abuf + alen)
129         {
130           free(rr_name);
131           status = ARES_EBADRESP;
132           break;
133         }
134       rr_type = DNS_RR_TYPE(aptr);
135       rr_class = DNS_RR_CLASS(aptr);
136       rr_len = DNS_RR_LEN(aptr);
137       rr_ttl = DNS_RR_TTL(aptr);
138       aptr += RRFIXEDSZ;
139
140       if (rr_class == C_IN && rr_type == T_A
141           && rr_len == sizeof(struct in_addr)
142           && strcasecmp(rr_name, hostname) == 0)
143         {
144           if (addrs)
145             {
146               if (aptr + sizeof(struct in_addr) > abuf + alen)
147               {
148                 free(rr_name);
149                 status = ARES_EBADRESP;
150                 break;
151               }
152               memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
153             }
154           if (naddrs < max_addr_ttls)
155             {
156               struct ares_addrttl * const at = &addrttls[naddrs];
157               if (aptr + sizeof(struct in_addr) > abuf + alen)
158               {
159                 free(rr_name);
160                 status = ARES_EBADRESP;
161                 break;
162               }
163               memcpy(&at->ipaddr, aptr,  sizeof(struct in_addr));
164               at->ttl = rr_ttl;
165             }
166           naddrs++;
167           status = ARES_SUCCESS;
168         }
169
170       if (rr_class == C_IN && rr_type == T_CNAME)
171         {
172           /* Record the RR name as an alias. */
173           if (aliases)
174             aliases[naliases] = rr_name;
175           else
176             free(rr_name);
177           naliases++;
178
179           /* Decode the RR data and replace the hostname with it. */
180           status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
181                                                   &len);
182           if (status != ARES_SUCCESS)
183             break;
184           free(hostname);
185           hostname = rr_data;
186
187           /* Take the min of the TTLs we see in the CNAME chain. */
188           if (cname_ttl > rr_ttl)
189             cname_ttl = rr_ttl;
190         }
191       else
192         free(rr_name);
193
194       aptr += rr_len;
195       if (aptr > abuf + alen)
196         {
197           status = ARES_EBADRESP;
198           break;
199         }
200     }
201
202   if (status == ARES_SUCCESS && naddrs == 0 && naliases == 0)
203     /* the check for naliases to be zero is to make sure CNAME responses
204        don't get caught here */
205     status = ARES_ENODATA;
206   if (status == ARES_SUCCESS)
207     {
208       /* We got our answer. */
209       if (naddrttls)
210         {
211           const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
212           for (i = 0; i < n; i++)
213             {
214               /* Ensure that each A TTL is no larger than the CNAME TTL. */
215               if (addrttls[i].ttl > cname_ttl)
216                 addrttls[i].ttl = cname_ttl;
217             }
218           *naddrttls = n;
219         }
220       if (aliases)
221         aliases[naliases] = NULL;
222       if (host)
223         {
224           /* Allocate memory to build the host entry. */
225           hostent = malloc(sizeof(struct hostent));
226           if (hostent)
227             {
228               hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
229               if (hostent->h_addr_list)
230                 {
231                   /* Fill in the hostent and return successfully. */
232                   hostent->h_name = hostname;
233                   hostent->h_aliases = aliases;
234                   hostent->h_addrtype = AF_INET;
235                   hostent->h_length = sizeof(struct in_addr);
236                   for (i = 0; i < naddrs; i++)
237                     hostent->h_addr_list[i] = (char *) &addrs[i];
238                   hostent->h_addr_list[naddrs] = NULL;
239                   if (!naddrs && addrs)
240                     free(addrs);
241                   *host = hostent;
242                   return ARES_SUCCESS;
243                 }
244               free(hostent);
245             }
246           status = ARES_ENOMEM;
247         }
248      }
249   if (aliases)
250     {
251       for (i = 0; i < naliases; i++)
252         free(aliases[i]);
253       free(aliases);
254     }
255   free(addrs);
256   free(hostname);
257   return status;
258 }