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;
57 /* Set *host to NULL for all failure cases. */
60 /* Give up if abuf doesn't have room for a header. */
64 /* Fetch the question and answer count from the header. */
65 qdcount = DNS_HEADER_QDCOUNT(abuf);
66 ancount = DNS_HEADER_ANCOUNT(abuf);
70 /* Expand the name from the question, and skip past the question. */
71 aptr = abuf + HFIXEDSZ;
72 status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
73 if (status != ARES_SUCCESS)
75 if (aptr + len + QFIXEDSZ > abuf + alen)
80 aptr += len + QFIXEDSZ;
82 /* Examine each answer resource record (RR) in turn. */
84 aliases = ares_malloc(alias_alloc * sizeof(char *));
90 for (i = 0; i < (int)ancount; i++)
92 /* Decode the RR up to the data field. */
93 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
94 if (status != ARES_SUCCESS)
97 if (aptr + RRFIXEDSZ > abuf + alen)
100 status = ARES_EBADRESP;
103 rr_type = DNS_RR_TYPE(aptr);
104 rr_class = DNS_RR_CLASS(aptr);
105 rr_len = DNS_RR_LEN(aptr);
107 if (aptr + rr_len > abuf + alen)
110 status = ARES_EBADRESP;
114 if (rr_class == C_IN && rr_type == T_PTR
115 && strcasecmp(rr_name, ptrname) == 0)
117 /* Decode the RR data and set hostname to it. */
118 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
120 if (status != ARES_SUCCESS)
128 rr_data_len = strlen(rr_data)+1;
129 aliases[aliascnt] = ares_malloc(rr_data_len * sizeof(char));
130 if (!aliases[aliascnt])
133 status = ARES_ENOMEM;
136 strncpy(aliases[aliascnt], rr_data, rr_data_len);
138 if (aliascnt >= alias_alloc) {
141 ptr = ares_realloc(aliases, alias_alloc * sizeof(char *));
144 status = ARES_ENOMEM;
151 if (rr_class == C_IN && rr_type == T_CNAME)
153 /* Decode the RR data and replace ptrname with it. */
154 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
156 if (status != ARES_SUCCESS)
167 if (aptr > abuf + alen)
168 { /* LCOV_EXCL_START: already checked above */
169 status = ARES_EBADRESP;
171 } /* LCOV_EXCL_STOP */
174 if (status == ARES_SUCCESS && !hostname)
175 status = ARES_ENODATA;
176 if (status == ARES_SUCCESS)
178 /* We got our answer. Allocate memory to build the host entry. */
179 hostent = ares_malloc(sizeof(struct hostent));
182 hostent->h_addr_list = ares_malloc(2 * sizeof(char *));
183 if (hostent->h_addr_list)
185 hostent->h_addr_list[0] = ares_malloc(addrlen);
186 if (hostent->h_addr_list[0])
188 hostent->h_aliases = ares_malloc((aliascnt+1) * sizeof (char *));
189 if (hostent->h_aliases)
191 /* Fill in the hostent and return successfully. */
192 hostent->h_name = hostname;
193 for (i=0 ; i<aliascnt ; i++)
194 hostent->h_aliases[i] = aliases[i];
195 hostent->h_aliases[aliascnt] = NULL;
196 hostent->h_addrtype = aresx_sitoss(family);
197 hostent->h_length = aresx_sitoss(addrlen);
198 memcpy(hostent->h_addr_list[0], addr, addrlen);
199 hostent->h_addr_list[1] = NULL;
205 ares_free(hostent->h_addr_list[0]);
207 ares_free(hostent->h_addr_list);
211 status = ARES_ENOMEM;
213 for (i=0 ; i<aliascnt ; i++)
215 ares_free(aliases[i]);