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