1 /* Copyright 1998 by the Massachusetts Institute of Technology.
2  *
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.
14  */
15 
16 static const char rcsid[] = "$Id: ares_parse_ptr_reply.c,v 1.3 1999/10/23 19:28:14 danw Exp $";
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/nameser.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <netdb.h>
25 #include "ares.h"
26 #include "ares_dns.h"
27 #include "ares_private.h"
28 
ares_parse_ptr_reply(const unsigned char * abuf,int alen,const void * addr,int addrlen,int family,struct hostent ** host)29 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
30 			 int addrlen, int family, struct hostent **host)
31 {
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;
37 
38   /* Set *host to NULL for all failure cases. */
39   *host = NULL;
40 
41   /* Give up if abuf doesn't have room for a header. */
42   if (alen < HFIXEDSZ)
43     return ARES_EBADRESP;
44 
45   /* Fetch the question and answer count from the header. */
46   qdcount = DNS_HEADER_QDCOUNT(abuf);
47   ancount = DNS_HEADER_ANCOUNT(abuf);
48   if (qdcount != 1)
49     return ARES_EBADRESP;
50 
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)
55     return status;
56   if (aptr + len + QFIXEDSZ > abuf + alen)
57     {
58       free(ptrname);
59       return ARES_EBADRESP;
60     }
61   aptr += len + QFIXEDSZ;
62 
63   /* Examine each answer resource record (RR) in turn. */
64   hostname = NULL;
65   for (i = 0; i < ancount; i++)
66     {
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)
70 	break;
71       aptr += len;
72       if (aptr + RRFIXEDSZ > abuf + alen)
73 	{
74 	  status = ARES_EBADRESP;
75 	  break;
76 	}
77       rr_type = DNS_RR_TYPE(aptr);
78       rr_class = DNS_RR_CLASS(aptr);
79       rr_len = DNS_RR_LEN(aptr);
80       aptr += RRFIXEDSZ;
81 
82       if (rr_class == C_IN && rr_type == T_PTR
83 	  && strcasecmp(rr_name, ptrname) == 0)
84 	{
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)
88 	    break;
89 	  if (hostname)
90 	    free(hostname);
91 	  hostname = rr_data;
92 	}
93 
94       if (rr_class == C_IN && rr_type == T_CNAME)
95 	{
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)
99 	    break;
100 	  free(ptrname);
101 	  ptrname = rr_data;
102 	}
103 
104       free(rr_name);
105       aptr += rr_len;
106       if (aptr > abuf + alen)
107 	{
108 	  status = ARES_EBADRESP;
109 	  break;
110 	}
111     }
112 
113   if (status == ARES_SUCCESS && !hostname)
114     status = ARES_ENODATA;
115   if (status == ARES_SUCCESS)
116     {
117       /* We got our answer.  Allocate memory to build the host entry. */
118       hostent = malloc(sizeof(struct hostent));
119       if (hostent)
120 	{
121 	  hostent->h_addr_list = malloc(2 * sizeof(char *));
122 	  if (hostent->h_addr_list)
123 	    {
124 	      hostent->h_addr_list[0] = malloc(addrlen);
125 	      if (hostent->h_addr_list[0])
126 		{
127 		  hostent->h_aliases = malloc(sizeof (char *));
128 		  if (hostent->h_aliases)
129 		    {
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;
137 		      *host = hostent;
138 		      free(ptrname);
139 		      return ARES_SUCCESS;
140 		    }
141 		  free(hostent->h_addr_list[0]);
142 		}
143 	      free(hostent->h_addr_list);
144 	    }
145 	  free(hostent);
146 	}
147       status = ARES_ENOMEM;
148     }
149   if (hostname)
150     free(hostname);
151   free(ptrname);
152   return status;
153 }
154