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_SYS_SOCKET_H
20 # include <sys/socket.h>
22 #ifdef HAVE_NETINET_IN_H
23 # include <netinet/in.h>
28 #ifdef HAVE_ARPA_NAMESER_H
29 # include <arpa/nameser.h>
33 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
34 # include <arpa/nameser_compat.h>
45 #include "ares_nowarn.h"
46 #include "ares_private.h"
48 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
49 int addrlen, int family, struct hostent **host)
51 unsigned int qdcount, ancount;
52 int status, i, rr_type, rr_class, rr_len;
54 const unsigned char *aptr;
55 char *ptrname, *hostname, *rr_name, *rr_data;
56 struct hostent *hostent;
61 /* Set *host to NULL for all failure cases. */
64 /* Give up if abuf doesn't have room for a header. */
68 /* Fetch the question and answer count from the header. */
69 qdcount = DNS_HEADER_QDCOUNT(abuf);
70 ancount = DNS_HEADER_ANCOUNT(abuf);
74 /* Expand the name from the question, and skip past the question. */
75 aptr = abuf + HFIXEDSZ;
76 status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
77 if (status != ARES_SUCCESS)
79 if (aptr + len + QFIXEDSZ > abuf + alen)
84 aptr += len + QFIXEDSZ;
86 /* Examine each answer resource record (RR) in turn. */
88 aliases = malloc(alias_alloc * sizeof(char *));
94 for (i = 0; i < (int)ancount; i++)
96 /* Decode the RR up to the data field. */
97 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
98 if (status != ARES_SUCCESS)
101 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_PTR
113 && strcasecmp(rr_name, ptrname) == 0)
115 /* Decode the RR data and set hostname to it. */
116 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
118 if (status != ARES_SUCCESS)
126 aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char));
127 if (!aliases[aliascnt])
130 status = ARES_ENOMEM;
133 strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
135 if (aliascnt >= alias_alloc) {
138 ptr = realloc(aliases, alias_alloc * sizeof(char *));
141 status = ARES_ENOMEM;
148 if (rr_class == C_IN && rr_type == T_CNAME)
150 /* Decode the RR data and replace ptrname with it. */
151 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
153 if (status != ARES_SUCCESS)
164 if (aptr > abuf + alen)
166 status = ARES_EBADRESP;
171 if (status == ARES_SUCCESS && !hostname)
172 status = ARES_ENODATA;
173 if (status == ARES_SUCCESS)
175 /* We got our answer. Allocate memory to build the host entry. */
176 hostent = malloc(sizeof(struct hostent));
179 hostent->h_addr_list = malloc(2 * sizeof(char *));
180 if (hostent->h_addr_list)
182 hostent->h_addr_list[0] = malloc(addrlen);
183 if (hostent->h_addr_list[0])
185 hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
186 if (hostent->h_aliases)
188 /* Fill in the hostent and return successfully. */
189 hostent->h_name = hostname;
190 for (i=0 ; i<aliascnt ; i++)
191 hostent->h_aliases[i] = aliases[i];
192 hostent->h_aliases[aliascnt] = NULL;
193 hostent->h_addrtype = aresx_sitoss(family);
194 hostent->h_length = aresx_sitoss(addrlen);
195 memcpy(hostent->h_addr_list[0], addr, addrlen);
196 hostent->h_addr_list[1] = NULL;
202 free(hostent->h_addr_list[0]);
204 free(hostent->h_addr_list);
208 status = ARES_ENOMEM;
210 for (i=0 ; i<aliascnt ; i++)