3 /* Copyright 1998 by the Massachusetts Institute of Technology.
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for any purpose and without
7 * fee is hereby granted, provided that the above copyright
8 * notice appear in all copies and that both that copyright
9 * notice and this permission notice appear in supporting
10 * documentation, and that the name of M.I.T. not be used in
11 * advertising or publicity pertaining to distribution of the
12 * software without specific, written prior permission.
13 * M.I.T. makes no representations about the suitability of
14 * this software for any purpose. It is provided "as is"
15 * without express or implied warranty.
20 #if defined(WIN32) && !defined(WATT32)
23 #include <sys/socket.h>
24 #include <netinet/in.h>
26 #include <arpa/nameser.h>
27 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
28 #include <arpa/nameser_compat.h>
39 #include "ares_private.h"
41 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
42 int addrlen, int family, struct hostent **host)
44 unsigned int qdcount, ancount;
45 int status, i, rr_type, rr_class, rr_len;
47 const unsigned char *aptr;
48 char *ptrname, *hostname, *rr_name, *rr_data;
49 struct hostent *hostent;
53 /* Set *host to NULL for all failure cases. */
56 /* Give up if abuf doesn't have room for a header. */
60 /* Fetch the question and answer count from the header. */
61 qdcount = DNS_HEADER_QDCOUNT(abuf);
62 ancount = DNS_HEADER_ANCOUNT(abuf);
66 /* Expand the name from the question, and skip past the question. */
67 aptr = abuf + HFIXEDSZ;
68 status = ares_expand_name(aptr, abuf, alen, &ptrname, &len);
69 if (status != ARES_SUCCESS)
71 if (aptr + len + QFIXEDSZ > abuf + alen)
76 aptr += len + QFIXEDSZ;
78 /* Examine each answer resource record (RR) in turn. */
80 aliases = (char **) malloc(8 * sizeof(char *));
86 for (i = 0; i < (int)ancount; i++)
88 /* Decode the RR up to the data field. */
89 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
90 if (status != ARES_SUCCESS)
93 if (aptr + RRFIXEDSZ > abuf + alen)
95 status = ARES_EBADRESP;
98 rr_type = DNS_RR_TYPE(aptr);
99 rr_class = DNS_RR_CLASS(aptr);
100 rr_len = DNS_RR_LEN(aptr);
103 if (rr_class == C_IN && rr_type == T_PTR
104 && strcasecmp(rr_name, ptrname) == 0)
106 /* Decode the RR data and set hostname to it. */
107 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
108 if (status != ARES_SUCCESS)
113 aliases[aliascnt] = malloc((strlen(rr_data)+1) * sizeof(char *));
114 if (!aliases[aliascnt])
116 status = ARES_ENOMEM;
119 strncpy(aliases[aliascnt], rr_data, strlen(rr_data)+1);
122 aliases = (char **) realloc(aliases, (aliascnt/16+1) * sizeof(char *));
125 if (rr_class == C_IN && rr_type == T_CNAME)
127 /* Decode the RR data and replace ptrname with it. */
128 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
129 if (status != ARES_SUCCESS)
137 if (aptr > abuf + alen)
139 status = ARES_EBADRESP;
144 if (status == ARES_SUCCESS && !hostname)
145 status = ARES_ENODATA;
146 if (status == ARES_SUCCESS)
148 /* We got our answer. Allocate memory to build the host entry. */
149 hostent = malloc(sizeof(struct hostent));
152 hostent->h_addr_list = malloc(2 * sizeof(char *));
153 if (hostent->h_addr_list)
155 hostent->h_addr_list[0] = malloc(addrlen);
156 if (hostent->h_addr_list[0])
158 hostent->h_aliases = malloc((aliascnt+1) * sizeof (char *));
159 if (hostent->h_aliases)
161 /* Fill in the hostent and return successfully. */
162 hostent->h_name = hostname;
163 for (i=0 ; i<aliascnt ; i++)
164 hostent->h_aliases[i] = aliases[i];
165 hostent->h_aliases[aliascnt] = NULL;
166 hostent->h_addrtype = family;
167 hostent->h_length = addrlen;
168 memcpy(hostent->h_addr_list[0], addr, addrlen);
169 hostent->h_addr_list[1] = NULL;
175 free(hostent->h_addr_list[0]);
177 free(hostent->h_addr_list);
181 status = ARES_ENOMEM;
183 for (i=0 ; i<aliascnt ; i++)