Tupone Alfredo fixed includes of arpa/nameser_compat.h to build fine on Mac OS
[platform/upstream/c-ares.git] / ares_parse_a_reply.c
1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
3  * Permission to use, copy, modify, and distribute this
4  * software and its documentation for any purpose and without
5  * fee is hereby granted, provided that the above copyright
6  * notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting
8  * documentation, and that the name of M.I.T. not be used in
9  * advertising or publicity pertaining to distribution of the
10  * software without specific, written prior permission.
11  * M.I.T. makes no representations about the suitability of
12  * this software for any purpose.  It is provided "as is"
13  * without express or implied warranty.
14  */
15
16 #include "setup.h"
17 #include <sys/types.h>
18
19 #if defined(WIN32) && !defined(WATT32)
20 #include "nameser.h"
21 #else
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
29 #endif
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include "ares.h"
35 #include "ares_dns.h"
36 #include "ares_private.h"
37
38 int ares_parse_a_reply(const unsigned char *abuf, int alen,
39                        struct hostent **host)
40 {
41   unsigned int qdcount, ancount;
42   int status, i, rr_type, rr_class, rr_len, naddrs;
43   int naliases;
44   long len;
45   const unsigned char *aptr;
46   char *hostname, *rr_name, *rr_data, **aliases;
47   struct in_addr *addrs;
48   struct hostent *hostent;
49
50   /* Set *host to NULL for all failure cases. */
51   *host = NULL;
52
53   /* Give up if abuf doesn't have room for a header. */
54   if (alen < HFIXEDSZ)
55     return ARES_EBADRESP;
56
57   /* Fetch the question and answer count from the header. */
58   qdcount = DNS_HEADER_QDCOUNT(abuf);
59   ancount = DNS_HEADER_ANCOUNT(abuf);
60   if (qdcount != 1)
61     return ARES_EBADRESP;
62
63   /* Expand the name from the question, and skip past the question. */
64   aptr = abuf + HFIXEDSZ;
65   status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
66   if (status != ARES_SUCCESS)
67     return status;
68   if (aptr + len + QFIXEDSZ > abuf + alen)
69     {
70       free(hostname);
71       return ARES_EBADRESP;
72     }
73   aptr += len + QFIXEDSZ;
74
75   /* Allocate addresses and aliases; ancount gives an upper bound for both. */
76   addrs = malloc(ancount * sizeof(struct in_addr));
77   if (!addrs)
78     {
79       free(hostname);
80       return ARES_ENOMEM;
81     }
82   aliases = malloc((ancount + 1) * sizeof(char *));
83   if (!aliases)
84     {
85       free(hostname);
86       free(addrs);
87       return ARES_ENOMEM;
88     }
89   naddrs = 0;
90   naliases = 0;
91
92   /* Examine each answer resource record (RR) in turn. */
93   for (i = 0; i < (int)ancount; i++)
94     {
95       /* Decode the RR up to the data field. */
96       status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
97       if (status != ARES_SUCCESS)
98         break;
99       aptr += len;
100       if (aptr + RRFIXEDSZ > abuf + alen)
101         {
102           status = ARES_EBADRESP;
103           break;
104         }
105       rr_type = DNS_RR_TYPE(aptr);
106       rr_class = DNS_RR_CLASS(aptr);
107       rr_len = DNS_RR_LEN(aptr);
108       aptr += RRFIXEDSZ;
109
110       if (rr_class == C_IN && rr_type == T_A
111           && rr_len == sizeof(struct in_addr)
112           && strcasecmp(rr_name, hostname) == 0)
113         {
114           memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
115           naddrs++;
116           status = ARES_SUCCESS;
117         }
118
119       if (rr_class == C_IN && rr_type == T_CNAME)
120         {
121           /* Record the RR name as an alias. */
122           aliases[naliases] = rr_name;
123           naliases++;
124
125           /* Decode the RR data and replace the hostname with it. */
126           status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
127           if (status != ARES_SUCCESS)
128             break;
129           free(hostname);
130           hostname = rr_data;
131         }
132       else
133         free(rr_name);
134
135       aptr += rr_len;
136       if (aptr > abuf + alen)
137         {
138           status = ARES_EBADRESP;
139           break;
140         }
141     }
142
143   if (status == ARES_SUCCESS && naddrs == 0)
144     status = ARES_ENODATA;
145   if (status == ARES_SUCCESS)
146     {
147       /* We got our answer.  Allocate memory to build the host entry. */
148       aliases[naliases] = NULL;
149       hostent = malloc(sizeof(struct hostent));
150       if (hostent)
151         {
152           hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
153           if (hostent->h_addr_list)
154             {
155               /* Fill in the hostent and return successfully. */
156               hostent->h_name = hostname;
157               hostent->h_aliases = aliases;
158               hostent->h_addrtype = AF_INET;
159               hostent->h_length = sizeof(struct in_addr);
160               for (i = 0; i < naddrs; i++)
161                 hostent->h_addr_list[i] = (char *) &addrs[i];
162               hostent->h_addr_list[naddrs] = NULL;
163               *host = hostent;
164               return ARES_SUCCESS;
165             }
166           free(hostent);
167         }
168       status = ARES_ENOMEM;
169     }
170   for (i = 0; i < naliases; i++)
171     free(aliases[i]);
172   free(aliases);
173   free(addrs);
174   free(hostname);
175   return status;
176 }