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
271cb0ef41Sopenharmony_ci#include "ares_setup.h"
281cb0ef41Sopenharmony_ci#include "ares.h"
291cb0ef41Sopenharmony_ci#include "ares_private.h"
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ciint ares_create_query(const char *name, int dnsclass, int type,
321cb0ef41Sopenharmony_ci                      unsigned short id, int rd, unsigned char **bufp,
331cb0ef41Sopenharmony_ci                      int *buflenp, int max_udp_size)
341cb0ef41Sopenharmony_ci{
351cb0ef41Sopenharmony_ci  ares_status_t      status;
361cb0ef41Sopenharmony_ci  ares_dns_record_t *dnsrec = NULL;
371cb0ef41Sopenharmony_ci  size_t             len;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  if (name == NULL || bufp == NULL || buflenp == NULL) {
401cb0ef41Sopenharmony_ci    status = ARES_EFORMERR;
411cb0ef41Sopenharmony_ci    goto done;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  *bufp    = NULL;
451cb0ef41Sopenharmony_ci  *buflenp = 0;
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
481cb0ef41Sopenharmony_ci  if (ares__is_onion_domain(name)) {
491cb0ef41Sopenharmony_ci    status = ARES_ENOTFOUND;
501cb0ef41Sopenharmony_ci    goto done;
511cb0ef41Sopenharmony_ci  }
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci  status = ares_dns_record_create(&dnsrec, id, rd ? ARES_FLAG_RD : 0,
541cb0ef41Sopenharmony_ci                                  ARES_OPCODE_QUERY, ARES_RCODE_NOERROR);
551cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
561cb0ef41Sopenharmony_ci    goto done;
571cb0ef41Sopenharmony_ci  }
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  status = ares_dns_record_query_add(dnsrec, name, (ares_dns_rec_type_t)type,
601cb0ef41Sopenharmony_ci                                     (ares_dns_class_t)dnsclass);
611cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
621cb0ef41Sopenharmony_ci    goto done;
631cb0ef41Sopenharmony_ci  }
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci  /* max_udp_size > 0 indicates EDNS, so send OPT RR as an additional record */
661cb0ef41Sopenharmony_ci  if (max_udp_size > 0) {
671cb0ef41Sopenharmony_ci    ares_dns_rr_t *rr = NULL;
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci    status = ares_dns_record_rr_add(&rr, dnsrec, ARES_SECTION_ADDITIONAL, "",
701cb0ef41Sopenharmony_ci                                    ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0);
711cb0ef41Sopenharmony_ci    if (status != ARES_SUCCESS) {
721cb0ef41Sopenharmony_ci      goto done;
731cb0ef41Sopenharmony_ci    }
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    if (max_udp_size > 65535) {
761cb0ef41Sopenharmony_ci      status = ARES_EFORMERR;
771cb0ef41Sopenharmony_ci      goto done;
781cb0ef41Sopenharmony_ci    }
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci    status = ares_dns_rr_set_u16(rr, ARES_RR_OPT_UDP_SIZE,
811cb0ef41Sopenharmony_ci                                 (unsigned short)max_udp_size);
821cb0ef41Sopenharmony_ci    if (status != ARES_SUCCESS) {
831cb0ef41Sopenharmony_ci      goto done;
841cb0ef41Sopenharmony_ci    }
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci    status = ares_dns_rr_set_u8(rr, ARES_RR_OPT_VERSION, 0);
871cb0ef41Sopenharmony_ci    if (status != ARES_SUCCESS) {
881cb0ef41Sopenharmony_ci      goto done;
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci    status = ares_dns_rr_set_u16(rr, ARES_RR_OPT_FLAGS, 0);
921cb0ef41Sopenharmony_ci    if (status != ARES_SUCCESS) {
931cb0ef41Sopenharmony_ci      goto done;
941cb0ef41Sopenharmony_ci    }
951cb0ef41Sopenharmony_ci  }
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci  status = ares_dns_write(dnsrec, bufp, &len);
981cb0ef41Sopenharmony_ci  if (status != ARES_SUCCESS) {
991cb0ef41Sopenharmony_ci    goto done;
1001cb0ef41Sopenharmony_ci  }
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  *buflenp = (int)len;
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_cidone:
1051cb0ef41Sopenharmony_ci  ares_dns_record_destroy(dnsrec);
1061cb0ef41Sopenharmony_ci  return (int)status;
1071cb0ef41Sopenharmony_ci}
108