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