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_private.h"
47 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
48 int addrlen, int family, struct hostent **host)
50 unsigned int qdcount, ancount;
51 int status, i, rr_type, rr_class, rr_len;
53 const unsigned char *aptr;
54 char *ptrname, *hostname, *rr_name, *rr_data;
55 struct hostent *hostent;
60 /* Set *host to NULL for all failure cases. */
63 /* Give up if abuf doesn't have room for a header. */
67 /* Fetch the question and answer count from the header. */
68 qdcount = DNS_HEADER_QDCOUNT(abuf);
69 ancount = DNS_HEADER_ANCOUNT(abuf);
73 /* Expand the name from the question, and skip past the question. */
74 aptr = abuf + HFIXEDSZ;
75 status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
76 if (status != ARES_SUCCESS)
78 if (aptr + len + QFIXEDSZ > abuf + alen)
83 aptr += len + QFIXEDSZ;
85 /* Examine each answer resource record (RR) in turn. */
87 aliases = malloc(alias_alloc * sizeof(char *));
93 for (i = 0; i < (int)ancount; i++)
95 /* Decode the RR up to the data field. */
96 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
97 if (status != ARES_SUCCESS)
100 if (aptr + RRFIXEDSZ > abuf + alen)
102 status = ARES_EBADRESP;
105 rr_type = DNS_RR_TYPE(aptr);
106 rr_class = DNS_RR_CLASS(aptr);
107 rr_len = DNS_RR_LEN(aptr);
110 if (rr_class == C_IN && rr_type == T_PTR
111 && strcasecmp(rr_name, ptrname) == 0)
113 /* Decode the RR data and set hostname to it. */
114 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
116 if (status != ARES_SUCCESS)
121 aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char *));
122 if (!aliases[aliascnt])
124 status = ARES_ENOMEM;
127 strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
129 if (aliascnt >= alias_alloc) {
132 ptr = realloc(aliases, alias_alloc * sizeof(char *));
134 status = ARES_ENOMEM;
141 if (rr_class == C_IN && rr_type == T_CNAME)
143 /* Decode the RR data and replace ptrname with it. */
144 status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
146 if (status != ARES_SUCCESS)
154 if (aptr > abuf + alen)
156 status = ARES_EBADRESP;
161 if (status == ARES_SUCCESS && !hostname)
162 status = ARES_ENODATA;
163 if (status == ARES_SUCCESS)
165 /* We got our answer. Allocate memory to build the host entry. */
166 hostent = malloc(sizeof(struct hostent));
169 hostent->h_addr_list = malloc(2 * sizeof(char *));
170 if (hostent->h_addr_list)
172 hostent->h_addr_list[0] = malloc(addrlen);
173 if (hostent->h_addr_list[0])
175 hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
176 if (hostent->h_aliases)
178 /* Fill in the hostent and return successfully. */
179 hostent->h_name = hostname;
180 for (i=0 ; i<aliascnt ; i++)
181 hostent->h_aliases[i] = aliases[i];
182 hostent->h_aliases[aliascnt] = NULL;
183 hostent->h_addrtype = family;
184 hostent->h_length = addrlen;
185 memcpy(hostent->h_addr_list[0], addr, addrlen);
186 hostent->h_addr_list[1] = NULL;
192 free(hostent->h_addr_list[0]);
194 free(hostent->h_addr_list);
198 status = ARES_ENOMEM;
200 for (i=0 ; i<aliascnt ; i++)