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