quickjs-tart

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

ares_parse_uri_reply.c (3523B)


      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_uri_reply(const unsigned char *abuf, int alen_int,
     31                          struct ares_uri_reply **uri_out)
     32 {
     33   ares_status_t          status;
     34   size_t                 alen;
     35   struct ares_uri_reply *uri_head = NULL;
     36   struct ares_uri_reply *uri_last = NULL;
     37   struct ares_uri_reply *uri_curr;
     38   ares_dns_record_t     *dnsrec = NULL;
     39   size_t                 i;
     40 
     41   *uri_out = NULL;
     42 
     43   if (alen_int < 0) {
     44     return ARES_EBADRESP;
     45   }
     46 
     47   alen = (size_t)alen_int;
     48 
     49   status = ares_dns_parse(abuf, alen, 0, &dnsrec);
     50   if (status != ARES_SUCCESS) {
     51     goto done;
     52   }
     53 
     54   if (ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER) == 0) {
     55     status = ARES_ENODATA;
     56     goto done;
     57   }
     58 
     59   for (i = 0; i < ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER); i++) {
     60     const ares_dns_rr_t *rr =
     61       ares_dns_record_rr_get(dnsrec, ARES_SECTION_ANSWER, i);
     62 
     63     if (rr == NULL) {
     64       /* Shouldn't be possible */
     65       status = ARES_EBADRESP; /* LCOV_EXCL_LINE: DefensiveCoding */
     66       goto done;              /* LCOV_EXCL_LINE: DefensiveCoding */
     67     }
     68 
     69     if (ares_dns_rr_get_class(rr) != ARES_CLASS_IN ||
     70         ares_dns_rr_get_type(rr) != ARES_REC_TYPE_URI) {
     71       continue;
     72     }
     73 
     74     /* Allocate storage for this URI answer appending it to the list */
     75     uri_curr = ares_malloc_data(ARES_DATATYPE_URI_REPLY);
     76     if (uri_curr == NULL) {
     77       status = ARES_ENOMEM; /* LCOV_EXCL_LINE: OutOfMemory */
     78       goto done;            /* LCOV_EXCL_LINE: OutOfMemory */
     79     }
     80 
     81     /* Link in the record */
     82     if (uri_last) {
     83       uri_last->next = uri_curr;
     84     } else {
     85       uri_head = uri_curr;
     86     }
     87     uri_last = uri_curr;
     88 
     89 
     90     uri_curr->priority = ares_dns_rr_get_u16(rr, ARES_RR_URI_PRIORITY);
     91     uri_curr->weight   = ares_dns_rr_get_u16(rr, ARES_RR_URI_WEIGHT);
     92     uri_curr->uri = ares_strdup(ares_dns_rr_get_str(rr, ARES_RR_URI_TARGET));
     93     uri_curr->ttl = (int)ares_dns_rr_get_ttl(rr);
     94 
     95     if (uri_curr->uri == NULL) {
     96       status = ARES_ENOMEM;
     97       goto done;
     98     }
     99   }
    100 
    101 done:
    102   /* clean up on error */
    103   if (status != ARES_SUCCESS) {
    104     if (uri_head) {
    105       ares_free_data(uri_head);
    106     }
    107   } else {
    108     /* everything looks fine, return the data */
    109     *uri_out = uri_head;
    110   }
    111   ares_dns_record_destroy(dnsrec);
    112   return (int)status;
    113 }