aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/c-ares/src/lib/legacy/ares_parse_ptr_reply.c
blob: 0e52f9db09bbc7e8dbf92d56da388164bb22d1de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* MIT License
 *
 * Copyright (c) 2023 Brad House
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * SPDX-License-Identifier: MIT
 */

#include "ares_private.h"

#ifdef HAVE_NETINET_IN_H
#  include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
#  include <netdb.h>
#endif


ares_status_t ares_parse_ptr_reply_dnsrec(const ares_dns_record_t *dnsrec,
                                          const void *addr, int addrlen,
                                          int family, struct hostent **host)
{
  ares_status_t   status;
  size_t          ptrcount = 0;
  struct hostent *hostent  = NULL;
  const char     *hostname = NULL;
  const char     *ptrname  = NULL;
  size_t          i;
  size_t          ancount;

  *host = NULL;

  /* Fetch name from query as we will use it to compare later on.  Old code
   * did this check, so we'll retain it. */
  status = ares_dns_record_query_get(dnsrec, 0, &ptrname, NULL, NULL);
  if (status != ARES_SUCCESS) {
    goto done;
  }

  ancount = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
  if (ancount == 0) {
    status = ARES_ENODATA;
    goto done;
  }

  /* Response structure */
  hostent = ares_malloc(sizeof(*hostent));
  if (hostent == NULL) {
    status = ARES_ENOMEM;
    goto done;
  }

  memset(hostent, 0, sizeof(*hostent));

  hostent->h_addr_list = ares_malloc(2 * sizeof(*hostent->h_addr_list));
  if (hostent->h_addr_list == NULL) {
    status = ARES_ENOMEM;
    goto done;
  }
  memset(hostent->h_addr_list, 0, 2 * sizeof(*hostent->h_addr_list));
  if (addr != NULL && addrlen > 0) {
    hostent->h_addr_list[0] = ares_malloc((size_t)addrlen);
    if (hostent->h_addr_list[0] == NULL) {
      status = ARES_ENOMEM;
      goto done;
    }
    memcpy(hostent->h_addr_list[0], addr, (size_t)addrlen);
  }
  hostent->h_addrtype = (HOSTENT_ADDRTYPE_TYPE)family;
  hostent->h_length   = (HOSTENT_LENGTH_TYPE)addrlen;

  /* Preallocate the maximum number + 1 */
  hostent->h_aliases = ares_malloc((ancount + 1) * sizeof(*hostent->h_aliases));
  if (hostent->h_aliases == NULL) {
    status = ARES_ENOMEM;
    goto done;
  }
  memset(hostent->h_aliases, 0, (ancount + 1) * sizeof(*hostent->h_aliases));


  /* Cycle through answers */
  for (i = 0; i < ancount; i++) {
    const ares_dns_rr_t *rr =
      ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, i);

    if (rr == NULL) {
      /* Shouldn't be possible */
      status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
      goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    }

    if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN) {
      continue;
    }

    /* Any time we see a CNAME, replace our ptrname with its value */
    if (ares_dns_rr_get_type(rr) == ARES_REC_TYPE_CNAME) {
      ptrname = ares_dns_rr_get_str(rr, ARES_RR_CNAME_CNAME);
      if (ptrname == NULL) {
        status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
        goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
      }
    }

    /* Handling for PTR records below this, otherwise skip */
    if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_PTR) {
      continue;
    }

    /* Issue #683
     * Old code compared the name in the rr to the ptrname, but I think this
     * is wrong since it was proven wrong for A & AAAA records.  Leaving
     * this code commented out for future reference
     *
     * rname = ares_dns_rr_get_name(rr);
     * if (rname == NULL) {
     *   status = ARES_EBADRESP;
     *   goto done;
     * }
     * if (!ares_strcaseeq(ptrname, rname)) {
     *   continue;
     * }
     */

    /* Save most recent PTR record as the hostname */
    hostname = ares_dns_rr_get_str(rr, ARES_RR_PTR_DNAME);
    if (hostname == NULL) {
      status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
      goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    }

    /* Append as an alias */
    hostent->h_aliases[ptrcount] = ares_strdup(hostname);
    if (hostent->h_aliases[ptrcount] == NULL) {
      status = ARES_ENOMEM;
      goto done;
    }
    ptrcount++;
  }

  if (ptrcount == 0) {
    status = ARES_ENODATA;
    goto done;
  } else {
    status = ARES_SUCCESS;
  }

  /* Fill in hostname */
  hostent->h_name = ares_strdup(hostname);
  if (hostent->h_name == NULL) {
    status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
    goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
  }

done:
  if (status != ARES_SUCCESS) {
    ares_free_hostent(hostent);
    /* Compatibility */
    if (status == ARES_EBADNAME) {
      status = ARES_EBADRESP;
    }
  } else {
    *host = hostent;
  }
  return status;
}

int ares_parse_ptr_reply(const unsigned char *abuf, int alen_int,
                         const void *addr, int addrlen, int family,
                         struct hostent **host)
{
  size_t             alen;
  ares_dns_record_t *dnsrec = NULL;
  ares_status_t      status;

  if (alen_int < 0) {
    return ARES_EBADRESP;
  }

  alen = (size_t)alen_int;

  status = ares_dns_parse(abuf, alen, 0, &dnsrec);
  if (status != ARES_SUCCESS) {
    goto done;
  }

  status = ares_parse_ptr_reply_dnsrec(dnsrec, addr, addrlen, family, host);

done:
  ares_dns_record_destroy(dnsrec);
  if (status == ARES_EBADNAME) {
    status = ARES_EBADRESP;
  }
  return (int)status;
}