quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

ares_parse_ptr_reply.c (6338B)


      1 /* MIT License
      2  *
      3  * Copyright (c) 2023 Brad House
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies of the Software, and to permit persons to whom the Software is
     10  * furnished to do so, subject to the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the next
     13  * paragraph) shall be included in all copies or substantial portions of the
     14  * Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  * SOFTWARE.
     23  *
     24  * SPDX-License-Identifier: MIT
     25  */
     26 
     27 #include "ares_private.h"
     28 
     29 #ifdef HAVE_NETINET_IN_H
     30 #  include <netinet/in.h>
     31 #endif
     32 #ifdef HAVE_NETDB_H
     33 #  include <netdb.h>
     34 #endif
     35 
     36 
     37 ares_status_t ares_parse_ptr_reply_dnsrec(const ares_dns_record_t *dnsrec,
     38                                           const void *addr, int addrlen,
     39                                           int family, struct hostent **host)
     40 {
     41   ares_status_t   status;
     42   size_t          ptrcount = 0;
     43   struct hostent *hostent  = NULL;
     44   const char     *hostname = NULL;
     45   const char     *ptrname  = NULL;
     46   size_t          i;
     47   size_t          ancount;
     48 
     49   *host = NULL;
     50 
     51   /* Fetch name from query as we will use it to compare later on.  Old code
     52    * did this check, so we'll retain it. */
     53   status = ares_dns_record_query_get(dnsrec, 0, &ptrname, NULL, NULL);
     54   if (status != ARES_SUCCESS) {
     55     goto done;
     56   }
     57 
     58   ancount = ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER);
     59   if (ancount == 0) {
     60     status = ARES_ENODATA;
     61     goto done;
     62   }
     63 
     64   /* Response structure */
     65   hostent = ares_malloc(sizeof(*hostent));
     66   if (hostent == NULL) {
     67     status = ARES_ENOMEM;
     68     goto done;
     69   }
     70 
     71   memset(hostent, 0, sizeof(*hostent));
     72 
     73   hostent->h_addr_list = ares_malloc(2 * sizeof(*hostent->h_addr_list));
     74   if (hostent->h_addr_list == NULL) {
     75     status = ARES_ENOMEM;
     76     goto done;
     77   }
     78   memset(hostent->h_addr_list, 0, 2 * sizeof(*hostent->h_addr_list));
     79   if (addr != NULL && addrlen > 0) {
     80     hostent->h_addr_list[0] = ares_malloc((size_t)addrlen);
     81     if (hostent->h_addr_list[0] == NULL) {
     82       status = ARES_ENOMEM;
     83       goto done;
     84     }
     85     memcpy(hostent->h_addr_list[0], addr, (size_t)addrlen);
     86   }
     87   hostent->h_addrtype = (HOSTENT_ADDRTYPE_TYPE)family;
     88   hostent->h_length   = (HOSTENT_LENGTH_TYPE)addrlen;
     89 
     90   /* Preallocate the maximum number + 1 */
     91   hostent->h_aliases = ares_malloc((ancount + 1) * sizeof(*hostent->h_aliases));
     92   if (hostent->h_aliases == NULL) {
     93     status = ARES_ENOMEM;
     94     goto done;
     95   }
     96   memset(hostent->h_aliases, 0, (ancount + 1) * sizeof(*hostent->h_aliases));
     97 
     98 
     99   /* Cycle through answers */
    100   for (i = 0; i < ancount; i++) {
    101     const ares_dns_rr_t *rr =
    102       ares_dns_record_rr_get_const(dnsrec, ARES_SECTION_ANSWER, i);
    103 
    104     if (rr == NULL) {
    105       /* Shouldn't be possible */
    106       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
    107       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    108     }
    109 
    110     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN) {
    111       continue;
    112     }
    113 
    114     /* Any time we see a CNAME, replace our ptrname with its value */
    115     if (ares_dns_rr_get_type(rr) == ARES_REC_TYPE_CNAME) {
    116       ptrname = ares_dns_rr_get_str(rr, ARES_RR_CNAME_CNAME);
    117       if (ptrname == NULL) {
    118         status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
    119         goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    120       }
    121     }
    122 
    123     /* Handling for PTR records below this, otherwise skip */
    124     if (ares_dns_rr_get_type(rr) != ARES_REC_TYPE_PTR) {
    125       continue;
    126     }
    127 
    128     /* Issue #683
    129      * Old code compared the name in the rr to the ptrname, but I think this
    130      * is wrong since it was proven wrong for A & AAAA records.  Leaving
    131      * this code commented out for future reference
    132      *
    133      * rname = ares_dns_rr_get_name(rr);
    134      * if (rname == NULL) {
    135      *   status = ARES_EBADRESP;
    136      *   goto done;
    137      * }
    138      * if (!ares_strcaseeq(ptrname, rname)) {
    139      *   continue;
    140      * }
    141      */
    142 
    143     /* Save most recent PTR record as the hostname */
    144     hostname = ares_dns_rr_get_str(rr, ARES_RR_PTR_DNAME);
    145     if (hostname == NULL) {
    146       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
    147       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
    148     }
    149 
    150     /* Append as an alias */
    151     hostent->h_aliases[ptrcount] = ares_strdup(hostname);
    152     if (hostent->h_aliases[ptrcount] == NULL) {
    153       status = ARES_ENOMEM;
    154       goto done;
    155     }
    156     ptrcount++;
    157   }
    158 
    159   if (ptrcount == 0) {
    160     status = ARES_ENODATA;
    161     goto done;
    162   } else {
    163     status = ARES_SUCCESS;
    164   }
    165 
    166   /* Fill in hostname */
    167   hostent->h_name = ares_strdup(hostname);
    168   if (hostent->h_name == NULL) {
    169     status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
    170     goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
    171   }
    172 
    173 done:
    174   if (status != ARES_SUCCESS) {
    175     ares_free_hostent(hostent);
    176     /* Compatibility */
    177     if (status == ARES_EBADNAME) {
    178       status = ARES_EBADRESP;
    179     }
    180   } else {
    181     *host = hostent;
    182   }
    183   return status;
    184 }
    185 
    186 int ares_parse_ptr_reply(const unsigned char *abuf, int alen_int,
    187                          const void *addr, int addrlen, int family,
    188                          struct hostent **host)
    189 {
    190   size_t             alen;
    191   ares_dns_record_t *dnsrec = NULL;
    192   ares_status_t      status;
    193 
    194   if (alen_int < 0) {
    195     return ARES_EBADRESP;
    196   }
    197 
    198   alen = (size_t)alen_int;
    199 
    200   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
    201   if (status != ARES_SUCCESS) {
    202     goto done;
    203   }
    204 
    205   status = ares_parse_ptr_reply_dnsrec(dnsrec, addr, addrlen, family, host);
    206 
    207 done:
    208   ares_dns_record_destroy(dnsrec);
    209   if (status == ARES_EBADNAME) {
    210     status = ARES_EBADRESP;
    211   }
    212   return (int)status;
    213 }