quickjs-tart

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

ares_parse_soa_reply.c (3846B)


      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 #include "ares_data.h"
     29 
     30 int ares_parse_soa_reply(const unsigned char *abuf, int alen_int,
     31                          struct ares_soa_reply **soa_out)
     32 {
     33   ares_status_t          status;
     34   size_t                 alen;
     35   struct ares_soa_reply *soa    = NULL;
     36   ares_dns_record_t     *dnsrec = NULL;
     37   size_t                 i;
     38 
     39   *soa_out = NULL;
     40 
     41   if (alen_int < 0) {
     42     return ARES_EBADRESP;
     43   }
     44 
     45   alen = (size_t)alen_int;
     46 
     47   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
     48   if (status != ARES_SUCCESS) {
     49     goto done;
     50   }
     51 
     52   if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
     53     status = ARES_EBADRESP; /* ENODATA might make more sense */
     54     goto done;
     55   }
     56 
     57   for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
     58     const ares_dns_rr_t *rr =
     59       ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
     60 
     61     if (rr == NULL) {
     62       /* Shouldn't be possible */
     63       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
     64       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
     65     }
     66 
     67     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
     68         ares_dns_rr_get_type(rr) != ARES_REC_TYPE_SOA) {
     69       continue;
     70     }
     71 
     72     /* allocate result struct */
     73     soa = ares_malloc_data(ARES_DATATYPE_SOA_REPLY);
     74     if (soa == NULL) {
     75       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
     76       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
     77     }
     78 
     79     soa->serial  = ares_dns_rr_get_u32(rr, ARES_RR_SOA_SERIAL);
     80     soa->refresh = ares_dns_rr_get_u32(rr, ARES_RR_SOA_REFRESH);
     81     soa->retry   = ares_dns_rr_get_u32(rr, ARES_RR_SOA_RETRY);
     82     soa->expire  = ares_dns_rr_get_u32(rr, ARES_RR_SOA_EXPIRE);
     83     soa->minttl  = ares_dns_rr_get_u32(rr, ARES_RR_SOA_MINIMUM);
     84     soa->nsname  = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_SOA_MNAME));
     85     if (soa->nsname == NULL) {
     86       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
     87       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
     88     }
     89     soa->hostmaster = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_SOA_RNAME));
     90     if (soa->hostmaster == NULL) {
     91       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
     92       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
     93     }
     94     break;
     95   }
     96 
     97   if (soa == NULL) {
     98     status = ARES_EBADRESP;
     99   }
    100 
    101 done:
    102   /* clean up on error */
    103   if (status != ARES_SUCCESS) {
    104     ares_free_data(soa);
    105     /* Compatibility */
    106     if (status == ARES_EBADNAME) {
    107       status = ARES_EBADRESP;
    108     }
    109   } else {
    110     /* everything looks fine, return the data */
    111     *soa_out = soa;
    112   }
    113   ares_dns_record_destroy(dnsrec);
    114   return (int)status;
    115 }