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);
107 if (rr_class == C_IN && rr_type == T_PTR
108 && strcasecmp(rr_name, ptrname) == 0)
110 /* Decode the RR data and set hostname to it. */
111 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
113 if (status != ARES_SUCCESS)
121 aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char));
122 if (!aliases[aliascnt])
125 status = ARES_ENOMEM;
128 strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
130 if (aliascnt >= alias_alloc) {
133 ptr = realloc(aliases, alias_alloc * sizeof(char *));
136 status = ARES_ENOMEM;
143 if (rr_class == C_IN && rr_type == T_CNAME)
145 /* Decode the RR data and replace ptrname with it. */
146 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
148 if (status != ARES_SUCCESS)
159 if (aptr > abuf + alen)
161 status = ARES_EBADRESP;
166 if (status == ARES_SUCCESS && !hostname)
167 status = ARES_ENODATA;
168 if (status == ARES_SUCCESS)
170 /* We got our answer. Allocate memory to build the host entry. */
171 hostent = malloc(sizeof(struct hostent));
174 hostent->h_addr_list = malloc(2 * sizeof(char *));
175 if (hostent->h_addr_list)
177 hostent->h_addr_list[0] = malloc(addrlen);
178 if (hostent->h_addr_list[0])
180 hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
181 if (hostent->h_aliases)
183 /* Fill in the hostent and return successfully. */
184 hostent->h_name = hostname;
185 for (i=0 ; i<aliascnt ; i++)
186 hostent->h_aliases[i] = aliases[i];
187 hostent->h_aliases[aliascnt] = NULL;
188 hostent->h_addrtype = aresx_sitoss(family);
189 hostent->h_length = aresx_sitoss(addrlen);
190 memcpy(hostent->h_addr_list[0], addr, addrlen);
191 hostent->h_addr_list[1] = NULL;
197 free(hostent->h_addr_list[0]);
199 free(hostent->h_addr_list);
203 status = ARES_ENOMEM;
205 for (i=0 ; i<aliascnt ; i++)