11cb0ef41Sopenharmony_ci/* MIT License
21cb0ef41Sopenharmony_ci *
31cb0ef41Sopenharmony_ci * Copyright (c) 2023 Brad House
41cb0ef41Sopenharmony_ci *
51cb0ef41Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
61cb0ef41Sopenharmony_ci * of this software and associated documentation files (the "Software"), to deal
71cb0ef41Sopenharmony_ci * in the Software without restriction, including without limitation the rights
81cb0ef41Sopenharmony_ci * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
91cb0ef41Sopenharmony_ci * copies of the Software, and to permit persons to whom the Software is
101cb0ef41Sopenharmony_ci * furnished to do so, subject to the following conditions:
111cb0ef41Sopenharmony_ci *
121cb0ef41Sopenharmony_ci * The above copyright notice and this permission notice (including the next
131cb0ef41Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
141cb0ef41Sopenharmony_ci * Software.
151cb0ef41Sopenharmony_ci *
161cb0ef41Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171cb0ef41Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181cb0ef41Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
191cb0ef41Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201cb0ef41Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211cb0ef41Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
221cb0ef41Sopenharmony_ci * SOFTWARE.
231cb0ef41Sopenharmony_ci *
241cb0ef41Sopenharmony_ci * SPDX-License-Identifier: MIT
251cb0ef41Sopenharmony_ci */
261cb0ef41Sopenharmony_ci#include "ares_setup.h"
271cb0ef41Sopenharmony_ci#include "ares.h"
281cb0ef41Sopenharmony_ci#include "ares_private.h"
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciares_bool_t ares_dns_opcode_isvalid(ares_dns_opcode_t opcode)
311cb0ef41Sopenharmony_ci{
321cb0ef41Sopenharmony_ci  switch (opcode) {
331cb0ef41Sopenharmony_ci    case ARES_OPCODE_QUERY:
341cb0ef41Sopenharmony_ci    case ARES_OPCODE_IQUERY:
351cb0ef41Sopenharmony_ci    case ARES_OPCODE_STATUS:
361cb0ef41Sopenharmony_ci    case ARES_OPCODE_NOTIFY:
371cb0ef41Sopenharmony_ci    case ARES_OPCODE_UPDATE:
381cb0ef41Sopenharmony_ci      return ARES_TRUE;
391cb0ef41Sopenharmony_ci  }
401cb0ef41Sopenharmony_ci  return ARES_FALSE;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ciares_bool_t ares_dns_rcode_isvalid(ares_dns_rcode_t rcode)
441cb0ef41Sopenharmony_ci{
451cb0ef41Sopenharmony_ci  switch (rcode) {
461cb0ef41Sopenharmony_ci    case ARES_RCODE_NOERROR:
471cb0ef41Sopenharmony_ci    case ARES_RCODE_FORMERR:
481cb0ef41Sopenharmony_ci    case ARES_RCODE_SERVFAIL:
491cb0ef41Sopenharmony_ci    case ARES_RCODE_NXDOMAIN:
501cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTIMP:
511cb0ef41Sopenharmony_ci    case ARES_RCODE_REFUSED:
521cb0ef41Sopenharmony_ci    case ARES_RCODE_YXDOMAIN:
531cb0ef41Sopenharmony_ci    case ARES_RCODE_YXRRSET:
541cb0ef41Sopenharmony_ci    case ARES_RCODE_NXRRSET:
551cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTAUTH:
561cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTZONE:
571cb0ef41Sopenharmony_ci    case ARES_RCODE_DSOTYPEI:
581cb0ef41Sopenharmony_ci    case ARES_RCODE_BADSIG:
591cb0ef41Sopenharmony_ci    case ARES_RCODE_BADKEY:
601cb0ef41Sopenharmony_ci    case ARES_RCODE_BADTIME:
611cb0ef41Sopenharmony_ci    case ARES_RCODE_BADMODE:
621cb0ef41Sopenharmony_ci    case ARES_RCODE_BADNAME:
631cb0ef41Sopenharmony_ci    case ARES_RCODE_BADALG:
641cb0ef41Sopenharmony_ci    case ARES_RCODE_BADTRUNC:
651cb0ef41Sopenharmony_ci    case ARES_RCODE_BADCOOKIE:
661cb0ef41Sopenharmony_ci      return ARES_TRUE;
671cb0ef41Sopenharmony_ci  }
681cb0ef41Sopenharmony_ci  return ARES_FALSE;
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciares_bool_t ares_dns_flags_arevalid(unsigned short flags)
721cb0ef41Sopenharmony_ci{
731cb0ef41Sopenharmony_ci  unsigned short allflags = ARES_FLAG_QR | ARES_FLAG_AA | ARES_FLAG_TC |
741cb0ef41Sopenharmony_ci                            ARES_FLAG_RD | ARES_FLAG_RA | ARES_FLAG_AD |
751cb0ef41Sopenharmony_ci                            ARES_FLAG_CD;
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  if (flags & ~allflags) {
781cb0ef41Sopenharmony_ci    return ARES_FALSE;
791cb0ef41Sopenharmony_ci  }
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  return ARES_TRUE;
821cb0ef41Sopenharmony_ci}
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciares_bool_t ares_dns_rec_type_isvalid(ares_dns_rec_type_t type,
851cb0ef41Sopenharmony_ci                                      ares_bool_t         is_query)
861cb0ef41Sopenharmony_ci{
871cb0ef41Sopenharmony_ci  switch (type) {
881cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_A:
891cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NS:
901cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CNAME:
911cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SOA:
921cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_PTR:
931cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HINFO:
941cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_MX:
951cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TXT:
961cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_AAAA:
971cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SRV:
981cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NAPTR:
991cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_OPT:
1001cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TLSA:
1011cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SVCB:
1021cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HTTPS:
1031cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_ANY:
1041cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_URI:
1051cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CAA:
1061cb0ef41Sopenharmony_ci      return ARES_TRUE;
1071cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_RAW_RR:
1081cb0ef41Sopenharmony_ci      return is_query ? ARES_FALSE : ARES_TRUE;
1091cb0ef41Sopenharmony_ci    default:
1101cb0ef41Sopenharmony_ci      break;
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci  return is_query ? ARES_TRUE : ARES_FALSE;
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ciares_bool_t ares_dns_rec_type_allow_name_compression(ares_dns_rec_type_t type)
1161cb0ef41Sopenharmony_ci{
1171cb0ef41Sopenharmony_ci  /* Only record types defined in RFC1035 allow name compression within the
1181cb0ef41Sopenharmony_ci   * RDATA.  Otherwise nameservers that don't understand an RR may not be
1191cb0ef41Sopenharmony_ci   * able to pass along the RR in a proper manner */
1201cb0ef41Sopenharmony_ci  switch (type) {
1211cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_A:
1221cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NS:
1231cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CNAME:
1241cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SOA:
1251cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_PTR:
1261cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HINFO:
1271cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_MX:
1281cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TXT:
1291cb0ef41Sopenharmony_ci      return ARES_TRUE;
1301cb0ef41Sopenharmony_ci    default:
1311cb0ef41Sopenharmony_ci      break;
1321cb0ef41Sopenharmony_ci  }
1331cb0ef41Sopenharmony_ci  return ARES_FALSE;
1341cb0ef41Sopenharmony_ci}
1351cb0ef41Sopenharmony_ci
1361cb0ef41Sopenharmony_ciares_bool_t ares_dns_class_isvalid(ares_dns_class_t qclass,
1371cb0ef41Sopenharmony_ci                                   ares_bool_t      is_query)
1381cb0ef41Sopenharmony_ci{
1391cb0ef41Sopenharmony_ci  switch (qclass) {
1401cb0ef41Sopenharmony_ci    case ARES_CLASS_IN:
1411cb0ef41Sopenharmony_ci    case ARES_CLASS_CHAOS:
1421cb0ef41Sopenharmony_ci    case ARES_CLASS_HESOID:
1431cb0ef41Sopenharmony_ci    case ARES_CLASS_NONE:
1441cb0ef41Sopenharmony_ci      return ARES_TRUE;
1451cb0ef41Sopenharmony_ci    case ARES_CLASS_ANY:
1461cb0ef41Sopenharmony_ci      return is_query ? ARES_TRUE : ARES_FALSE;
1471cb0ef41Sopenharmony_ci  }
1481cb0ef41Sopenharmony_ci  return ARES_FALSE;
1491cb0ef41Sopenharmony_ci}
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ciares_bool_t ares_dns_section_isvalid(ares_dns_section_t sect)
1521cb0ef41Sopenharmony_ci{
1531cb0ef41Sopenharmony_ci  switch (sect) {
1541cb0ef41Sopenharmony_ci    case ARES_SECTION_ANSWER:
1551cb0ef41Sopenharmony_ci    case ARES_SECTION_AUTHORITY:
1561cb0ef41Sopenharmony_ci    case ARES_SECTION_ADDITIONAL:
1571cb0ef41Sopenharmony_ci      return ARES_TRUE;
1581cb0ef41Sopenharmony_ci  }
1591cb0ef41Sopenharmony_ci  return ARES_FALSE;
1601cb0ef41Sopenharmony_ci}
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ciares_dns_rec_type_t ares_dns_rr_key_to_rec_type(ares_dns_rr_key_t key)
1631cb0ef41Sopenharmony_ci{
1641cb0ef41Sopenharmony_ci  /* NOTE: due to the way we've numerated the keys, we can simply divide by
1651cb0ef41Sopenharmony_ci   *       100 to get the type rather than having to do a huge switch
1661cb0ef41Sopenharmony_ci   *       statement.  That said, we do then validate the type returned is
1671cb0ef41Sopenharmony_ci   *       valid in case something completely bogus is passed in */
1681cb0ef41Sopenharmony_ci  ares_dns_rec_type_t type = key / 100;
1691cb0ef41Sopenharmony_ci  if (!ares_dns_rec_type_isvalid(type, ARES_FALSE)) {
1701cb0ef41Sopenharmony_ci    return 0;
1711cb0ef41Sopenharmony_ci  }
1721cb0ef41Sopenharmony_ci  return type;
1731cb0ef41Sopenharmony_ci}
1741cb0ef41Sopenharmony_ci
1751cb0ef41Sopenharmony_ciconst char *ares_dns_rec_type_tostr(ares_dns_rec_type_t type)
1761cb0ef41Sopenharmony_ci{
1771cb0ef41Sopenharmony_ci  switch (type) {
1781cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_A:
1791cb0ef41Sopenharmony_ci      return "A";
1801cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NS:
1811cb0ef41Sopenharmony_ci      return "NS";
1821cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CNAME:
1831cb0ef41Sopenharmony_ci      return "CNAME";
1841cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SOA:
1851cb0ef41Sopenharmony_ci      return "SOA";
1861cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_PTR:
1871cb0ef41Sopenharmony_ci      return "PTR";
1881cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HINFO:
1891cb0ef41Sopenharmony_ci      return "HINFO";
1901cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_MX:
1911cb0ef41Sopenharmony_ci      return "MX";
1921cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TXT:
1931cb0ef41Sopenharmony_ci      return "TXT";
1941cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_AAAA:
1951cb0ef41Sopenharmony_ci      return "AAAA";
1961cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SRV:
1971cb0ef41Sopenharmony_ci      return "SRV";
1981cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NAPTR:
1991cb0ef41Sopenharmony_ci      return "NAPTR";
2001cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_OPT:
2011cb0ef41Sopenharmony_ci      return "OPT";
2021cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TLSA:
2031cb0ef41Sopenharmony_ci      return "TLSA";
2041cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SVCB:
2051cb0ef41Sopenharmony_ci      return "SVCB";
2061cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HTTPS:
2071cb0ef41Sopenharmony_ci      return "HTTPS";
2081cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_ANY:
2091cb0ef41Sopenharmony_ci      return "ANY";
2101cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_URI:
2111cb0ef41Sopenharmony_ci      return "URI";
2121cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CAA:
2131cb0ef41Sopenharmony_ci      return "CAA";
2141cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_RAW_RR:
2151cb0ef41Sopenharmony_ci      return "RAWRR";
2161cb0ef41Sopenharmony_ci  }
2171cb0ef41Sopenharmony_ci  return "UNKNOWN";
2181cb0ef41Sopenharmony_ci}
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ciconst char *ares_dns_class_tostr(ares_dns_class_t qclass)
2211cb0ef41Sopenharmony_ci{
2221cb0ef41Sopenharmony_ci  switch (qclass) {
2231cb0ef41Sopenharmony_ci    case ARES_CLASS_IN:
2241cb0ef41Sopenharmony_ci      return "IN";
2251cb0ef41Sopenharmony_ci    case ARES_CLASS_CHAOS:
2261cb0ef41Sopenharmony_ci      return "CH";
2271cb0ef41Sopenharmony_ci    case ARES_CLASS_HESOID:
2281cb0ef41Sopenharmony_ci      return "HS";
2291cb0ef41Sopenharmony_ci    case ARES_CLASS_ANY:
2301cb0ef41Sopenharmony_ci      return "ANY";
2311cb0ef41Sopenharmony_ci    case ARES_CLASS_NONE:
2321cb0ef41Sopenharmony_ci      return "NONE";
2331cb0ef41Sopenharmony_ci  }
2341cb0ef41Sopenharmony_ci  return "UNKNOWN";
2351cb0ef41Sopenharmony_ci}
2361cb0ef41Sopenharmony_ci
2371cb0ef41Sopenharmony_ciconst char *ares_dns_opcode_tostr(ares_dns_opcode_t opcode)
2381cb0ef41Sopenharmony_ci{
2391cb0ef41Sopenharmony_ci  switch (opcode) {
2401cb0ef41Sopenharmony_ci    case ARES_OPCODE_QUERY:
2411cb0ef41Sopenharmony_ci      return "QUERY";
2421cb0ef41Sopenharmony_ci    case ARES_OPCODE_IQUERY:
2431cb0ef41Sopenharmony_ci      return "IQUERY";
2441cb0ef41Sopenharmony_ci    case ARES_OPCODE_STATUS:
2451cb0ef41Sopenharmony_ci      return "STATUS";
2461cb0ef41Sopenharmony_ci    case ARES_OPCODE_NOTIFY:
2471cb0ef41Sopenharmony_ci      return "NOTIFY";
2481cb0ef41Sopenharmony_ci    case ARES_OPCODE_UPDATE:
2491cb0ef41Sopenharmony_ci      return "UPDATE";
2501cb0ef41Sopenharmony_ci  }
2511cb0ef41Sopenharmony_ci  return "UNKNOWN";
2521cb0ef41Sopenharmony_ci}
2531cb0ef41Sopenharmony_ci
2541cb0ef41Sopenharmony_ciconst char *ares_dns_rr_key_tostr(ares_dns_rr_key_t key)
2551cb0ef41Sopenharmony_ci{
2561cb0ef41Sopenharmony_ci  switch (key) {
2571cb0ef41Sopenharmony_ci    case ARES_RR_A_ADDR:
2581cb0ef41Sopenharmony_ci      return "ADDR";
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ci    case ARES_RR_NS_NSDNAME:
2611cb0ef41Sopenharmony_ci      return "NSDNAME";
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_ci    case ARES_RR_CNAME_CNAME:
2641cb0ef41Sopenharmony_ci      return "CNAME";
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ci    case ARES_RR_SOA_MNAME:
2671cb0ef41Sopenharmony_ci      return "MNAME";
2681cb0ef41Sopenharmony_ci
2691cb0ef41Sopenharmony_ci    case ARES_RR_SOA_RNAME:
2701cb0ef41Sopenharmony_ci      return "RNAME";
2711cb0ef41Sopenharmony_ci
2721cb0ef41Sopenharmony_ci    case ARES_RR_SOA_SERIAL:
2731cb0ef41Sopenharmony_ci      return "SERIAL";
2741cb0ef41Sopenharmony_ci
2751cb0ef41Sopenharmony_ci    case ARES_RR_SOA_REFRESH:
2761cb0ef41Sopenharmony_ci      return "REFRESH";
2771cb0ef41Sopenharmony_ci
2781cb0ef41Sopenharmony_ci    case ARES_RR_SOA_RETRY:
2791cb0ef41Sopenharmony_ci      return "RETRY";
2801cb0ef41Sopenharmony_ci
2811cb0ef41Sopenharmony_ci    case ARES_RR_SOA_EXPIRE:
2821cb0ef41Sopenharmony_ci      return "EXPIRE";
2831cb0ef41Sopenharmony_ci
2841cb0ef41Sopenharmony_ci    case ARES_RR_SOA_MINIMUM:
2851cb0ef41Sopenharmony_ci      return "MINIMUM";
2861cb0ef41Sopenharmony_ci
2871cb0ef41Sopenharmony_ci    case ARES_RR_PTR_DNAME:
2881cb0ef41Sopenharmony_ci      return "DNAME";
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci    case ARES_RR_AAAA_ADDR:
2911cb0ef41Sopenharmony_ci      return "ADDR";
2921cb0ef41Sopenharmony_ci
2931cb0ef41Sopenharmony_ci    case ARES_RR_HINFO_CPU:
2941cb0ef41Sopenharmony_ci      return "CPU";
2951cb0ef41Sopenharmony_ci
2961cb0ef41Sopenharmony_ci    case ARES_RR_HINFO_OS:
2971cb0ef41Sopenharmony_ci      return "OS";
2981cb0ef41Sopenharmony_ci
2991cb0ef41Sopenharmony_ci    case ARES_RR_MX_PREFERENCE:
3001cb0ef41Sopenharmony_ci      return "PREFERENCE";
3011cb0ef41Sopenharmony_ci
3021cb0ef41Sopenharmony_ci    case ARES_RR_MX_EXCHANGE:
3031cb0ef41Sopenharmony_ci      return "EXCHANGE";
3041cb0ef41Sopenharmony_ci
3051cb0ef41Sopenharmony_ci    case ARES_RR_TXT_DATA:
3061cb0ef41Sopenharmony_ci      return "DATA";
3071cb0ef41Sopenharmony_ci
3081cb0ef41Sopenharmony_ci    case ARES_RR_SRV_PRIORITY:
3091cb0ef41Sopenharmony_ci      return "PRIORITY";
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci    case ARES_RR_SRV_WEIGHT:
3121cb0ef41Sopenharmony_ci      return "WEIGHT";
3131cb0ef41Sopenharmony_ci
3141cb0ef41Sopenharmony_ci    case ARES_RR_SRV_PORT:
3151cb0ef41Sopenharmony_ci      return "PORT";
3161cb0ef41Sopenharmony_ci
3171cb0ef41Sopenharmony_ci    case ARES_RR_SRV_TARGET:
3181cb0ef41Sopenharmony_ci      return "TARGET";
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_ORDER:
3211cb0ef41Sopenharmony_ci      return "ORDER";
3221cb0ef41Sopenharmony_ci
3231cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_PREFERENCE:
3241cb0ef41Sopenharmony_ci      return "PREFERENCE";
3251cb0ef41Sopenharmony_ci
3261cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_FLAGS:
3271cb0ef41Sopenharmony_ci      return "FLAGS";
3281cb0ef41Sopenharmony_ci
3291cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_SERVICES:
3301cb0ef41Sopenharmony_ci      return "SERVICES";
3311cb0ef41Sopenharmony_ci
3321cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_REGEXP:
3331cb0ef41Sopenharmony_ci      return "REGEXP";
3341cb0ef41Sopenharmony_ci
3351cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_REPLACEMENT:
3361cb0ef41Sopenharmony_ci      return "REPLACEMENT";
3371cb0ef41Sopenharmony_ci
3381cb0ef41Sopenharmony_ci    case ARES_RR_OPT_UDP_SIZE:
3391cb0ef41Sopenharmony_ci      return "UDP_SIZE";
3401cb0ef41Sopenharmony_ci
3411cb0ef41Sopenharmony_ci    case ARES_RR_OPT_VERSION:
3421cb0ef41Sopenharmony_ci      return "VERSION";
3431cb0ef41Sopenharmony_ci
3441cb0ef41Sopenharmony_ci    case ARES_RR_OPT_FLAGS:
3451cb0ef41Sopenharmony_ci      return "FLAGS";
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci    case ARES_RR_OPT_OPTIONS:
3481cb0ef41Sopenharmony_ci      return "OPTIONS";
3491cb0ef41Sopenharmony_ci
3501cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_CERT_USAGE:
3511cb0ef41Sopenharmony_ci      return "CERT_USAGE";
3521cb0ef41Sopenharmony_ci
3531cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_SELECTOR:
3541cb0ef41Sopenharmony_ci      return "SELECTOR";
3551cb0ef41Sopenharmony_ci
3561cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_MATCH:
3571cb0ef41Sopenharmony_ci      return "MATCH";
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_DATA:
3601cb0ef41Sopenharmony_ci      return "DATA";
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PRIORITY:
3631cb0ef41Sopenharmony_ci      return "PRIORITY";
3641cb0ef41Sopenharmony_ci
3651cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_TARGET:
3661cb0ef41Sopenharmony_ci      return "TARGET";
3671cb0ef41Sopenharmony_ci
3681cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PARAMS:
3691cb0ef41Sopenharmony_ci      return "PARAMS";
3701cb0ef41Sopenharmony_ci
3711cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PRIORITY:
3721cb0ef41Sopenharmony_ci      return "PRIORITY";
3731cb0ef41Sopenharmony_ci
3741cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_TARGET:
3751cb0ef41Sopenharmony_ci      return "TARGET";
3761cb0ef41Sopenharmony_ci
3771cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PARAMS:
3781cb0ef41Sopenharmony_ci      return "PARAMS";
3791cb0ef41Sopenharmony_ci
3801cb0ef41Sopenharmony_ci    case ARES_RR_URI_PRIORITY:
3811cb0ef41Sopenharmony_ci      return "PRIORITY";
3821cb0ef41Sopenharmony_ci
3831cb0ef41Sopenharmony_ci    case ARES_RR_URI_WEIGHT:
3841cb0ef41Sopenharmony_ci      return "WEIGHT";
3851cb0ef41Sopenharmony_ci
3861cb0ef41Sopenharmony_ci    case ARES_RR_URI_TARGET:
3871cb0ef41Sopenharmony_ci      return "TARGET";
3881cb0ef41Sopenharmony_ci
3891cb0ef41Sopenharmony_ci    case ARES_RR_CAA_CRITICAL:
3901cb0ef41Sopenharmony_ci      return "CRITICAL";
3911cb0ef41Sopenharmony_ci
3921cb0ef41Sopenharmony_ci    case ARES_RR_CAA_TAG:
3931cb0ef41Sopenharmony_ci      return "TAG";
3941cb0ef41Sopenharmony_ci
3951cb0ef41Sopenharmony_ci    case ARES_RR_CAA_VALUE:
3961cb0ef41Sopenharmony_ci      return "VALUE";
3971cb0ef41Sopenharmony_ci
3981cb0ef41Sopenharmony_ci    case ARES_RR_RAW_RR_TYPE:
3991cb0ef41Sopenharmony_ci      return "TYPE";
4001cb0ef41Sopenharmony_ci
4011cb0ef41Sopenharmony_ci    case ARES_RR_RAW_RR_DATA:
4021cb0ef41Sopenharmony_ci      return "DATA";
4031cb0ef41Sopenharmony_ci  }
4041cb0ef41Sopenharmony_ci
4051cb0ef41Sopenharmony_ci  return "UNKNOWN";
4061cb0ef41Sopenharmony_ci}
4071cb0ef41Sopenharmony_ci
4081cb0ef41Sopenharmony_ciares_dns_datatype_t ares_dns_rr_key_datatype(ares_dns_rr_key_t key)
4091cb0ef41Sopenharmony_ci{
4101cb0ef41Sopenharmony_ci  switch (key) {
4111cb0ef41Sopenharmony_ci    case ARES_RR_A_ADDR:
4121cb0ef41Sopenharmony_ci      return ARES_DATATYPE_INADDR;
4131cb0ef41Sopenharmony_ci
4141cb0ef41Sopenharmony_ci    case ARES_RR_AAAA_ADDR:
4151cb0ef41Sopenharmony_ci      return ARES_DATATYPE_INADDR6;
4161cb0ef41Sopenharmony_ci
4171cb0ef41Sopenharmony_ci    case ARES_RR_NS_NSDNAME:
4181cb0ef41Sopenharmony_ci    case ARES_RR_CNAME_CNAME:
4191cb0ef41Sopenharmony_ci    case ARES_RR_SOA_MNAME:
4201cb0ef41Sopenharmony_ci    case ARES_RR_SOA_RNAME:
4211cb0ef41Sopenharmony_ci    case ARES_RR_PTR_DNAME:
4221cb0ef41Sopenharmony_ci    case ARES_RR_MX_EXCHANGE:
4231cb0ef41Sopenharmony_ci    case ARES_RR_SRV_TARGET:
4241cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_TARGET:
4251cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_TARGET:
4261cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_REPLACEMENT:
4271cb0ef41Sopenharmony_ci    case ARES_RR_URI_TARGET:
4281cb0ef41Sopenharmony_ci      return ARES_DATATYPE_NAME;
4291cb0ef41Sopenharmony_ci
4301cb0ef41Sopenharmony_ci    case ARES_RR_HINFO_CPU:
4311cb0ef41Sopenharmony_ci    case ARES_RR_HINFO_OS:
4321cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_FLAGS:
4331cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_SERVICES:
4341cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_REGEXP:
4351cb0ef41Sopenharmony_ci    case ARES_RR_CAA_TAG:
4361cb0ef41Sopenharmony_ci      return ARES_DATATYPE_STR;
4371cb0ef41Sopenharmony_ci
4381cb0ef41Sopenharmony_ci    case ARES_RR_SOA_SERIAL:
4391cb0ef41Sopenharmony_ci    case ARES_RR_SOA_REFRESH:
4401cb0ef41Sopenharmony_ci    case ARES_RR_SOA_RETRY:
4411cb0ef41Sopenharmony_ci    case ARES_RR_SOA_EXPIRE:
4421cb0ef41Sopenharmony_ci    case ARES_RR_SOA_MINIMUM:
4431cb0ef41Sopenharmony_ci      return ARES_DATATYPE_U32;
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_ci    case ARES_RR_MX_PREFERENCE:
4461cb0ef41Sopenharmony_ci    case ARES_RR_SRV_PRIORITY:
4471cb0ef41Sopenharmony_ci    case ARES_RR_SRV_WEIGHT:
4481cb0ef41Sopenharmony_ci    case ARES_RR_SRV_PORT:
4491cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_ORDER:
4501cb0ef41Sopenharmony_ci    case ARES_RR_NAPTR_PREFERENCE:
4511cb0ef41Sopenharmony_ci    case ARES_RR_OPT_UDP_SIZE:
4521cb0ef41Sopenharmony_ci    case ARES_RR_OPT_FLAGS:
4531cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PRIORITY:
4541cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PRIORITY:
4551cb0ef41Sopenharmony_ci    case ARES_RR_URI_PRIORITY:
4561cb0ef41Sopenharmony_ci    case ARES_RR_URI_WEIGHT:
4571cb0ef41Sopenharmony_ci    case ARES_RR_RAW_RR_TYPE:
4581cb0ef41Sopenharmony_ci      return ARES_DATATYPE_U16;
4591cb0ef41Sopenharmony_ci
4601cb0ef41Sopenharmony_ci    case ARES_RR_OPT_VERSION:
4611cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_CERT_USAGE:
4621cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_SELECTOR:
4631cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_MATCH:
4641cb0ef41Sopenharmony_ci    case ARES_RR_CAA_CRITICAL:
4651cb0ef41Sopenharmony_ci      return ARES_DATATYPE_U8;
4661cb0ef41Sopenharmony_ci
4671cb0ef41Sopenharmony_ci    case ARES_RR_CAA_VALUE:
4681cb0ef41Sopenharmony_ci    case ARES_RR_TXT_DATA:
4691cb0ef41Sopenharmony_ci      return ARES_DATATYPE_BINP;
4701cb0ef41Sopenharmony_ci
4711cb0ef41Sopenharmony_ci    case ARES_RR_TLSA_DATA:
4721cb0ef41Sopenharmony_ci    case ARES_RR_RAW_RR_DATA:
4731cb0ef41Sopenharmony_ci      return ARES_DATATYPE_BIN;
4741cb0ef41Sopenharmony_ci
4751cb0ef41Sopenharmony_ci    case ARES_RR_OPT_OPTIONS:
4761cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PARAMS:
4771cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PARAMS:
4781cb0ef41Sopenharmony_ci      return ARES_DATATYPE_OPT;
4791cb0ef41Sopenharmony_ci  }
4801cb0ef41Sopenharmony_ci
4811cb0ef41Sopenharmony_ci  return 0;
4821cb0ef41Sopenharmony_ci}
4831cb0ef41Sopenharmony_ci
4841cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_a_keys[]     = { ARES_RR_A_ADDR };
4851cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_ns_keys[]    = { ARES_RR_NS_NSDNAME };
4861cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_cname_keys[] = { ARES_RR_CNAME_CNAME };
4871cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_soa_keys[]   = {
4881cb0ef41Sopenharmony_ci  ARES_RR_SOA_MNAME,   ARES_RR_SOA_RNAME, ARES_RR_SOA_SERIAL,
4891cb0ef41Sopenharmony_ci  ARES_RR_SOA_REFRESH, ARES_RR_SOA_RETRY, ARES_RR_SOA_EXPIRE,
4901cb0ef41Sopenharmony_ci  ARES_RR_SOA_MINIMUM
4911cb0ef41Sopenharmony_ci};
4921cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_ptr_keys[]   = { ARES_RR_PTR_DNAME };
4931cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_hinfo_keys[] = { ARES_RR_HINFO_CPU,
4941cb0ef41Sopenharmony_ci                                                   ARES_RR_HINFO_OS };
4951cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_mx_keys[]    = { ARES_RR_MX_PREFERENCE,
4961cb0ef41Sopenharmony_ci                                                   ARES_RR_MX_EXCHANGE };
4971cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_txt_keys[]   = { ARES_RR_TXT_DATA };
4981cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_aaaa_keys[]  = { ARES_RR_AAAA_ADDR };
4991cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_srv_keys[]   = {
5001cb0ef41Sopenharmony_ci  ARES_RR_SRV_PRIORITY, ARES_RR_SRV_WEIGHT, ARES_RR_SRV_PORT, ARES_RR_SRV_TARGET
5011cb0ef41Sopenharmony_ci};
5021cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_naptr_keys[] = {
5031cb0ef41Sopenharmony_ci  ARES_RR_NAPTR_ORDER,    ARES_RR_NAPTR_PREFERENCE, ARES_RR_NAPTR_FLAGS,
5041cb0ef41Sopenharmony_ci  ARES_RR_NAPTR_SERVICES, ARES_RR_NAPTR_REGEXP,     ARES_RR_NAPTR_REPLACEMENT
5051cb0ef41Sopenharmony_ci};
5061cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_opt_keys[]    = { ARES_RR_OPT_UDP_SIZE,
5071cb0ef41Sopenharmony_ci                                                    ARES_RR_OPT_VERSION,
5081cb0ef41Sopenharmony_ci                                                    ARES_RR_OPT_FLAGS,
5091cb0ef41Sopenharmony_ci                                                    ARES_RR_OPT_OPTIONS };
5101cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_tlsa_keys[]   = { ARES_RR_TLSA_CERT_USAGE,
5111cb0ef41Sopenharmony_ci                                                    ARES_RR_TLSA_SELECTOR,
5121cb0ef41Sopenharmony_ci                                                    ARES_RR_TLSA_MATCH,
5131cb0ef41Sopenharmony_ci                                                    ARES_RR_TLSA_DATA };
5141cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_svcb_keys[]   = { ARES_RR_SVCB_PRIORITY,
5151cb0ef41Sopenharmony_ci                                                    ARES_RR_SVCB_TARGET,
5161cb0ef41Sopenharmony_ci                                                    ARES_RR_SVCB_PARAMS };
5171cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_https_keys[]  = { ARES_RR_HTTPS_PRIORITY,
5181cb0ef41Sopenharmony_ci                                                    ARES_RR_HTTPS_TARGET,
5191cb0ef41Sopenharmony_ci                                                    ARES_RR_HTTPS_PARAMS };
5201cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_uri_keys[]    = { ARES_RR_URI_PRIORITY,
5211cb0ef41Sopenharmony_ci                                                    ARES_RR_URI_WEIGHT,
5221cb0ef41Sopenharmony_ci                                                    ARES_RR_URI_TARGET };
5231cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_caa_keys[]    = { ARES_RR_CAA_CRITICAL,
5241cb0ef41Sopenharmony_ci                                                    ARES_RR_CAA_TAG,
5251cb0ef41Sopenharmony_ci                                                    ARES_RR_CAA_VALUE };
5261cb0ef41Sopenharmony_cistatic const ares_dns_rr_key_t rr_raw_rr_keys[] = { ARES_RR_RAW_RR_TYPE,
5271cb0ef41Sopenharmony_ci                                                    ARES_RR_RAW_RR_DATA };
5281cb0ef41Sopenharmony_ci
5291cb0ef41Sopenharmony_ciconst ares_dns_rr_key_t       *ares_dns_rr_get_keys(ares_dns_rec_type_t type,
5301cb0ef41Sopenharmony_ci                                                    size_t             *cnt)
5311cb0ef41Sopenharmony_ci{
5321cb0ef41Sopenharmony_ci  if (cnt == NULL) {
5331cb0ef41Sopenharmony_ci    return NULL;
5341cb0ef41Sopenharmony_ci  }
5351cb0ef41Sopenharmony_ci
5361cb0ef41Sopenharmony_ci  *cnt = 0;
5371cb0ef41Sopenharmony_ci
5381cb0ef41Sopenharmony_ci  switch (type) {
5391cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_A:
5401cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_a_keys) / sizeof(*rr_a_keys);
5411cb0ef41Sopenharmony_ci      return rr_a_keys;
5421cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NS:
5431cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_ns_keys) / sizeof(*rr_ns_keys);
5441cb0ef41Sopenharmony_ci      return rr_ns_keys;
5451cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CNAME:
5461cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_cname_keys) / sizeof(*rr_cname_keys);
5471cb0ef41Sopenharmony_ci      return rr_cname_keys;
5481cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SOA:
5491cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_soa_keys) / sizeof(*rr_soa_keys);
5501cb0ef41Sopenharmony_ci      return rr_soa_keys;
5511cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_PTR:
5521cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_ptr_keys) / sizeof(*rr_ptr_keys);
5531cb0ef41Sopenharmony_ci      return rr_ptr_keys;
5541cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HINFO:
5551cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_hinfo_keys) / sizeof(*rr_hinfo_keys);
5561cb0ef41Sopenharmony_ci      return rr_hinfo_keys;
5571cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_MX:
5581cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_mx_keys) / sizeof(*rr_mx_keys);
5591cb0ef41Sopenharmony_ci      return rr_mx_keys;
5601cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TXT:
5611cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_txt_keys) / sizeof(*rr_txt_keys);
5621cb0ef41Sopenharmony_ci      return rr_txt_keys;
5631cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_AAAA:
5641cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_aaaa_keys) / sizeof(*rr_aaaa_keys);
5651cb0ef41Sopenharmony_ci      return rr_aaaa_keys;
5661cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SRV:
5671cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_srv_keys) / sizeof(*rr_srv_keys);
5681cb0ef41Sopenharmony_ci      return rr_srv_keys;
5691cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_NAPTR:
5701cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_naptr_keys) / sizeof(*rr_naptr_keys);
5711cb0ef41Sopenharmony_ci      return rr_naptr_keys;
5721cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_OPT:
5731cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_opt_keys) / sizeof(*rr_opt_keys);
5741cb0ef41Sopenharmony_ci      return rr_opt_keys;
5751cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_TLSA:
5761cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_tlsa_keys) / sizeof(*rr_tlsa_keys);
5771cb0ef41Sopenharmony_ci      return rr_tlsa_keys;
5781cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_SVCB:
5791cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_svcb_keys) / sizeof(*rr_svcb_keys);
5801cb0ef41Sopenharmony_ci      return rr_svcb_keys;
5811cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_HTTPS:
5821cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_https_keys) / sizeof(*rr_https_keys);
5831cb0ef41Sopenharmony_ci      return rr_https_keys;
5841cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_ANY:
5851cb0ef41Sopenharmony_ci      /* Not real */
5861cb0ef41Sopenharmony_ci      break;
5871cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_URI:
5881cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_uri_keys) / sizeof(*rr_uri_keys);
5891cb0ef41Sopenharmony_ci      return rr_uri_keys;
5901cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_CAA:
5911cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_caa_keys) / sizeof(*rr_caa_keys);
5921cb0ef41Sopenharmony_ci      return rr_caa_keys;
5931cb0ef41Sopenharmony_ci    case ARES_REC_TYPE_RAW_RR:
5941cb0ef41Sopenharmony_ci      *cnt = sizeof(rr_raw_rr_keys) / sizeof(*rr_raw_rr_keys);
5951cb0ef41Sopenharmony_ci      return rr_raw_rr_keys;
5961cb0ef41Sopenharmony_ci  }
5971cb0ef41Sopenharmony_ci
5981cb0ef41Sopenharmony_ci  return NULL;
5991cb0ef41Sopenharmony_ci}
6001cb0ef41Sopenharmony_ci
6011cb0ef41Sopenharmony_ciares_bool_t ares_dns_class_fromstr(ares_dns_class_t *qclass, const char *str)
6021cb0ef41Sopenharmony_ci{
6031cb0ef41Sopenharmony_ci  size_t i;
6041cb0ef41Sopenharmony_ci
6051cb0ef41Sopenharmony_ci  static const struct {
6061cb0ef41Sopenharmony_ci    const char      *name;
6071cb0ef41Sopenharmony_ci    ares_dns_class_t qclass;
6081cb0ef41Sopenharmony_ci  } list[] = {
6091cb0ef41Sopenharmony_ci    {"IN",    ARES_CLASS_IN    },
6101cb0ef41Sopenharmony_ci    { "CH",   ARES_CLASS_CHAOS },
6111cb0ef41Sopenharmony_ci    { "HS",   ARES_CLASS_HESOID},
6121cb0ef41Sopenharmony_ci    { "NONE", ARES_CLASS_NONE  },
6131cb0ef41Sopenharmony_ci    { "ANY",  ARES_CLASS_ANY   },
6141cb0ef41Sopenharmony_ci    { NULL,   0                }
6151cb0ef41Sopenharmony_ci  };
6161cb0ef41Sopenharmony_ci
6171cb0ef41Sopenharmony_ci  if (qclass == NULL || str == NULL) {
6181cb0ef41Sopenharmony_ci    return ARES_FALSE;
6191cb0ef41Sopenharmony_ci  }
6201cb0ef41Sopenharmony_ci
6211cb0ef41Sopenharmony_ci  for (i = 0; list[i].name != NULL; i++) {
6221cb0ef41Sopenharmony_ci    if (strcasecmp(list[i].name, str) == 0) {
6231cb0ef41Sopenharmony_ci      *qclass = list[i].qclass;
6241cb0ef41Sopenharmony_ci      return ARES_TRUE;
6251cb0ef41Sopenharmony_ci    }
6261cb0ef41Sopenharmony_ci  }
6271cb0ef41Sopenharmony_ci  return ARES_FALSE;
6281cb0ef41Sopenharmony_ci}
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ciares_bool_t ares_dns_rec_type_fromstr(ares_dns_rec_type_t *qtype,
6311cb0ef41Sopenharmony_ci                                      const char          *str)
6321cb0ef41Sopenharmony_ci{
6331cb0ef41Sopenharmony_ci  size_t i;
6341cb0ef41Sopenharmony_ci
6351cb0ef41Sopenharmony_ci  static const struct {
6361cb0ef41Sopenharmony_ci    const char         *name;
6371cb0ef41Sopenharmony_ci    ares_dns_rec_type_t type;
6381cb0ef41Sopenharmony_ci  } list[] = {
6391cb0ef41Sopenharmony_ci    {"A",       ARES_REC_TYPE_A     },
6401cb0ef41Sopenharmony_ci    { "NS",     ARES_REC_TYPE_NS    },
6411cb0ef41Sopenharmony_ci    { "CNAME",  ARES_REC_TYPE_CNAME },
6421cb0ef41Sopenharmony_ci    { "SOA",    ARES_REC_TYPE_SOA   },
6431cb0ef41Sopenharmony_ci    { "PTR",    ARES_REC_TYPE_PTR   },
6441cb0ef41Sopenharmony_ci    { "HINFO",  ARES_REC_TYPE_HINFO },
6451cb0ef41Sopenharmony_ci    { "MX",     ARES_REC_TYPE_MX    },
6461cb0ef41Sopenharmony_ci    { "TXT",    ARES_REC_TYPE_TXT   },
6471cb0ef41Sopenharmony_ci    { "AAAA",   ARES_REC_TYPE_AAAA  },
6481cb0ef41Sopenharmony_ci    { "SRV",    ARES_REC_TYPE_SRV   },
6491cb0ef41Sopenharmony_ci    { "NAPTR",  ARES_REC_TYPE_NAPTR },
6501cb0ef41Sopenharmony_ci    { "OPT",    ARES_REC_TYPE_OPT   },
6511cb0ef41Sopenharmony_ci    { "TLSA",   ARES_REC_TYPE_TLSA  },
6521cb0ef41Sopenharmony_ci    { "SVCB",   ARES_REC_TYPE_SVCB  },
6531cb0ef41Sopenharmony_ci    { "HTTPS",  ARES_REC_TYPE_HTTPS },
6541cb0ef41Sopenharmony_ci    { "ANY",    ARES_REC_TYPE_ANY   },
6551cb0ef41Sopenharmony_ci    { "URI",    ARES_REC_TYPE_URI   },
6561cb0ef41Sopenharmony_ci    { "CAA",    ARES_REC_TYPE_CAA   },
6571cb0ef41Sopenharmony_ci    { "RAW_RR", ARES_REC_TYPE_RAW_RR},
6581cb0ef41Sopenharmony_ci    { NULL,     0                   }
6591cb0ef41Sopenharmony_ci  };
6601cb0ef41Sopenharmony_ci
6611cb0ef41Sopenharmony_ci  if (qtype == NULL || str == NULL) {
6621cb0ef41Sopenharmony_ci    return ARES_FALSE;
6631cb0ef41Sopenharmony_ci  }
6641cb0ef41Sopenharmony_ci
6651cb0ef41Sopenharmony_ci  for (i = 0; list[i].name != NULL; i++) {
6661cb0ef41Sopenharmony_ci    if (strcasecmp(list[i].name, str) == 0) {
6671cb0ef41Sopenharmony_ci      *qtype = list[i].type;
6681cb0ef41Sopenharmony_ci      return ARES_TRUE;
6691cb0ef41Sopenharmony_ci    }
6701cb0ef41Sopenharmony_ci  }
6711cb0ef41Sopenharmony_ci  return ARES_FALSE;
6721cb0ef41Sopenharmony_ci}
6731cb0ef41Sopenharmony_ci
6741cb0ef41Sopenharmony_ciconst char *ares_dns_section_tostr(ares_dns_section_t section)
6751cb0ef41Sopenharmony_ci{
6761cb0ef41Sopenharmony_ci  switch (section) {
6771cb0ef41Sopenharmony_ci    case ARES_SECTION_ANSWER:
6781cb0ef41Sopenharmony_ci      return "ANSWER";
6791cb0ef41Sopenharmony_ci    case ARES_SECTION_AUTHORITY:
6801cb0ef41Sopenharmony_ci      return "AUTHORITY";
6811cb0ef41Sopenharmony_ci    case ARES_SECTION_ADDITIONAL:
6821cb0ef41Sopenharmony_ci      return "ADDITIONAL";
6831cb0ef41Sopenharmony_ci  }
6841cb0ef41Sopenharmony_ci  return "UNKNOWN";
6851cb0ef41Sopenharmony_ci}
6861cb0ef41Sopenharmony_ci
6871cb0ef41Sopenharmony_cistatic ares_dns_opt_datatype_t ares_dns_opt_get_type_opt(unsigned short opt)
6881cb0ef41Sopenharmony_ci{
6891cb0ef41Sopenharmony_ci  ares_opt_param_t param = (ares_opt_param_t)opt;
6901cb0ef41Sopenharmony_ci  switch (param) {
6911cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_LLQ:
6921cb0ef41Sopenharmony_ci      /* Really it is u16 version, u16 opcode, u16 error, u64 id, u32 lease */
6931cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
6941cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_UL:
6951cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U32;
6961cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_NSID:
6971cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
6981cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_DAU:
6991cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U8_LIST;
7001cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_DHU:
7011cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U8_LIST;
7021cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_N3U:
7031cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U8_LIST;
7041cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_CLIENT_SUBNET:
7051cb0ef41Sopenharmony_ci      /* Really it is a u16 address family, u8 source prefix length,
7061cb0ef41Sopenharmony_ci       * u8 scope prefix length, address */
7071cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
7081cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_EXPIRE:
7091cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U32;
7101cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_COOKIE:
7111cb0ef41Sopenharmony_ci      /* 8 bytes for client, 16-40 bytes for server */
7121cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
7131cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_TCP_KEEPALIVE:
7141cb0ef41Sopenharmony_ci      /* Timeout in 100ms intervals */
7151cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U16;
7161cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_PADDING:
7171cb0ef41Sopenharmony_ci      /* Arbitrary padding */
7181cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
7191cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_CHAIN:
7201cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_NAME;
7211cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_KEY_TAG:
7221cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U16_LIST;
7231cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EXTENDED_DNS_ERROR:
7241cb0ef41Sopenharmony_ci      /* Really 16bit code followed by textual message */
7251cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
7261cb0ef41Sopenharmony_ci  }
7271cb0ef41Sopenharmony_ci  return ARES_OPT_DATATYPE_BIN;
7281cb0ef41Sopenharmony_ci}
7291cb0ef41Sopenharmony_ci
7301cb0ef41Sopenharmony_cistatic ares_dns_opt_datatype_t ares_dns_opt_get_type_svcb(unsigned short opt)
7311cb0ef41Sopenharmony_ci{
7321cb0ef41Sopenharmony_ci  ares_svcb_param_t param = (ares_svcb_param_t)opt;
7331cb0ef41Sopenharmony_ci  switch (param) {
7341cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_NO_DEFAULT_ALPN:
7351cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_NONE;
7361cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_ECH:
7371cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_BIN;
7381cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_MANDATORY:
7391cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U16_LIST;
7401cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_ALPN:
7411cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_STR_LIST;
7421cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_PORT:
7431cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_U16;
7441cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_IPV4HINT:
7451cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_INADDR4_LIST;
7461cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_IPV6HINT:
7471cb0ef41Sopenharmony_ci      return ARES_OPT_DATATYPE_INADDR6_LIST;
7481cb0ef41Sopenharmony_ci  }
7491cb0ef41Sopenharmony_ci  return ARES_OPT_DATATYPE_BIN;
7501cb0ef41Sopenharmony_ci}
7511cb0ef41Sopenharmony_ci
7521cb0ef41Sopenharmony_ciares_dns_opt_datatype_t ares_dns_opt_get_datatype(ares_dns_rr_key_t key,
7531cb0ef41Sopenharmony_ci                                                  unsigned short    opt)
7541cb0ef41Sopenharmony_ci{
7551cb0ef41Sopenharmony_ci  switch (key) {
7561cb0ef41Sopenharmony_ci    case ARES_RR_OPT_OPTIONS:
7571cb0ef41Sopenharmony_ci      return ares_dns_opt_get_type_opt(opt);
7581cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PARAMS:
7591cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PARAMS:
7601cb0ef41Sopenharmony_ci      return ares_dns_opt_get_type_svcb(opt);
7611cb0ef41Sopenharmony_ci    default:
7621cb0ef41Sopenharmony_ci      break;
7631cb0ef41Sopenharmony_ci  }
7641cb0ef41Sopenharmony_ci  return ARES_OPT_DATATYPE_BIN;
7651cb0ef41Sopenharmony_ci}
7661cb0ef41Sopenharmony_ci
7671cb0ef41Sopenharmony_cistatic const char *ares_dns_opt_get_name_opt(unsigned short opt)
7681cb0ef41Sopenharmony_ci{
7691cb0ef41Sopenharmony_ci  ares_opt_param_t param = (ares_opt_param_t)opt;
7701cb0ef41Sopenharmony_ci  switch (param) {
7711cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_LLQ:
7721cb0ef41Sopenharmony_ci      return "LLQ";
7731cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_UL:
7741cb0ef41Sopenharmony_ci      return "UL";
7751cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_NSID:
7761cb0ef41Sopenharmony_ci      return "NSID";
7771cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_DAU:
7781cb0ef41Sopenharmony_ci      return "DAU";
7791cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_DHU:
7801cb0ef41Sopenharmony_ci      return "DHU";
7811cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_N3U:
7821cb0ef41Sopenharmony_ci      return "N3U";
7831cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_CLIENT_SUBNET:
7841cb0ef41Sopenharmony_ci      return "edns-client-subnet";
7851cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_EXPIRE:
7861cb0ef41Sopenharmony_ci      return "edns-expire";
7871cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_COOKIE:
7881cb0ef41Sopenharmony_ci      return "COOKIE";
7891cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_TCP_KEEPALIVE:
7901cb0ef41Sopenharmony_ci      return "edns-tcp-keepalive";
7911cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_PADDING:
7921cb0ef41Sopenharmony_ci      return "Padding";
7931cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_CHAIN:
7941cb0ef41Sopenharmony_ci      return "CHAIN";
7951cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EDNS_KEY_TAG:
7961cb0ef41Sopenharmony_ci      return "edns-key-tag";
7971cb0ef41Sopenharmony_ci    case ARES_OPT_PARAM_EXTENDED_DNS_ERROR:
7981cb0ef41Sopenharmony_ci      return "extended-dns-error";
7991cb0ef41Sopenharmony_ci  }
8001cb0ef41Sopenharmony_ci  return NULL;
8011cb0ef41Sopenharmony_ci}
8021cb0ef41Sopenharmony_ci
8031cb0ef41Sopenharmony_cistatic const char *ares_dns_opt_get_name_svcb(unsigned short opt)
8041cb0ef41Sopenharmony_ci{
8051cb0ef41Sopenharmony_ci  ares_svcb_param_t param = (ares_svcb_param_t)opt;
8061cb0ef41Sopenharmony_ci  switch (param) {
8071cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_NO_DEFAULT_ALPN:
8081cb0ef41Sopenharmony_ci      return "no-default-alpn";
8091cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_ECH:
8101cb0ef41Sopenharmony_ci      return "ech";
8111cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_MANDATORY:
8121cb0ef41Sopenharmony_ci      return "mandatory";
8131cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_ALPN:
8141cb0ef41Sopenharmony_ci      return "alpn";
8151cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_PORT:
8161cb0ef41Sopenharmony_ci      return "port";
8171cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_IPV4HINT:
8181cb0ef41Sopenharmony_ci      return "ipv4hint";
8191cb0ef41Sopenharmony_ci    case ARES_SVCB_PARAM_IPV6HINT:
8201cb0ef41Sopenharmony_ci      return "ipv6hint";
8211cb0ef41Sopenharmony_ci  }
8221cb0ef41Sopenharmony_ci  return NULL;
8231cb0ef41Sopenharmony_ci}
8241cb0ef41Sopenharmony_ci
8251cb0ef41Sopenharmony_ciconst char *ares_dns_opt_get_name(ares_dns_rr_key_t key, unsigned short opt)
8261cb0ef41Sopenharmony_ci{
8271cb0ef41Sopenharmony_ci  switch (key) {
8281cb0ef41Sopenharmony_ci    case ARES_RR_OPT_OPTIONS:
8291cb0ef41Sopenharmony_ci      return ares_dns_opt_get_name_opt(opt);
8301cb0ef41Sopenharmony_ci    case ARES_RR_SVCB_PARAMS:
8311cb0ef41Sopenharmony_ci    case ARES_RR_HTTPS_PARAMS:
8321cb0ef41Sopenharmony_ci      return ares_dns_opt_get_name_svcb(opt);
8331cb0ef41Sopenharmony_ci    default:
8341cb0ef41Sopenharmony_ci      break;
8351cb0ef41Sopenharmony_ci  }
8361cb0ef41Sopenharmony_ci  return NULL;
8371cb0ef41Sopenharmony_ci}
8381cb0ef41Sopenharmony_ci
8391cb0ef41Sopenharmony_ciconst char *ares_dns_rcode_tostr(ares_dns_rcode_t rcode)
8401cb0ef41Sopenharmony_ci{
8411cb0ef41Sopenharmony_ci  switch (rcode) {
8421cb0ef41Sopenharmony_ci    case ARES_RCODE_NOERROR:
8431cb0ef41Sopenharmony_ci      return "NOERROR";
8441cb0ef41Sopenharmony_ci    case ARES_RCODE_FORMERR:
8451cb0ef41Sopenharmony_ci      return "FORMERR";
8461cb0ef41Sopenharmony_ci    case ARES_RCODE_SERVFAIL:
8471cb0ef41Sopenharmony_ci      return "SERVFAIL";
8481cb0ef41Sopenharmony_ci    case ARES_RCODE_NXDOMAIN:
8491cb0ef41Sopenharmony_ci      return "NXDOMAIN";
8501cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTIMP:
8511cb0ef41Sopenharmony_ci      return "NOTIMP";
8521cb0ef41Sopenharmony_ci    case ARES_RCODE_REFUSED:
8531cb0ef41Sopenharmony_ci      return "REFUSED";
8541cb0ef41Sopenharmony_ci    case ARES_RCODE_YXDOMAIN:
8551cb0ef41Sopenharmony_ci      return "YXDOMAIN";
8561cb0ef41Sopenharmony_ci    case ARES_RCODE_YXRRSET:
8571cb0ef41Sopenharmony_ci      return "YXRRSET";
8581cb0ef41Sopenharmony_ci    case ARES_RCODE_NXRRSET:
8591cb0ef41Sopenharmony_ci      return "NXRRSET";
8601cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTAUTH:
8611cb0ef41Sopenharmony_ci      return "NOTAUTH";
8621cb0ef41Sopenharmony_ci    case ARES_RCODE_NOTZONE:
8631cb0ef41Sopenharmony_ci      return "NOTZONE";
8641cb0ef41Sopenharmony_ci    case ARES_RCODE_DSOTYPEI:
8651cb0ef41Sopenharmony_ci      return "DSOTYPEI";
8661cb0ef41Sopenharmony_ci    case ARES_RCODE_BADSIG:
8671cb0ef41Sopenharmony_ci      return "BADSIG";
8681cb0ef41Sopenharmony_ci    case ARES_RCODE_BADKEY:
8691cb0ef41Sopenharmony_ci      return "BADKEY";
8701cb0ef41Sopenharmony_ci    case ARES_RCODE_BADTIME:
8711cb0ef41Sopenharmony_ci      return "BADTIME";
8721cb0ef41Sopenharmony_ci    case ARES_RCODE_BADMODE:
8731cb0ef41Sopenharmony_ci      return "BADMODE";
8741cb0ef41Sopenharmony_ci    case ARES_RCODE_BADNAME:
8751cb0ef41Sopenharmony_ci      return "BADNAME";
8761cb0ef41Sopenharmony_ci    case ARES_RCODE_BADALG:
8771cb0ef41Sopenharmony_ci      return "BADALG";
8781cb0ef41Sopenharmony_ci    case ARES_RCODE_BADTRUNC:
8791cb0ef41Sopenharmony_ci      return "BADTRUNC";
8801cb0ef41Sopenharmony_ci    case ARES_RCODE_BADCOOKIE:
8811cb0ef41Sopenharmony_ci      return "BADCOOKIE";
8821cb0ef41Sopenharmony_ci  }
8831cb0ef41Sopenharmony_ci
8841cb0ef41Sopenharmony_ci  return "UNKNOWN";
8851cb0ef41Sopenharmony_ci}
886