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.
17 #include <sys/types.h>
19 #if defined(WIN32) && !defined(WATT32)
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
36 #include "ares_private.h"
38 int ares_parse_a_reply(const unsigned char *abuf, int alen,
39 struct hostent **host)
41 unsigned int qdcount, ancount;
42 int status, i, rr_type, rr_class, rr_len, naddrs;
45 const unsigned char *aptr;
46 char *hostname, *rr_name, *rr_data, **aliases;
47 struct in_addr *addrs;
48 struct hostent *hostent;
50 /* Set *host to NULL for all failure cases. */
53 /* Give up if abuf doesn't have room for a header. */
57 /* Fetch the question and answer count from the header. */
58 qdcount = DNS_HEADER_QDCOUNT(abuf);
59 ancount = DNS_HEADER_ANCOUNT(abuf);
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)
68 if (aptr + len + QFIXEDSZ > abuf + alen)
73 aptr += len + QFIXEDSZ;
75 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
76 addrs = malloc(ancount * sizeof(struct in_addr));
82 aliases = malloc((ancount + 1) * sizeof(char *));
92 /* Examine each answer resource record (RR) in turn. */
93 for (i = 0; i < (int)ancount; i++)
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)
100 if (aptr + RRFIXEDSZ > abuf + alen)
102 status = ARES_EBADRESP;
105 rr_type = DNS_RR_TYPE(aptr);
106 rr_class = DNS_RR_CLASS(aptr);
107 rr_len = DNS_RR_LEN(aptr);
110 if (rr_class == C_IN && rr_type == T_A
111 && rr_len == sizeof(struct in_addr)
112 && strcasecmp(rr_name, hostname) == 0)
114 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
116 status = ARES_SUCCESS;
119 if (rr_class == C_IN && rr_type == T_CNAME)
121 /* Record the RR name as an alias. */
122 aliases[naliases] = rr_name;
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)
136 if (aptr > abuf + alen)
138 status = ARES_EBADRESP;
143 if (status == ARES_SUCCESS && naddrs == 0)
144 status = ARES_ENODATA;
145 if (status == ARES_SUCCESS)
147 /* We got our answer. Allocate memory to build the host entry. */
148 aliases[naliases] = NULL;
149 hostent = malloc(sizeof(struct hostent));
152 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
153 if (hostent->h_addr_list)
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;
168 status = ARES_ENOMEM;
170 for (i = 0; i < naliases; i++)