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