Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc-cloned / deps / grpc / third_party / cares / cares / ares_parse_ptr_reply.c
1
2 /* Copyright 1998 by the Massachusetts Institute of Technology.
3  *
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.
15  */
16
17 #include "ares_setup.h"
18
19 #ifdef HAVE_NETINET_IN_H
20 #  include <netinet/in.h>
21 #endif
22 #ifdef HAVE_NETDB_H
23 #  include <netdb.h>
24 #endif
25 #ifdef HAVE_ARPA_NAMESER_H
26 #  include <arpa/nameser.h>
27 #else
28 #  include "nameser.h"
29 #endif
30 #ifdef HAVE_ARPA_NAMESER_COMPAT_H
31 #  include <arpa/nameser_compat.h>
32 #endif
33
34 #ifdef HAVE_STRINGS_H
35 #  include <strings.h>
36 #endif
37
38 #include "ares.h"
39 #include "ares_dns.h"
40 #include "ares_nowarn.h"
41 #include "ares_private.h"
42
43 int ares_parse_ptr_reply(const unsigned char *abuf, int alen, const void *addr,
44                          int addrlen, int family, struct hostent **host)
45 {
46   unsigned int qdcount, ancount;
47   int status, i, rr_type, rr_class, rr_len;
48   long len;
49   const unsigned char *aptr;
50   char *ptrname, *hostname, *rr_name, *rr_data;
51   struct hostent *hostent;
52   int aliascnt = 0;
53   int alias_alloc = 8;
54   char ** aliases;
55   size_t rr_data_len;
56
57   /* Set *host to NULL for all failure cases. */
58   *host = NULL;
59
60   /* Give up if abuf doesn't have room for a header. */
61   if (alen < HFIXEDSZ)
62     return ARES_EBADRESP;
63
64   /* Fetch the question and answer count from the header. */
65   qdcount = DNS_HEADER_QDCOUNT(abuf);
66   ancount = DNS_HEADER_ANCOUNT(abuf);
67   if (qdcount != 1)
68     return ARES_EBADRESP;
69
70   /* Expand the name from the question, and skip past the question. */
71   aptr = abuf + HFIXEDSZ;
72   status = ares__expand_name_for_response(aptr, abuf, alen, &ptrname, &len);
73   if (status != ARES_SUCCESS)
74     return status;
75   if (aptr + len + QFIXEDSZ > abuf + alen)
76     {
77       ares_free(ptrname);
78       return ARES_EBADRESP;
79     }
80   aptr += len + QFIXEDSZ;
81
82   /* Examine each answer resource record (RR) in turn. */
83   hostname = NULL;
84   aliases = ares_malloc(alias_alloc * sizeof(char *));
85   if (!aliases)
86     {
87       ares_free(ptrname);
88       return ARES_ENOMEM;
89     }
90   for (i = 0; i < (int)ancount; i++)
91     {
92       /* Decode the RR up to the data field. */
93       status = ares__expand_name_for_response(aptr, abuf, alen, &rr_name, &len);
94       if (status != ARES_SUCCESS)
95         break;
96       aptr += len;
97       if (aptr + RRFIXEDSZ > abuf + alen)
98         {
99           ares_free(rr_name);
100           status = ARES_EBADRESP;
101           break;
102         }
103       rr_type = DNS_RR_TYPE(aptr);
104       rr_class = DNS_RR_CLASS(aptr);
105       rr_len = DNS_RR_LEN(aptr);
106       aptr += RRFIXEDSZ;
107       if (aptr + rr_len > abuf + alen)
108         {
109           ares_free(rr_name);
110           status = ARES_EBADRESP;
111           break;
112         }
113
114       if (rr_class == C_IN && rr_type == T_PTR
115           && strcasecmp(rr_name, ptrname) == 0)
116         {
117           /* Decode the RR data and set hostname to it. */
118           status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
119                                                   &len);
120           if (status != ARES_SUCCESS)
121             {
122               ares_free(rr_name);
123               break;
124             }
125           if (hostname)
126             ares_free(hostname);
127           hostname = rr_data;
128           rr_data_len = strlen(rr_data)+1;
129           aliases[aliascnt] = ares_malloc(rr_data_len * sizeof(char));
130           if (!aliases[aliascnt])
131             {
132               ares_free(rr_name);
133               status = ARES_ENOMEM;
134               break;
135             }
136           strncpy(aliases[aliascnt], rr_data, rr_data_len);
137           aliascnt++;
138           if (aliascnt >= alias_alloc) {
139             char **ptr;
140             alias_alloc *= 2;
141             ptr = ares_realloc(aliases, alias_alloc * sizeof(char *));
142             if(!ptr) {
143               ares_free(rr_name);
144               status = ARES_ENOMEM;
145               break;
146             }
147             aliases = ptr;
148           }
149         }
150
151       if (rr_class == C_IN && rr_type == T_CNAME)
152         {
153           /* Decode the RR data and replace ptrname with it. */
154           status = ares__expand_name_for_response(aptr, abuf, alen, &rr_data,
155                                                   &len);
156           if (status != ARES_SUCCESS)
157             {
158               ares_free(rr_name);
159               break;
160             }
161           ares_free(ptrname);
162           ptrname = rr_data;
163         }
164
165       ares_free(rr_name);
166       aptr += rr_len;
167       if (aptr > abuf + alen)
168         {  /* LCOV_EXCL_START: already checked above */
169           status = ARES_EBADRESP;
170           break;
171         }  /* LCOV_EXCL_STOP */
172     }
173
174   if (status == ARES_SUCCESS && !hostname)
175     status = ARES_ENODATA;
176   if (status == ARES_SUCCESS)
177     {
178       /* We got our answer.  Allocate memory to build the host entry. */
179       hostent = ares_malloc(sizeof(struct hostent));
180       if (hostent)
181         {
182           hostent->h_addr_list = ares_malloc(2 * sizeof(char *));
183           if (hostent->h_addr_list)
184             {
185               hostent->h_addr_list[0] = ares_malloc(addrlen);
186               if (hostent->h_addr_list[0])
187                 {
188                   hostent->h_aliases = ares_malloc((aliascnt+1) * sizeof (char *));
189                   if (hostent->h_aliases)
190                     {
191                       /* Fill in the hostent and return successfully. */
192                       hostent->h_name = hostname;
193                       for (i=0 ; i<aliascnt ; i++)
194                         hostent->h_aliases[i] = aliases[i];
195                       hostent->h_aliases[aliascnt] = NULL;
196                       hostent->h_addrtype = aresx_sitoss(family);
197                       hostent->h_length = aresx_sitoss(addrlen);
198                       memcpy(hostent->h_addr_list[0], addr, addrlen);
199                       hostent->h_addr_list[1] = NULL;
200                       *host = hostent;
201                       ares_free(aliases);
202                       ares_free(ptrname);
203                       return ARES_SUCCESS;
204                     }
205                   ares_free(hostent->h_addr_list[0]);
206                 }
207               ares_free(hostent->h_addr_list);
208             }
209           ares_free(hostent);
210         }
211       status = ARES_ENOMEM;
212     }
213   for (i=0 ; i<aliascnt ; i++)
214     if (aliases[i])
215       ares_free(aliases[i]);
216   ares_free(aliases);
217   if (hostname)
218     ares_free(hostname);
219   ares_free(ptrname);
220   return status;
221 }