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 #ifndef __ARES_DNS_PRIVATE_H 27 #define __ARES_DNS_PRIVATE_H 28 29 ares_bool_t ares_dns_rec_type_allow_name_compression(ares_dns_rec_type_t type); 30 ares_bool_t ares_dns_opcode_isvalid(ares_dns_opcode_t opcode); 31 ares_bool_t ares_dns_rcode_isvalid(ares_dns_rcode_t rcode); 32 ares_bool_t ares_dns_flags_arevalid(unsigned short flags); 33 ares_bool_t ares_dns_rec_type_isvalid(ares_dns_rec_type_t type, 34 ares_bool_t is_query); 35 ares_bool_t ares_dns_class_isvalid(ares_dns_class_t qclass, 36 ares_bool_t is_query); 37 ares_bool_t ares_dns_section_isvalid(ares_dns_section_t sect); 38 ares_status_t ares_dns_rr_set_str_own(ares_dns_rr_t *dns_rr, 39 ares_dns_rr_key_t key, char *val); 40 ares_status_t ares_dns_rr_set_bin_own(ares_dns_rr_t *dns_rr, 41 ares_dns_rr_key_t key, unsigned char *val, 42 size_t len); 43 ares_status_t ares_dns_rr_set_opt_own(ares_dns_rr_t *dns_rr, 44 ares_dns_rr_key_t key, unsigned short opt, 45 unsigned char *val, size_t val_len); 46 ares_status_t ares_dns_record_rr_prealloc(ares_dns_record_t *dnsrec, 47 ares_dns_section_t sect, size_t cnt); 48 ares_bool_t ares_dns_has_opt_rr(const ares_dns_record_t *rec); 49 void ares_dns_record_write_ttl_decrement(ares_dns_record_t *dnsrec, 50 unsigned int ttl_decrement); 51 52 struct ares_dns_qd { 53 char *name; 54 ares_dns_rec_type_t qtype; 55 ares_dns_class_t qclass; 56 }; 57 58 typedef struct { 59 struct in_addr addr; 60 } ares__dns_a_t; 61 62 typedef struct { 63 char *nsdname; 64 } ares__dns_ns_t; 65 66 typedef struct { 67 char *cname; 68 } ares__dns_cname_t; 69 70 typedef struct { 71 char *mname; 72 char *rname; 73 unsigned int serial; 74 unsigned int refresh; 75 unsigned int retry; 76 unsigned int expire; 77 unsigned int minimum; 78 } ares__dns_soa_t; 79 80 typedef struct { 81 char *dname; 82 } ares__dns_ptr_t; 83 84 typedef struct { 85 char *cpu; 86 char *os; 87 } ares__dns_hinfo_t; 88 89 typedef struct { 90 unsigned short preference; 91 char *exchange; 92 } ares__dns_mx_t; 93 94 typedef struct { 95 char *data; 96 size_t data_len; 97 } ares__dns_txt_t; 98 99 typedef struct { 100 struct ares_in6_addr addr; 101 } ares__dns_aaaa_t; 102 103 typedef struct { 104 unsigned short priority; 105 unsigned short weight; 106 unsigned short port; 107 char *target; 108 } ares__dns_srv_t; 109 110 typedef struct { 111 unsigned short order; 112 unsigned short preference; 113 char *flags; 114 char *services; 115 char *regexp; 116 char *replacement; 117 } ares__dns_naptr_t; 118 119 typedef struct { 120 unsigned short opt; 121 unsigned char *val; 122 size_t val_len; 123 } ares__dns_optval_t; 124 125 typedef struct { 126 ares__dns_optval_t *optval; /*!< Attribute/value pairs */ 127 size_t cnt; /*!< Count of Attribute/Value pairs */ 128 size_t alloc; /*!< Allocated count of attribute/value 129 * pairs */ 130 } ares__dns_options_t; 131 132 typedef struct { 133 unsigned short udp_size; /*!< taken from class */ 134 unsigned char version; /*!< taken from bits 8-16 of ttl */ 135 unsigned short flags; /*!< Flags, remaining 16 bits, though only 136 * 1 currently defined */ 137 ares__dns_options_t *options; /*!< Attribute/Value pairs */ 138 } ares__dns_opt_t; 139 140 typedef struct { 141 unsigned char cert_usage; 142 unsigned char selector; 143 unsigned char match; 144 unsigned char *data; 145 size_t data_len; 146 } ares__dns_tlsa_t; 147 148 typedef struct { 149 unsigned short priority; 150 char *target; 151 ares__dns_options_t *params; 152 } ares__dns_svcb_t; 153 154 typedef struct { 155 unsigned short priority; 156 unsigned short weight; 157 char *target; 158 } ares__dns_uri_t; 159 160 typedef struct { 161 unsigned char critical; 162 char *tag; 163 unsigned char *value; 164 size_t value_len; 165 } ares__dns_caa_t; 166 167 /*! Raw, unparsed RR data */ 168 typedef struct { 169 unsigned short type; /*!< Not ares_rec_type_t because it likely isn't one 170 * of those values since it wasn't parsed */ 171 unsigned char *data; /*!< Raw RR data */ 172 size_t length; /*!< Length of raw RR data */ 173 } ares__dns_raw_rr_t; 174 175 /*! DNS RR data structure */ 176 struct ares_dns_rr { 177 ares_dns_record_t *parent; 178 char *name; 179 ares_dns_rec_type_t type; 180 ares_dns_class_t rclass; 181 unsigned int ttl; 182 183 union { 184 ares__dns_a_t a; 185 ares__dns_ns_t ns; 186 ares__dns_cname_t cname; 187 ares__dns_soa_t soa; 188 ares__dns_ptr_t ptr; 189 ares__dns_hinfo_t hinfo; 190 ares__dns_mx_t mx; 191 ares__dns_txt_t txt; 192 ares__dns_aaaa_t aaaa; 193 ares__dns_srv_t srv; 194 ares__dns_naptr_t naptr; 195 ares__dns_opt_t opt; 196 ares__dns_tlsa_t tlsa; 197 ares__dns_svcb_t svcb; 198 ares__dns_svcb_t https; /*!< https is a type of svcb, so this is right */ 199 ares__dns_uri_t uri; 200 ares__dns_caa_t caa; 201 ares__dns_raw_rr_t raw_rr; 202 } r; 203 }; 204 205 /*! DNS data structure */ 206 struct ares_dns_record { 207 unsigned short id; /*!< DNS query id */ 208 unsigned short flags; /*!< One or more ares_dns_flags_t */ 209 ares_dns_opcode_t opcode; /*!< DNS Opcode */ 210 ares_dns_rcode_t rcode; /*!< DNS RCODE */ 211 unsigned short raw_rcode; /*!< Raw rcode, used to ultimately form real 212 * rcode after reading OPT record if it 213 * exists */ 214 unsigned int ttl_decrement; /*!< Special case to apply to writing out 215 * this record, where it will decrement 216 * the ttl of any resource records by 217 * this amount. Used for cache */ 218 219 ares_dns_qd_t *qd; 220 size_t qdcount; 221 size_t qdalloc; 222 223 ares_dns_rr_t *an; 224 size_t ancount; 225 size_t analloc; 226 227 ares_dns_rr_t *ns; 228 size_t nscount; 229 size_t nsalloc; 230 231 ares_dns_rr_t *ar; 232 size_t arcount; 233 size_t aralloc; 234 }; 235 236 #endif 237