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_setup.h"
28#include "ares.h"
29#include "ares_private.h"
30
31int ares_create_query(const char *name, int dnsclass, int type,
32                      unsigned short id, int rd, unsigned char **bufp,
33                      int *buflenp, int max_udp_size)
34{
35  ares_status_t      status;
36  ares_dns_record_t *dnsrec = NULL;
37  size_t             len;
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  /* Per RFC 7686, reject queries for ".onion" domain names with NXDOMAIN. */
48  if (ares__is_onion_domain(name)) {
49    status = ARES_ENOTFOUND;
50    goto done;
51  }
52
53  status = ares_dns_record_create(&dnsrec, id, rd ? ARES_FLAG_RD : 0,
54                                  ARES_OPCODE_QUERY, ARES_RCODE_NOERROR);
55  if (status != ARES_SUCCESS) {
56    goto done;
57  }
58
59  status = ares_dns_record_query_add(dnsrec, name, (ares_dns_rec_type_t)type,
60                                     (ares_dns_class_t)dnsclass);
61  if (status != ARES_SUCCESS) {
62    goto done;
63  }
64
65  /* max_udp_size > 0 indicates EDNS, so send OPT RR as an additional record */
66  if (max_udp_size > 0) {
67    ares_dns_rr_t *rr = NULL;
68
69    status = ares_dns_record_rr_add(&rr, dnsrec, ARES_SECTION_ADDITIONAL, "",
70                                    ARES_REC_TYPE_OPT, ARES_CLASS_IN, 0);
71    if (status != ARES_SUCCESS) {
72      goto done;
73    }
74
75    if (max_udp_size > 65535) {
76      status = ARES_EFORMERR;
77      goto done;
78    }
79
80    status = ares_dns_rr_set_u16(rr, ARES_RR_OPT_UDP_SIZE,
81                                 (unsigned short)max_udp_size);
82    if (status != ARES_SUCCESS) {
83      goto done;
84    }
85
86    status = ares_dns_rr_set_u8(rr, ARES_RR_OPT_VERSION, 0);
87    if (status != ARES_SUCCESS) {
88      goto done;
89    }
90
91    status = ares_dns_rr_set_u16(rr, ARES_RR_OPT_FLAGS, 0);
92    if (status != ARES_SUCCESS) {
93      goto done;
94    }
95  }
96
97  status = ares_dns_write(dnsrec, bufp, &len);
98  if (status != ARES_SUCCESS) {
99    goto done;
100  }
101
102  *buflenp = (int)len;
103
104done:
105  ares_dns_record_destroy(dnsrec);
106  return (int)status;
107}
108