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>
33 #include "ares_private.h"
35 int ares_parse_a_reply(const unsigned char *abuf, int alen,
36 struct hostent **host)
38 unsigned int qdcount, ancount;
39 int status, i, rr_type, rr_class, rr_len, naddrs;
42 const unsigned char *aptr;
43 char *hostname, *rr_name, *rr_data, **aliases;
44 struct in_addr *addrs;
45 struct hostent *hostent;
47 /* Set *host to NULL for all failure cases. */
50 /* Give up if abuf doesn't have room for a header. */
54 /* Fetch the question and answer count from the header. */
55 qdcount = DNS_HEADER_QDCOUNT(abuf);
56 ancount = DNS_HEADER_ANCOUNT(abuf);
60 /* Expand the name from the question, and skip past the question. */
61 aptr = abuf + HFIXEDSZ;
62 status = ares_expand_name(aptr, abuf, alen, &hostname, &len);
63 if (status != ARES_SUCCESS)
65 if (aptr + len + QFIXEDSZ > abuf + alen)
70 aptr += len + QFIXEDSZ;
72 /* Allocate addresses and aliases; ancount gives an upper bound for both. */
73 addrs = malloc(ancount * sizeof(struct in_addr));
79 aliases = malloc((ancount + 1) * sizeof(char *));
89 /* Examine each answer resource record (RR) in turn. */
90 for (i = 0; i < (int)ancount; i++)
92 /* Decode the RR up to the data field. */
93 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
94 if (status != ARES_SUCCESS)
97 if (aptr + RRFIXEDSZ > abuf + alen)
99 status = ARES_EBADRESP;
102 rr_type = DNS_RR_TYPE(aptr);
103 rr_class = DNS_RR_CLASS(aptr);
104 rr_len = DNS_RR_LEN(aptr);
107 if (rr_class == C_IN && rr_type == T_A
108 && rr_len == sizeof(struct in_addr)
109 && strcasecmp(rr_name, hostname) == 0)
111 memcpy(&addrs[naddrs], aptr, sizeof(struct in_addr));
113 status = ARES_SUCCESS;
116 if (rr_class == C_IN && rr_type == T_CNAME)
118 /* Record the RR name as an alias. */
119 aliases[naliases] = rr_name;
122 /* Decode the RR data and replace the hostname with it. */
123 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
124 if (status != ARES_SUCCESS)
133 if (aptr > abuf + alen)
135 status = ARES_EBADRESP;
140 if (status == ARES_SUCCESS && naddrs == 0)
141 status = ARES_ENODATA;
142 if (status == ARES_SUCCESS)
144 /* We got our answer. Allocate memory to build the host entry. */
145 aliases[naliases] = NULL;
146 hostent = malloc(sizeof(struct hostent));
149 hostent->h_addr_list = malloc((naddrs + 1) * sizeof(char *));
150 if (hostent->h_addr_list)
152 /* Fill in the hostent and return successfully. */
153 hostent->h_name = hostname;
154 hostent->h_aliases = aliases;
155 hostent->h_addrtype = AF_INET;
156 hostent->h_length = sizeof(struct in_addr);
157 for (i = 0; i < naddrs; i++)
158 hostent->h_addr_list[i] = (char *) &addrs[i];
159 hostent->h_addr_list[naddrs] = NULL;
165 status = ARES_ENOMEM;
167 for (i = 0; i < naliases; i++)