1 /* Copyright 1998 by the Massachusetts Institute of Technology.
3 * Permission to use, copy, modify, and distribute this
4 * software and its documentation for any purpose and without
5 * fee is hereby granted, provided that the above copyright
6 * notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting
8 * documentation, and that the name of M.I.T. not be used in
9 * advertising or publicity pertaining to distribution of the
10 * software without specific, written prior permission.
11 * M.I.T. makes no representations about the suitability of
12 * this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
16 static const char rcsid[] = "$Id: ares_parse_ptr_reply.c,v 1.3 1999/10/23 19:28:14 danw Exp $";
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
27 #include "ares_private.h"
29 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
30 int addrlen, int family, struct hostent **host)
32 unsigned int qdcount, ancount;
33 int status, i, len, rr_type, rr_class, rr_len;
34 const unsigned char *aptr;
35 char *ptrname, *hostname, *rr_name, *rr_data;
36 struct hostent *hostent;
38 /* Set *host to NULL for all failure cases. */
41 /* Give up if abuf doesn't have room for a header. */
45 /* Fetch the question and answer count from the header. */
46 qdcount = DNS_HEADER_QDCOUNT(abuf);
47 ancount = DNS_HEADER_ANCOUNT(abuf);
51 /* Expand the name from the question, and skip past the question. */
52 aptr = abuf + HFIXEDSZ;
53 status = ares_expand_name(aptr, abuf, alen, &ptrname, &len);
54 if (status != ARES_SUCCESS)
56 if (aptr + len + QFIXEDSZ > abuf + alen)
61 aptr += len + QFIXEDSZ;
63 /* Examine each answer resource record (RR) in turn. */
65 for (i = 0; i < ancount; i++)
67 /* Decode the RR up to the data field. */
68 status = ares_expand_name(aptr, abuf, alen, &rr_name, &len);
69 if (status != ARES_SUCCESS)
72 if (aptr + RRFIXEDSZ > abuf + alen)
74 status = ARES_EBADRESP;
77 rr_type = DNS_RR_TYPE(aptr);
78 rr_class = DNS_RR_CLASS(aptr);
79 rr_len = DNS_RR_LEN(aptr);
82 if (rr_class == C_IN && rr_type == T_PTR
83 && strcasecmp(rr_name, ptrname) == 0)
85 /* Decode the RR data and set hostname to it. */
86 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
87 if (status != ARES_SUCCESS)
94 if (rr_class == C_IN && rr_type == T_CNAME)
96 /* Decode the RR data and replace ptrname with it. */
97 status = ares_expand_name(aptr, abuf, alen, &rr_data, &len);
98 if (status != ARES_SUCCESS)
106 if (aptr > abuf + alen)
108 status = ARES_EBADRESP;
113 if (status == ARES_SUCCESS && !hostname)
114 status = ARES_ENODATA;
115 if (status == ARES_SUCCESS)
117 /* We got our answer. Allocate memory to build the host entry. */
118 hostent = malloc(sizeof(struct hostent));
121 hostent->h_addr_list = malloc(2 * sizeof(char *));
122 if (hostent->h_addr_list)
124 hostent->h_addr_list[0] = malloc(addrlen);
125 if (hostent->h_addr_list[0])
127 hostent->h_aliases = malloc(sizeof (char *));
128 if (hostent->h_aliases)
130 /* Fill in the hostent and return successfully. */
131 hostent->h_name = hostname;
132 hostent->h_aliases[0] = NULL;
133 hostent->h_addrtype = family;
134 hostent->h_length = addrlen;
135 memcpy(hostent->h_addr_list[0], addr, addrlen);
136 hostent->h_addr_list[1] = NULL;
141 free(hostent->h_addr_list[0]);
143 free(hostent->h_addr_list);
147 status = ARES_ENOMEM;