3 /* Copyright 2005 Dominick Meglio
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.
19 #include <sys/types.h>
21 #if defined(WIN32) && !defined(WATT32)
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
28 #ifdef HAVE_ARPA_NAMESER_H
29 #include <arpa/nameser.h>
31 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
32 #include <arpa/nameser_compat.h>
40 #include "inet_net_pton.h"
41 #include "ares_private.h"
43 int ares_parse_aaaa_reply(const unsigned char *abuf, int alen,
44 struct hostent **host)
46 unsigned int qdcount, ancount;
47 int status, i, rr_type, rr_class, rr_len, naddrs;
50 const unsigned char *aptr;
51 char *hostname, *rr_name, *rr_data, **aliases;
52 struct in6_addr *addrs;
53 struct hostent *hostent;
55 /* Set *host to NULL for all failure cases. */
58 /* Give up if abuf doesn't have room for a header. */
62 /* Fetch the question and answer count from the header. */
63 qdcount = DNS_HEADER_QDCOUNT(abuf);
64 ancount = DNS_HEADER_ANCOUNT(abuf);
68 /* Expand the name from the question, and skip past the question. */
69 aptr = abuf + HFIXEDSZ;
70 status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
71 if (status != ARES_SUCCESS)
73 if (aptr + len + QFIXEDSZ > abuf + alen)
78 aptr += len + QFIXEDSZ;
80 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
81 addrs = malloc(ancount * sizeof(struct in6_addr));
87 aliases = malloc((ancount + 1) * sizeof(char *));
97 /* Examine each answer resource record (RR) in turn. */
98 for (i = 0; i < (int)ancount; i++)
100 /* Decode the RR up to the data field. */
101 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
102 if (status != ARES_SUCCESS)
105 if (aptr + RRFIXEDSZ > abuf + alen)
107 status = ARES_EBADRESP;
110 rr_type = DNS_RR_TYPE(aptr);
111 rr_class = DNS_RR_CLASS(aptr);
112 rr_len = DNS_RR_LEN(aptr);
115 if (rr_class == C_IN && rr_type == T_AAAA
116 && rr_len == sizeof(struct in6_addr)
117 && strcasecmp(rr_name, hostname) == 0)
119 memcpy(&addrs[naddrs], aptr, sizeof(struct in6_addr));
121 status = ARES_SUCCESS;
124 if (rr_class == C_IN && rr_type == T_CNAME)
126 /* Record the RR name as an alias. */
127 aliases[naliases] = rr_name;
130 /* Decode the RR data and replace the hostname with it. */
131 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
132 if (status != ARES_SUCCESS)
141 if (aptr > abuf + alen)
143 status = ARES_EBADRESP;
148 if (status == ARES_SUCCESS && naddrs == 0)
149 status = ARES_ENODATA;
150 if (status == ARES_SUCCESS)
152 /* We got our answer. Allocate memory to build the host entry. */
153 aliases[naliases] = NULL;
154 hostent = malloc(sizeof(struct hostent));
157 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
158 if (hostent->h_addr_list)
160 /* Fill in the hostent and return successfully. */
161 hostent->h_name = hostname;
162 hostent->h_aliases = aliases;
163 hostent->h_addrtype = AF_INET6;
164 hostent->h_length = sizeof(struct in6_addr);
165 for (i = 0; i < naddrs; i++)
166 hostent->h_addr_list[i] = (char *) &addrs[i];
167 hostent->h_addr_list[naddrs] = NULL;
173 status = ARES_ENOMEM;
175 for (i = 0; i < naliases; i++)