3 /* Copyright 1998 by the Massachusetts Institute of Technology.
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 #include <arpa/nameser.h>
29 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
30 #include <arpa/nameser_compat.h>
38 #include "ares_private.h"
40 int ares_parse_a_reply(const unsigned char *abuf, int alen,
41 struct hostent **host)
43 unsigned int qdcount, ancount;
44 int status, i, rr_type, rr_class, rr_len, naddrs;
47 const unsigned char *aptr;
48 char *hostname, *rr_name, *rr_data, **aliases;
49 struct in_addr *addrs;
50 struct hostent *hostent;
52 /* Set *host to NULL for all failure cases. */
55 /* Give up if abuf doesn't have room for a header. */
59 /* Fetch the question and answer count from the header. */
60 qdcount = DNS_HEADER_QDCOUNT(abuf);
61 ancount = DNS_HEADER_ANCOUNT(abuf);
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)
70 if (aptr + len + QFIXEDSZ > abuf + alen)
75 aptr += len + QFIXEDSZ;
77 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
78 addrs = malloc(ancount * sizeof(struct in_addr));
84 aliases = malloc((ancount + 1) * sizeof(char *));
94 /* Examine each answer resource record (RR) in turn. */
95 for (i = 0; i < (int)ancount; i++)
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)
102 if (aptr + RRFIXEDSZ > abuf + alen)
104 status = ARES_EBADRESP;
107 rr_type = DNS_RR_TYPE(aptr);
108 rr_class = DNS_RR_CLASS(aptr);
109 rr_len = DNS_RR_LEN(aptr);
112 if (rr_class == C_IN && rr_type == T_A
113 && rr_len == sizeof(struct in_addr)
114 && strcasecmp(rr_name, hostname) == 0)
116 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
118 status = ARES_SUCCESS;
121 if (rr_class == C_IN && rr_type == T_CNAME)
123 /* Record the RR name as an alias. */
124 aliases[naliases] = rr_name;
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)
138 if (aptr > abuf + alen)
140 status = ARES_EBADRESP;
145 if (status == ARES_SUCCESS && naddrs == 0)
146 status = ARES_ENODATA;
147 if (status == ARES_SUCCESS)
149 /* We got our answer. Allocate memory to build the host entry. */
150 aliases[naliases] = NULL;
151 hostent = malloc(sizeof(struct hostent));
154 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
155 if (hostent->h_addr_list)
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;
170 status = ARES_ENOMEM;
172 for (i = 0; i < naliases; i++)