quickjs-tart

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

ares_create_query.c (2702B)


      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 static int ares_create_query_int(const char *name, int dnsclass, int type,
     30                                  unsigned short id, int rd,
     31                                  unsigned char **bufp, int *buflenp,
     32                                  int max_udp_size)
     33 {
     34   ares_status_t      status;
     35   ares_dns_record_t *dnsrec = NULL;
     36   size_t             len;
     37   ares_dns_flags_t   rd_flag = rd ? ARES_FLAG_RD : 0;
     38 
     39   if (name == NULL || bufp == NULL || buflenp == NULL) {
     40     status = ARES_EFORMERR;
     41     goto done;
     42   }
     43 
     44   *bufp    = NULL;
     45   *buflenp = 0;
     46 
     47   status = ares_dns_record_create_query(
     48     &dnsrec, name, (ares_dns_class_t)dnsclass, (ares_dns_rec_type_t)type, id,
     49     rd_flag, (size_t)max_udp_size);
     50   if (status != ARES_SUCCESS) {
     51     goto done;
     52   }
     53 
     54   status = ares_dns_write(dnsrec, bufp, &len);
     55   if (status != ARES_SUCCESS) {
     56     goto done;
     57   }
     58 
     59   *buflenp = (int)len;
     60 
     61 done:
     62   ares_dns_record_destroy(dnsrec);
     63   return (int)status;
     64 }
     65 
     66 int ares_create_query(const char *name, int dnsclass, int type,
     67                       unsigned short id, int rd, unsigned char **bufp,
     68                       int *buflenp, int max_udp_size)
     69 {
     70   return ares_create_query_int(name, dnsclass, type, id, rd, bufp, buflenp,
     71                                max_udp_size);
     72 }
     73 
     74 int ares_mkquery(const char *name, int dnsclass, int type, unsigned short id,
     75                  int rd, unsigned char **buf, int *buflen)
     76 {
     77   return ares_create_query_int(name, dnsclass, type, id, rd, buf, buflen, 0);
     78 }