• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_status_t        ares_dns_record_duplicate_ex(ares_dns_record_t      **dest,
30                                                   const ares_dns_record_t *src);
31 ares_bool_t          ares_dns_rec_allow_name_comp(ares_dns_rec_type_t type);
32 ares_bool_t          ares_dns_opcode_isvalid(ares_dns_opcode_t opcode);
33 ares_bool_t          ares_dns_rcode_isvalid(ares_dns_rcode_t rcode);
34 ares_bool_t          ares_dns_flags_arevalid(unsigned short flags);
35 ares_bool_t          ares_dns_rec_type_isvalid(ares_dns_rec_type_t type,
36                                                ares_bool_t         is_query);
37 ares_bool_t          ares_dns_class_isvalid(ares_dns_class_t    qclass,
38                                             ares_dns_rec_type_t type,
39                                             ares_bool_t         is_query);
40 ares_bool_t          ares_dns_section_isvalid(ares_dns_section_t sect);
41 ares_status_t        ares_dns_rr_set_str_own(ares_dns_rr_t    *dns_rr,
42                                              ares_dns_rr_key_t key, char *val);
43 ares_status_t        ares_dns_rr_set_bin_own(ares_dns_rr_t    *dns_rr,
44                                              ares_dns_rr_key_t key, unsigned char *val,
45                                              size_t len);
46 ares_status_t        ares_dns_rr_set_abin_own(ares_dns_rr_t          *dns_rr,
47                                               ares_dns_rr_key_t       key,
48                                               ares_dns_multistring_t *strs);
49 ares_status_t        ares_dns_rr_set_opt_own(ares_dns_rr_t    *dns_rr,
50                                              ares_dns_rr_key_t key, unsigned short opt,
51                                              unsigned char *val, size_t val_len);
52 ares_status_t        ares_dns_record_rr_prealloc(ares_dns_record_t *dnsrec,
53                                                  ares_dns_section_t sect, size_t cnt);
54 ares_dns_rr_t       *ares_dns_get_opt_rr(ares_dns_record_t *rec);
55 const ares_dns_rr_t *ares_dns_get_opt_rr_const(const ares_dns_record_t *rec);
56 void                 ares_dns_record_ttl_decrement(ares_dns_record_t *dnsrec,
57                                                    unsigned int       ttl_decrement);
58 
59 /* Same as ares_dns_write() but appends to an existing buffer object */
60 ares_status_t        ares_dns_write_buf(const ares_dns_record_t *dnsrec,
61                                         ares_buf_t              *buf);
62 
63 /* Same as ares_dns_write_buf(), but prepends a 16bit length */
64 ares_status_t        ares_dns_write_buf_tcp(const ares_dns_record_t *dnsrec,
65                                             ares_buf_t              *buf);
66 
67 /*! Create a DNS record object for a query. The arguments are the same as
68  *  those for ares_create_query().
69  *
70  *  \param[out] dnsrec       DNS record object to create.
71  *  \param[in]  name         NUL-terminated name for the query.
72  *  \param[in]  dnsclass     Class for the query.
73  *  \param[in]  type         Type for the query.
74  *  \param[in]  id           Identifier for the query.
75  *  \param[in]  flags        Flags for the query.
76  *  \param[in]  max_udp_size Maximum size of a UDP packet for EDNS.
77  *  \return ARES_SUCCESS on success, otherwise an error code.
78  */
79 ares_status_t
80   ares_dns_record_create_query(ares_dns_record_t **dnsrec, const char *name,
81                                ares_dns_class_t    dnsclass,
82                                ares_dns_rec_type_t type, unsigned short id,
83                                ares_dns_flags_t flags, size_t max_udp_size);
84 
85 /*! Convert the RCODE and ANCOUNT from a DNS query reply into a status code.
86  *
87  *  \param[in] rcode   The RCODE from the reply.
88  *  \param[in] ancount The ANCOUNT from the reply.
89  *  \return An appropriate status code.
90  */
91 ares_status_t ares_dns_query_reply_tostatus(ares_dns_rcode_t rcode,
92                                             size_t           ancount);
93 
94 struct ares_dns_qd {
95   char               *name;
96   ares_dns_rec_type_t qtype;
97   ares_dns_class_t    qclass;
98 };
99 
100 typedef struct {
101   struct in_addr addr;
102 } ares_dns_a_t;
103 
104 typedef struct {
105   char *nsdname;
106 } ares_dns_ns_t;
107 
108 typedef struct {
109   char *cname;
110 } ares_dns_cname_t;
111 
112 typedef struct {
113   char        *mname;
114   char        *rname;
115   unsigned int serial;
116   unsigned int refresh;
117   unsigned int retry;
118   unsigned int expire;
119   unsigned int minimum;
120 } ares_dns_soa_t;
121 
122 typedef struct {
123   char *dname;
124 } ares_dns_ptr_t;
125 
126 typedef struct {
127   char *cpu;
128   char *os;
129 } ares_dns_hinfo_t;
130 
131 typedef struct {
132   unsigned short preference;
133   char          *exchange;
134 } ares_dns_mx_t;
135 
136 typedef struct {
137   ares_dns_multistring_t *strs;
138 } ares_dns_txt_t;
139 
140 typedef struct {
141   unsigned short type_covered;
142   unsigned char  algorithm;
143   unsigned char  labels;
144   unsigned int   original_ttl;
145   unsigned int   expiration;
146   unsigned int   inception;
147   unsigned short key_tag;
148   char          *signers_name;
149   unsigned char *signature;
150   size_t         signature_len;
151 } ares_dns_sig_t;
152 
153 typedef struct {
154   struct ares_in6_addr addr;
155 } ares_dns_aaaa_t;
156 
157 typedef struct {
158   unsigned short priority;
159   unsigned short weight;
160   unsigned short port;
161   char          *target;
162 } ares_dns_srv_t;
163 
164 typedef struct {
165   unsigned short order;
166   unsigned short preference;
167   char          *flags;
168   char          *services;
169   char          *regexp;
170   char          *replacement;
171 } ares_dns_naptr_t;
172 
173 typedef struct {
174   unsigned short opt;
175   unsigned char *val;
176   size_t         val_len;
177 } ares_dns_optval_t;
178 
179 typedef struct {
180   unsigned short udp_size; /*!< taken from class */
181   unsigned char  version;  /*!< taken from bits 8-16 of ttl */
182   unsigned short flags;    /*!< Flags, remaining 16 bits, though only
183                             *   1 currently defined */
184   ares_array_t  *options;  /*!< Type is ares_dns_optval_t */
185 } ares_dns_opt_t;
186 
187 typedef struct {
188   unsigned char  cert_usage;
189   unsigned char  selector;
190   unsigned char  match;
191   unsigned char *data;
192   size_t         data_len;
193 } ares_dns_tlsa_t;
194 
195 typedef struct {
196   unsigned short priority;
197   char          *target;
198   ares_array_t  *params; /*!< Type is ares_dns_optval_t */
199 } ares_dns_svcb_t;
200 
201 typedef struct {
202   unsigned short priority;
203   unsigned short weight;
204   char          *target;
205 } ares_dns_uri_t;
206 
207 typedef struct {
208   unsigned char  critical;
209   char          *tag;
210   unsigned char *value;
211   size_t         value_len;
212 } ares_dns_caa_t;
213 
214 /*! Raw, unparsed RR data */
215 typedef struct {
216   unsigned short type;   /*!< Not ares_rec_type_t because it likely isn't one
217                           *   of those values since it wasn't parsed */
218   unsigned char *data;   /*!< Raw RR data */
219   size_t         length; /*!< Length of raw RR data */
220 } ares_dns_raw_rr_t;
221 
222 /*! DNS RR data structure */
223 struct ares_dns_rr {
224   ares_dns_record_t  *parent;
225   char               *name;
226   ares_dns_rec_type_t type;
227   ares_dns_class_t    rclass;
228   unsigned int        ttl;
229 
230   union {
231     ares_dns_a_t      a;
232     ares_dns_ns_t     ns;
233     ares_dns_cname_t  cname;
234     ares_dns_soa_t    soa;
235     ares_dns_ptr_t    ptr;
236     ares_dns_hinfo_t  hinfo;
237     ares_dns_mx_t     mx;
238     ares_dns_txt_t    txt;
239     ares_dns_sig_t    sig;
240     ares_dns_aaaa_t   aaaa;
241     ares_dns_srv_t    srv;
242     ares_dns_naptr_t  naptr;
243     ares_dns_opt_t    opt;
244     ares_dns_tlsa_t   tlsa;
245     ares_dns_svcb_t   svcb;
246     ares_dns_svcb_t   https; /*!< https is a type of svcb, so this is right */
247     ares_dns_uri_t    uri;
248     ares_dns_caa_t    caa;
249     ares_dns_raw_rr_t raw_rr;
250   } r;
251 };
252 
253 /*! DNS data structure */
254 struct ares_dns_record {
255   unsigned short    id;            /*!< DNS query id */
256   unsigned short    flags;         /*!< One or more ares_dns_flags_t */
257   ares_dns_opcode_t opcode;        /*!< DNS Opcode */
258   ares_dns_rcode_t  rcode;         /*!< DNS RCODE */
259   unsigned short    raw_rcode;     /*!< Raw rcode, used to ultimately form real
260                                     *   rcode after reading OPT record if it
261                                     *   exists */
262   unsigned int      ttl_decrement; /*!< Special case to apply to writing out
263                                     *   this record, where it will decrement
264                                     *   the ttl of any resource records by
265                                     *   this amount.  Used for cache */
266 
267   ares_array_t     *qd;            /*!< Type is ares_dns_qd_t */
268   ares_array_t     *an;            /*!< Type is ares_dns_rr_t */
269   ares_array_t     *ns;            /*!< Type is ares_dns_rr_t */
270   ares_array_t     *ar;            /*!< Type is ares_dns_rr_t */
271 };
272 
273 #endif
274