- Jakub Hrozek renamed addrttl and addr6ttl structs to ares_addrttl and
[platform/upstream/c-ares.git] / ares_parse_aaaa_reply.c
1 /* $Id$ */
2
3 /* Copyright 1998 by the Massachusetts Institute of Technology.
4  * Copyright 2005 Dominick Meglio
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 #ifdef HAVE_LIMITS_H
49 #  include <limits.h>
50 #endif
51
52 #include "ares.h"
53 #include "ares_dns.h"
54 #include "inet_net_pton.h"
55 #include "ares_private.h"
56
57 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
58                           struct hostent **host, struct ares_addr6ttl *addrttls,
59                           int *naddrttls)
60 {
61   unsigned int qdcount, ancount;
62   int status, i, rr_type, rr_class, rr_len, rr_ttl, naddrs;
63   int cname_ttl = INT_MAX;  /* the TTL imposed by the CNAME chain */
64   int naliases;
65   long len;
66   const unsigned char *aptr;
67   char *hostname, *rr_name, *rr_data, **aliases;
68   struct in6_addr *addrs;
69   struct hostent *hostent;
70   const int max_addr_ttls = (addrttls && naddrttls) ? *naddrttls : 0;
71
72   /* Set *host to NULL for all failure cases. */
73   if (host)
74     *host = NULL;
75   /* Same with *naddrttls. */
76   if (naddrttls)
77     *naddrttls = 0;
78
79   /* Give up if abuf doesn't have room for a header. */
80   if (alen < HFIXEDSZ)
81     return ARES_EBADRESP;
82
83   /* Fetch the question and answer count from the header. */
84   qdcount = DNS_HEADER_QDCOUNT(abuf);
85   ancount = DNS_HEADER_ANCOUNT(abuf);
86   if (qdcount != 1)
87     return ARES_EBADRESP;
88
89   /* Expand the name from the question, and skip past the question. */
90   aptr = abuf + HFIXEDSZ;
91   status = ares__expand_name_for_response(aptr, abuf, alen, &hostname, &len);
92   if (status != ARES_SUCCESS)
93     return status;
94   if (aptr + len + QFIXEDSZ > abuf + alen)
95     {
96       free(hostname);
97       return ARES_EBADRESP;
98     }
99   aptr += len + QFIXEDSZ;
100
101   /* Allocate addresses and aliases; ancount gives an upper bound for both. */
102   if (host)
103     {
104       addrs = malloc(ancount * sizeof(struct in6_addr));
105       if (!addrs)
106         {
107           free(hostname);
108           return ARES_ENOMEM;
109         }
110       aliases = malloc((ancount + 1) * sizeof(char *));
111       if (!aliases)
112         {
113           free(hostname);
114           free(addrs);
115           return ARES_ENOMEM;
116         }
117     }
118   else
119     {
120       addrs = NULL;
121       aliases = NULL;
122     }
123   naddrs = 0;
124   naliases = 0;
125
126   /* Examine each answer resource record (RR) in turn. */
127   for (i = 0; i < (int)ancount; i++)
128     {
129       /* Decode the RR up to the data field. */
130       status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
131       if (status != ARES_SUCCESS)
132         break;
133       aptr += len;
134       if (aptr + RRFIXEDSZ > abuf + alen)
135         {
136           status = ARES_EBADRESP;
137           break;
138         }
139       rr_type = DNS_RR_TYPE(aptr);
140       rr_class = DNS_RR_CLASS(aptr);
141       rr_len = DNS_RR_LEN(aptr);
142       rr_ttl = DNS_RR_TTL(aptr);
143       aptr += RRFIXEDSZ;
144
145       if (rr_class == C_IN && rr_type == T_AAAA
146           && rr_len == sizeof(struct in6_addr)
147           && strcasecmp(rr_name, hostname) == 0)
148         {
149           if (addrs)
150             {
151               if (aptr + sizeof(struct in6_addr) > abuf + alen)
152               {
153                 status = ARES_EBADRESP;
154                 break;
155               }
156               memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
157             }
158           if (naddrs < max_addr_ttls)
159             {
160               struct ares_addr6ttl * const at = &addrttls[naddrs];
161               if (aptr + sizeof(struct in6_addr) > abuf + alen)
162               {
163                 status = ARES_EBADRESP;
164                 break;
165               }
166               memcpy(&at->ip6addr, aptr,  sizeof(struct in6_addr));
167               at->ttl = rr_ttl;
168             }
169           naddrs++;
170           status = ARES_SUCCESS;
171         }
172
173       if (rr_class == C_IN && rr_type == T_CNAME)
174         {
175           /* Record the RR name as an alias. */
176           if (aliases)
177             aliases[naliases] = rr_name;
178           else
179             free(rr_name);
180           naliases++;
181
182           /* Decode the RR data and replace the hostname with it. */
183           status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
184                                                   &len);
185           if (status != ARES_SUCCESS)
186             break;
187           free(hostname);
188           hostname = rr_data;
189
190           /* Take the min of the TTLs we see in the CNAME chain. */
191           if (cname_ttl > rr_ttl)
192             cname_ttl = rr_ttl;
193         }
194       else
195         free(rr_name);
196
197       aptr += rr_len;
198       if (aptr > abuf + alen)
199         {
200           status = ARES_EBADRESP;
201           break;
202         }
203     }
204
205   if (status == ARES_SUCCESS && naddrs == 0)
206     status = ARES_ENODATA;
207   if (status == ARES_SUCCESS)
208     {
209       /* We got our answer. */
210       if (naddrttls)
211         {
212           const int n = naddrs < max_addr_ttls ? naddrs : max_addr_ttls;
213           for (i = 0; i < n; i++)
214             {
215               /* Ensure that each A TTL is no larger than the CNAME TTL. */
216               if (addrttls[i].ttl > cname_ttl)
217                 addrttls[i].ttl = cname_ttl;
218             }
219           *naddrttls = n;
220         }
221       if (aliases)
222         aliases[naliases] = NULL;
223       if (host)
224         {
225           /* Allocate memory to build the host entry. */
226           hostent = malloc(sizeof(struct hostent));
227           if (hostent)
228             {
229               hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
230               if (hostent->h_addr_list)
231                 {
232                   /* Fill in the hostent and return successfully. */
233                   hostent->h_name = hostname;
234                   hostent->h_aliases = aliases;
235                   hostent->h_addrtype = AF_INET6;
236                   hostent->h_length = sizeof(struct in6_addr);
237                   for (i = 0; i < naddrs; i++)
238                     hostent->h_addr_list[i] = (char *) &addrs[i];
239                   hostent->h_addr_list[naddrs] = NULL;
240                   *host = hostent;
241                   return ARES_SUCCESS;
242                 }
243               free(hostent);
244             }
245           status = ARES_ENOMEM;
246         }
247     }
248   if (aliases)
249     {
250       for (i = 0; i < naliases; i++)
251         free(aliases[i]);
252       free(aliases);
253     }
254   free(addrs);
255   free(hostname);
256   return status;
257 }