2nd try adding CVS id.
[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 #include <sys/types.h>
20
21 #if defined(WIN32) && !defined(WATT32)
22 #include "nameser.h"
23 #else
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 #ifdef HAVE_ARPA_NAMESER_H
29 #include <arpa/nameser.h>
30 #endif
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
33 #endif
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include "ares.h"
39 #include "ares_dns.h"
40 #include "inet_net_pton.h"
41 #include "ares_private.h"
42
43 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
44                        struct hostent **host)
45 {
46   unsigned int qdcount, ancount;
47   int status, i, rr_type, rr_class, rr_len, naddrs;
48   int naliases;
49   long len;
50   const unsigned char *aptr;
51   char *hostname, *rr_name, *rr_data, **aliases;
52   struct in6_addr *addrs;
53   struct hostent *hostent;
54
55   /* Set *host to NULL for all failure cases. */
56   *host = NULL;
57
58   /* Give up if abuf doesn't have room for a header. */
59   if (alen < HFIXEDSZ)
60     return ARES_EBADRESP;
61
62   /* Fetch the question and answer count from the header. */
63   qdcount = DNS_HEADER_QDCOUNT(abuf);
64   ancount = DNS_HEADER_ANCOUNT(abuf);
65   if (qdcount != 1)
66     return ARES_EBADRESP;
67
68   /* Expand the name from the question, and skip past the question. */
69   aptr = abuf + HFIXEDSZ;
70   status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
71   if (status != ARES_SUCCESS)
72     return status;
73   if (aptr + len + QFIXEDSZ > abuf + alen)
74     {
75       free(hostname);
76       return ARES_EBADRESP;
77     }
78   aptr += len + QFIXEDSZ;
79
80   /* Allocate addresses and aliases; ancount gives an upper bound for both. */
81   addrs = malloc(ancount * sizeof(struct in6_addr));
82   if (!addrs)
83     {
84       free(hostname);
85       return ARES_ENOMEM;
86     }
87   aliases = malloc((ancount + 1) * sizeof(char *));
88   if (!aliases)
89     {
90       free(hostname);
91       free(addrs);
92       return ARES_ENOMEM;
93     }
94   naddrs = 0;
95   naliases = 0;
96
97   /* Examine each answer resource record (RR) in turn. */
98   for (i = 0; i < (int)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         break;
104       aptr += len;
105       if (aptr + RRFIXEDSZ > abuf + alen)
106         {
107           status = ARES_EBADRESP;
108           break;
109         }
110       rr_type = DNS_RR_TYPE(aptr);
111       rr_class = DNS_RR_CLASS(aptr);
112       rr_len = DNS_RR_LEN(aptr);
113       aptr += RRFIXEDSZ;
114
115       if (rr_class == C_IN && rr_type == T_AAAA
116           && rr_len == sizeof(struct in6_addr)
117           && strcasecmp(rr_name, hostname) == 0)
118         {
119           memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
120           naddrs++;
121           status = ARES_SUCCESS;
122         }
123
124       if (rr_class == C_IN && rr_type == T_CNAME)
125         {
126           /* Record the RR name as an alias. */
127           aliases[naliases] = rr_name;
128           naliases++;
129
130           /* Decode the RR data and replace the hostname with it. */
131           status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
132           if (status != ARES_SUCCESS)
133             break;
134           free(hostname);
135           hostname = rr_data;
136         }
137       else
138         free(rr_name);
139
140       aptr += rr_len;
141       if (aptr > abuf + alen)
142         {
143           status = ARES_EBADRESP;
144           break;
145         }
146     }
147
148   if (status == ARES_SUCCESS && naddrs == 0)
149     status = ARES_ENODATA;
150   if (status == ARES_SUCCESS)
151     {
152       /* We got our answer.  Allocate memory to build the host entry. */
153       aliases[naliases] = NULL;
154       hostent = malloc(sizeof(struct hostent));
155       if (hostent)
156         {
157           hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
158           if (hostent->h_addr_list)
159             {
160               /* Fill in the hostent and return successfully. */
161               hostent->h_name = hostname;
162               hostent->h_aliases = aliases;
163               hostent->h_addrtype = AF_INET6;
164               hostent->h_length = sizeof(struct in6_addr);
165               for (i = 0; i < naddrs; i++)
166                 hostent->h_addr_list[i] = (char *) &addrs[i];
167               hostent->h_addr_list[naddrs] = NULL;
168               *host = hostent;
169               return ARES_SUCCESS;
170             }
171           free(hostent);
172         }
173       status = ARES_ENOMEM;
174     }
175   for (i = 0; i < naliases; i++)
176     free(aliases[i]);
177   free(aliases);
178   free(addrs);
179   free(hostname);
180   return status;
181 }