1 /* Copyright 1998 by the Massachusetts Institute of Technology.
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.
16 static const char rcsid[] = "$Id: ares_parse_a_reply.c,v 1.2 1998/08/17 21:45:51 ghudson Exp $";
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>
28 #include "ares_private.h"
30 int ares_parse_a_reply(const unsigned char *abuf, int alen,
31 struct hostent **host)
33 unsigned int qdcount, ancount;
34 int status, i, len, rr_type, rr_class, rr_len, naddrs;
36 const unsigned char *aptr;
37 char *hostname, *rr_name, *rr_data, **aliases;
38 struct in_addr *addrs;
39 struct hostent *hostent;
41 /* Set *host to NULL for all failure cases. */
44 /* Give up if abuf doesn't have room for a header. */
48 /* Fetch the question and answer count from the header. */
49 qdcount = DNS_HEADER_QDCOUNT(abuf);
50 ancount = DNS_HEADER_ANCOUNT(abuf);
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)
59 if (aptr + len + QFIXEDSZ > abuf + alen)
64 aptr += len + QFIXEDSZ;
66 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
67 addrs = malloc(ancount * sizeof(struct in_addr));
73 aliases = malloc((ancount + 1) * sizeof(char *));
83 /* Examine each answer resource record (RR) in turn. */
84 for (i = 0; i < ancount; i++)
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)
91 if (aptr + RRFIXEDSZ > abuf + alen)
93 status = ARES_EBADRESP;
96 rr_type = DNS_RR_TYPE(aptr);
97 rr_class = DNS_RR_CLASS(aptr);
98 rr_len = DNS_RR_LEN(aptr);
101 if (rr_class == C_IN && rr_type == T_A
102 && rr_len == sizeof(struct in_addr)
103 && strcasecmp(rr_name, hostname) == 0)
105 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
107 status = ARES_SUCCESS;
110 if (rr_class == C_IN && rr_type == T_CNAME)
112 /* Record the RR name as an alias. */
113 aliases[naliases] = rr_name;
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)
127 if (aptr > abuf + alen)
129 status = ARES_EBADRESP;
134 if (status == ARES_SUCCESS && naddrs == 0)
135 status = ARES_ENODATA;
136 if (status == ARES_SUCCESS)
138 /* We got our answer. Allocate memory to build the host entry. */
139 aliases[naliases] = NULL;
140 hostent = malloc(sizeof(struct hostent));
143 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
144 if (hostent->h_addr_list)
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;
159 status = ARES_ENOMEM;
161 for (i = 0; i < naliases; i++)