2 /* Copyright 1998 by the Massachusetts Institute of Technology.
4 * Permission to use, copy, modify, and distribute this
5 * software and its documentation for any purpose and without
6 * fee is hereby granted, provided that the above copyright
7 * notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting
9 * documentation, and that the name of M.I.T. not be used in
10 * advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission.
12 * M.I.T. makes no representations about the suitability of
13 * this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
17 #include "ares_setup.h"
19 #ifdef HAVE_NETINET_IN_H
20 # include <netinet/in.h>
25 #ifdef HAVE_ARPA_NAMESER_H
26 # include <arpa/nameser.h>
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 # include <arpa/nameser_compat.h>
40 #include "ares_nowarn.h"
41 #include "ares_private.h"
43 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
44 int addrlen, int family, struct hostent **host)
46 unsigned int qdcount, ancount;
47 int status, i, rr_type, rr_class, rr_len;
49 const unsigned char *aptr;
50 char *ptrname, *hostname, *rr_name, *rr_data;
51 struct hostent *hostent;
56 /* Set *host to NULL for all failure cases. */
59 /* Give up if abuf doesn't have room for a header. */
63 /* Fetch the question and answer count from the header. */
64 qdcount = DNS_HEADER_QDCOUNT(abuf);
65 ancount = DNS_HEADER_ANCOUNT(abuf);
69 /* Expand the name from the question, and skip past the question. */
70 aptr = abuf + HFIXEDSZ;
71 status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
72 if (status != ARES_SUCCESS)
74 if (aptr + len + QFIXEDSZ > abuf + alen)
79 aptr += len + QFIXEDSZ;
81 /* Examine each answer resource record (RR) in turn. */
83 aliases = malloc(alias_alloc * sizeof(char *));
89 for (i = 0; i < (int)ancount; i++)
91 /* Decode the RR up to the data field. */
92 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
93 if (status != ARES_SUCCESS)
96 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);
106 if (aptr + rr_len > abuf + alen)
109 status = ARES_EBADRESP;
113 if (rr_class == C_IN && rr_type == T_PTR
114 && strcasecmp(rr_name, ptrname) == 0)
116 /* Decode the RR data and set hostname to it. */
117 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
119 if (status != ARES_SUCCESS)
127 aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char));
128 if (!aliases[aliascnt])
131 status = ARES_ENOMEM;
134 strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
136 if (aliascnt >= alias_alloc) {
139 ptr = realloc(aliases, alias_alloc * sizeof(char *));
142 status = ARES_ENOMEM;
149 if (rr_class == C_IN && rr_type == T_CNAME)
151 /* Decode the RR data and replace ptrname with it. */
152 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
154 if (status != ARES_SUCCESS)
165 if (aptr > abuf + alen)
167 status = ARES_EBADRESP;
172 if (status == ARES_SUCCESS && !hostname)
173 status = ARES_ENODATA;
174 if (status == ARES_SUCCESS)
176 /* We got our answer. Allocate memory to build the host entry. */
177 hostent = malloc(sizeof(struct hostent));
180 hostent->h_addr_list = malloc(2 * sizeof(char *));
181 if (hostent->h_addr_list)
183 hostent->h_addr_list[0] = malloc(addrlen);
184 if (hostent->h_addr_list[0])
186 hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
187 if (hostent->h_aliases)
189 /* Fill in the hostent and return successfully. */
190 hostent->h_name = hostname;
191 for (i=0 ; i<aliascnt ; i++)
192 hostent->h_aliases[i] = aliases[i];
193 hostent->h_aliases[aliascnt] = NULL;
194 hostent->h_addrtype = aresx_sitoss(family);
195 hostent->h_length = aresx_sitoss(addrlen);
196 memcpy(hostent->h_addr_list[0], addr, addrlen);
197 hostent->h_addr_list[1] = NULL;
203 free(hostent->h_addr_list[0]);
205 free(hostent->h_addr_list);
209 status = ARES_ENOMEM;
211 for (i=0 ; i<aliascnt ; i++)