1 /* dns-msg.h
2  *
3  * Copyright (c) 2018, 2019 Apple Computer, Inc. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Lightweight framework for generating, sending, and unpacking DNS messages.
18  * Definitions...
19  */
20 
21 #ifndef __DNS_MSG_H
22 #define __DNS_MSG_H
23 
24 #include "srp.h"
25 
26 #ifndef DNS_MAX_UDP_PAYLOAD
27 #define DNS_MAX_UDP_PAYLOAD 1410
28 #endif
29 
30 #define DNS_HEADER_SIZE            12
31 #define DNS_DATA_SIZE              (DNS_MAX_UDP_PAYLOAD - DNS_HEADER_SIZE)
32 #define DNS_MAX_POINTER            ((2 << 14) - 1)
33 #define DNS_MAX_LABEL_SIZE         63
34 #define DNS_MAX_LABEL_SIZE_ESCAPED 252
35 #define DNS_MAX_NAME_SIZE          255
36 #define DNS_MAX_NAME_SIZE_ESCAPED  1009
37 #define DNS_MAX_LABELS             128
38 
39 typedef struct message message_t;
40 
41 typedef struct dns_wire dns_wire_t;
42 struct dns_wire {
43     uint16_t id;
44     uint16_t bitfield;
45     uint16_t qdcount;
46     uint16_t ancount;
47     uint16_t nscount;
48     uint16_t arcount;
49     uint8_t data[DNS_DATA_SIZE];
50 };
51 
52 typedef struct dns_name_pointer dns_name_pointer_t;
53 struct dns_name_pointer {
54     dns_name_pointer_t *NULLABLE next;
55     uint8_t *NONNULL message_start;
56     uint8_t *NONNULL name_start;
57     int num_labels;
58     int length;
59 };
60 
61 typedef struct dns_towire_state dns_towire_state_t;
62 struct dns_towire_state {
63     dns_wire_t *NULLABLE message;
64     uint8_t *NONNULL p;
65     uint8_t *NONNULL lim;
66     uint8_t *NULLABLE p_rdlength;
67     uint8_t *NULLABLE p_opt;
68     uint16_t line, outer_line;
69     bool truncated : 1;
70     unsigned int error : 31;
71 };
72 
73 typedef struct dns_transaction dns_transaction_t;
74 struct dns_transaction {
75     dns_transaction_t *NULLABLE next;
76     dns_towire_state_t towire;
77     dns_wire_t *NULLABLE response;
78     int response_length;
79     int sock;
80 };
81 
82 typedef void (*dns_response_callback_t)(dns_transaction_t *NONNULL txn);
83 
84 typedef struct dns_label dns_label_t;
85 typedef dns_label_t dns_name_t;
86 struct dns_label {
87     dns_label_t *NULLABLE next;
88     uint8_t len;
89     char data[DNS_MAX_LABEL_SIZE];
90 };
91 
92 typedef struct dns_rdata_txt dns_rdata_txt_t;
93 struct dns_rdata_txt {
94     uint8_t len;
95     char *NONNULL data;
96 };
97 
98 typedef struct dns_rdata_unparsed dns_rdata_unparsed_t;
99 struct dns_rdata_unparsed {
100     uint8_t *NULLABLE data;
101     uint16_t len;
102 };
103 
104 typedef struct dns_rdata_single_name dns_rdata_ptr_t;
105 typedef struct dns_rdata_single_name dns_rdata_cname_t;
106 struct dns_rdata_single_name {
107     dns_label_t *NONNULL name;
108 };
109 
110 typedef struct dns_rdata_srv dns_rdata_srv_t;
111 struct dns_rdata_srv {
112     dns_label_t *NONNULL name;
113     uint16_t priority;
114     uint16_t weight;
115     uint16_t port;
116 };
117 
118 typedef struct dns_rdata_sig dns_rdata_sig_t;
119 struct dns_rdata_sig {
120     uint16_t type;
121     uint8_t algorithm;
122     uint8_t label;
123     uint32_t rrttl;
124     uint32_t expiry;
125     uint32_t inception;
126     uint16_t key_tag;
127     dns_label_t *NONNULL signer;
128     int start;
129     int len;
130     uint8_t *NONNULL signature;
131 };
132 
133 typedef struct dns_rdata_key dns_rdata_key_t;
134 struct dns_rdata_key {
135     uint16_t flags;
136     uint8_t protocol;
137     uint8_t algorithm;
138     int len;
139     uint8_t *NONNULL key;
140 };
141 
142 typedef struct dns_rr dns_rr_t;
143 struct dns_rr {
144     dns_label_t *NONNULL name;
145     uint16_t type;
146     uint16_t qclass;
147     uint32_t ttl;
148     union {
149         dns_rdata_unparsed_t unparsed;
150         dns_rdata_ptr_t ptr;
151         dns_rdata_cname_t cname;
152         struct in_addr a;
153         struct in6_addr aaaa;
154         dns_rdata_srv_t srv;
155         dns_rdata_txt_t txt;
156         dns_rdata_sig_t sig;
157         dns_rdata_key_t key;
158     } data;
159 };
160 
161 typedef struct dns_edns0 dns_edns0_t;
162 struct dns_edns0 {
163     dns_edns0_t *NULLABLE next;
164     uint16_t length;
165     uint16_t type;
166     uint8_t data[0];
167 };
168 
169 typedef struct dns_message dns_message_t;
170 struct dns_message {
171     int ref_count;
172     int qdcount, ancount, nscount, arcount;
173     dns_rr_t *NULLABLE questions;
174     dns_rr_t *NULLABLE answers;
175     dns_rr_t *NULLABLE authority;
176     dns_rr_t *NULLABLE additional;
177     dns_edns0_t *NULLABLE edns0;
178 };
179 
180 // Masks for bitfield data
181 #define dns_qr_mask     0x8000
182 #define dns_opcode_mask 0x7800
183 #define dns_flags_mask  0x07f0
184 #define dns_rcode_mask  0x000f
185 
186 // Shifts for bitfield data
187 #define dns_qr_shift     15
188 #define dns_opcode_shift 11
189 #define dns_rcode_shift  0
190 
191 // Booleans
192 #define dns_flags_aa 0x0400
193 #define dns_flags_tc 0x0200
194 #define dns_flags_rd 0x0100
195 #define dns_flags_ra 0x0080
196 #define dns_flags_ad 0x0020
197 #define dns_flags_cd 0x0010
198 
199 // Getters
200 #define dns_qr_get(w)     ((ntohs((w)->bitfield) & dns_qr_mask) >> dns_qr_shift)
201 #define dns_opcode_get(w) ((ntohs((w)->bitfield) & dns_opcode_mask) >> dns_opcode_shift)
202 #define dns_rcode_get(w)  ((ntohs((w)->bitfield) & dns_rcode_mask) >> dns_rcode_shift)
203 
204 // Setters
205 #define dns_qr_set(w, value) \
206     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_qr_mask) | ((value) << dns_qr_shift))))
207 #define dns_opcode_set(w, value) \
208     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_opcode_mask) | ((value) << dns_opcode_shift))))
209 #define dns_rcode_set(w, value) \
210     ((w)->bitfield = htons(((ntohs((w)->bitfield) & ~dns_rcode_mask) | ((value) << dns_rcode_shift))))
211 
212 // Query/Response
213 #define dns_qr_query           0
214 #define dns_qr_response        1
215 
216 // Opcodes
217 #define dns_opcode_query       0
218 #define dns_opcode_iquery      1
219 #define dns_opcode_status      2
220 #define dns_opcode_notify      4
221 #define dns_opcode_update      5
222 #define dns_opcode_dso         6
223 
224 // Response Codes
225 #define dns_rcode_noerror      0 // [RFC1035] No Error
226 #define dns_rcode_formerr      1 // [RFC1035] Format Error
227 #define dns_rcode_servfail     2 // [RFC1035] Server Failure
228 #define dns_rcode_nxdomain     3 // [RFC1035] Non-Existent Domain
229 #define dns_rcode_notimp       4 // [RFC1035] Not Implemented
230 #define dns_rcode_refused      5 // [RFC1035] Query Refused
231 #define dns_rcode_yxdomain     6 // [RFC2136][RFC6672] Name Exists when it should not
232 #define dns_rcode_yxrrset      7 // [RFC2136] RR Set Exists when it should not
233 #define dns_rcode_nxrrset      8 // [RFC2136] RR Set that should exist does not
234 #define dns_rcode_notauth      9 // [RFC2136] Server Not Authoritative for zone, or [RFC2845] Not Authorized
235 #define dns_rcode_notzone     10 // [RFC2136] Name not contained in zone
236 #define dns_rcode_dsotypeni   11 // [RFCTBD draft-ietf-dnsop-session-signal] DSO-Type Not Implemented
237 #define dns_rcode_badvers     16 // [RFC6891] Bad OPT Version, or [RFC2845] TSIG Signature Failure
238 #define dns_rcode_badkey      17 // [RFC2845] Key not recognized
239 #define dns_rcode_badtime     18 // [RFC2845] Signature out of time window
240 #define dns_rcode_badmode     19 // [RFC2930] Bad TKEY Mode
241 #define dns_rcode_badname     20 // [RFC2930] Duplicate key name
242 #define dns_rcode_badalg      21 // [RFC2930] Algorithm not supported
243 #define dns_rcode_badtrunc    22 // [RFC4635] Bad Truncation
244 #define dns_rcode_badcookie   23 // [RFC7873] Bad/missing Server Cookie
245 
246 #define dns_qclass_in          1 // [RFC1035] Internet (IN)
247 #define dns_qclass_chaos       3 // [D. Moon, "Chaosnet"] Chaosnet (MIT)
248 #define dns_qclass_hesiod      4 // [MIT Project Athena Technical Plan] Hesiod service
249 #define dns_qclass_none      254 // [RFC2136] NONE (delete, or not in use)
250 #define dns_qclass_any       255 // [RFC1035] ANY (wildcard)
251 
252 #define dns_rrtype_a           1 // [RFC1035] a host address
253 #define dns_rrtype_ns          2 // [RFC1035] an authoritative name server
254 #define dns_rrtype_md          3 // [RFC1035] a mail destination (OBSOLETE - use MX)
255 #define dns_rrtype_mf          4 // [RFC1035] a mail forwarder (OBSOLETE - use MX)
256 #define dns_rrtype_cname       5 // [RFC1035] the canonical name for an alias
257 #define dns_rrtype_soa         6 // [RFC1035] marks the start of a zone of authority
258 #define dns_rrtype_mb          7 // [RFC1035] a mailbox domain name (EXPERIMENTAL)
259 #define dns_rrtype_mg          8 // [RFC1035] a mail group member (EXPERIMENTAL)
260 #define dns_rrtype_mr          9 // [RFC1035] a mail rename domain name (EXPERIMENTAL)
261 #define dns_rrtype_null       10 // [RFC1035]    a null RR (EXPERIMENTAL)
262 #define dns_rrtype_wks        11 // [RFC1035]    a well known service description
263 #define dns_rrtype_ptr        12 // [RFC1035]    a domain name pointer
264 #define dns_rrtype_hinfo      13 // [RFC1035]    host information
265 #define dns_rrtype_minfo      14 // [RFC1035]    mailbox or mail list information
266 #define dns_rrtype_mx         15 // [RFC1035]    mail exchange
267 #define dns_rrtype_txt        16 // [RFC1035] text strings
268 #define dns_rrtype_rp         17 // [RFC1183] for Responsible Person
269 #define dns_rrtype_afsdb      18 // [RFC1183,RFC5864] for AFS Data Base location
270 #define dns_rrtype_x25        19 // [RFC1183] for X.25 PSDN address
271 #define dns_rrtype_isdn       20 // [RFC1183] for ISDN address
272 #define dns_rrtype_rt         21 // [RFC1183] for Route Through
273 #define dns_rrtype_nsap       22 // [RFC1706] for NSAP address, NSAP style A record
274 #define dns_rrtype_nsap_ptr   23 // [RFC1348,RFC1637,RFC1706] for domain name pointer, NSAP style
275 #define dns_rrtype_sig        24 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2931,RFC3110,RFC3008]
276 #define dns_rrtype_key        25 // [RFC4034,RFC3755,RFC2535,RFC2536,RFC2537,RFC2539,RFC3008,RFC3110]
277 #define dns_rrtype_px         26 // [RFC2163] X.400 mail mapping information
278 #define dns_rrtype_gpos       27 // [RFC1712] Geographical Position
279 #define dns_rrtype_aaaa       28 // [RFC3596] IP6 Address
280 #define dns_rrtype_loc        29 // [RFC1876] Location Information
281 #define dns_rrtype_nxt        30 // [RFC3755] [RFC2535] Next Domain (OBSOLETE)
282 #define dns_rrtype_eid        31 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Endpoint Identifier
283 #define dns_rrtype_nimloc     32 // [http://ana-3.lcs.mit.edu/~jnc/nimrod/dns.txt] Nimrod Locator
284 #define dns_rrtype_srv        33 // [RFC2782] Server Selection
285 #define dns_rrtype_atma       34 // ["ATM Name System, V2.0"] ATM Address
286 #define dns_rrtype_naptr      35 // [RFC2915] [RFC2168] [RFC3403] Naming Authority Pointer
287 #define dns_rrtype_kx         36 // [RFC2230] Key Exchanger
288 #define dns_rrtype_cert       37 // [RFC4398] CERT
289 #define dns_rrtype_a6         38 // [RFC3226] [RFC2874] [RFC6563]    A6 (OBSOLETE - use AAAA)
290 #define dns_rrtype_dname      39 // [RFC6672]
291 #define dns_rrtype_sink       40 // [http://tools.ietf.org/html/draft-eastlake-kitchen-sink]
292 #define dns_rrtype_opt        41 // [RFC6891] [RFC3225]
293 #define dns_rrtype_apl        42 // [RFC3123]
294 #define dns_rrtype_ds         43 // [RFC4034] [RFC3658] Delegation Signer
295 #define dns_rrtype_sshfp      44 // [RFC4255] SSH Key Fingerprint
296 #define dns_rrtype_ipseckey   45 // [RFC4025]
297 #define dns_rrtype_rrsig      46 // [RFC4034] [RFC3755]
298 #define dns_rrtype_nsec       47 // [RFC4034] [RFC3755]
299 #define dns_rrtype_dnskey     48 // [RFC4034] [RFC3755]
300 #define dns_rrtype_dhcid      49 // [RFC4701] DHCID
301 #define dns_rrtype_nsec3      50 // [RFC5155] NSEC3
302 #define dns_rrtype_nsec3param 51 // [RFC5155] NSEC3PARAM
303 #define dns_rrtype_tlsa       52 // [RFC6698] TLSA
304 #define dns_rrtype_smimea     53 // [RFC8162] S/MIME cert association
305 #define dns_rrtype_hip        55 // Host Identity Protocol
306 #define dns_rrtype_ninfo      56 // [Jim_Reid] NINFO/ninfo-completed-template
307 #define dns_rrtype_rkey       57 // [Jim_Reid] RKEY/rkey-completed-template
308 #define dns_rrtype_talink     58 // [Wouter_Wijngaards] Trust Anchor LINK
309 #define dns_rrtype_cds        59 // [RFC7344] Child DS
310 #define dns_rrtype_cdnskey    60 // [RFC7344]   DNSKEY(s) the Child wants reflected in DS
311 #define dns_rrtype_openpgpkey 61 // [RFC7929]   OpenPGP Key
312 #define dns_rrtype_csync      62 // [RFC7477] Child-To-Parent Synchronization
313 #define dns_rrtype_spf        99 // [RFC7208]
314 #define dns_rrtype_uinfo     100 // [IANA-Reserved]
315 #define dns_rrtype_uid       101 // [IANA-Reserved]
316 #define dns_rrtype_gid       102 // [IANA-Reserved]
317 #define dns_rrtype_unspec    103 // [IANA-Reserved]
318 #define dns_rrtype_nid       104 // [RFC6742]
319 #define dns_rrtype_l32       105 // [RFC6742]
320 #define dns_rrtype_l64       106 // [RFC6742]
321 #define dns_rrtype_lp        107 // [RFC6742]
322 #define dns_rrtype_eui48     108 // an EUI-48 address [RFC7043]
323 #define dns_rrtype_eui64     109 // an EUI-64 address [RFC7043]
324 #define dns_rrtype_tkey      249 // Transaction Key [RFC2930]
325 #define dns_rrtype_tsig      250 // Transaction Signature [RFC2845]
326 #define dns_rrtype_ixfr      251 // incremental transfer    [RFC1995]
327 #define dns_rrtype_axfr      252 // transfer of an entire zone [RFC1035][RFC5936]
328 #define dns_rrtype_mailb     253 // mailbox-related RRs (MB, MG or MR) [RFC1035]
329 #define dns_rrtype_maila     254 // mail agent RRs (OBSOLETE - see MX) [RFC1035]
330 #define dns_rrtype_any       255 // A request for some or all records the server has available
331 #define dns_rrtype_uri       256 // URI [RFC7553]   URI/uri-completed-template
332 #define dns_rrtype_caa       257 // Certification Authority Restriction [RFC6844]
333 #define dns_rrtype_avc       258 // Application Visibility and Control [Wolfgang_Riedel]
334 #define dns_rrtype_doa       259 // Digital Object Architecture [draft-durand-doa-over-dns]
335 
336 #define dns_opt_llq            1 // On-hold [http://files.dns-sd.org/draft-sekar-dns-llq.txt]
337 #define dns_opt_update_lease   2 // On-hold [http://files.dns-sd.org/draft-sekar-dns-ul.txt]
338 #define dns_opt_nsid           3 // [RFC5001]
339 #define dns_opt_owner          4 // [draft-cheshire-edns0-owner-option]
340 #define dns_opt_dau            5 // [RFC6975]
341 #define dns_opt_dhu            6 // [RFC6975]
342 #define dns_opt_n3u            7 // [RFC6975]
343 #define dns_opt_client_subnet  8 // [RFC7871]
344 #define dns_opt_expire         9 // [RFC7314]
345 #define dns_opt_cookie        10 // [RFC7873]
346 #define dns_opt_keepalive     11 // [RFC7828]
347 #define dns_opt_padding       12 // [RFC7830]
348 #define dns_opt_chain         13 // [RFC7901]
349 #define dns_opt_key_tag       14 // [RFC8145]
350 
351 // towire.c:
352 
353 uint16_t srp_random16(void);
354 void dns_name_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
355                        dns_towire_state_t *NONNULL txn,
356                        const char *NONNULL name, int line);
357 #define dns_name_to_wire(r_pointer, txn, name) dns_name_to_wire_(r_pointer, txn, name, __LINE__)
358 
359 void dns_full_name_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
360                             dns_towire_state_t *NONNULL txn,
361                             const char *NONNULL name, int line);
362 #define dns_full_name_to_wire(r_pointer, txn, name) dns_full_name_to_wire_(r_pointer, txn, name, __LINE__)
363 
364 void dns_pointer_to_wire_(dns_name_pointer_t *NULLABLE r_pointer,
365                           dns_towire_state_t *NONNULL txn,
366                           dns_name_pointer_t *NONNULL pointer, int line);
367 #define dns_pointer_to_wire(r_pointer, txn, pointer) dns_pointer_to_wire_(r_pointer, txn, pointer, __LINE__)
368 
369 void dns_u8_to_wire_(dns_towire_state_t *NONNULL txn, uint8_t val, int line);
370 #define dns_u8_to_wire(txn, val) dns_u8_to_wire_(txn, val, __LINE__)
371 
372 void dns_u16_to_wire_(dns_towire_state_t *NONNULL txn, uint16_t val, int line);
373 #define dns_u16_to_wire(txn, val) dns_u16_to_wire_(txn, val, __LINE__)
374 
375 void dns_u32_to_wire_(dns_towire_state_t *NONNULL txn, uint32_t val, int line);
376 #define dns_u32_to_wire(txn, val) dns_u32_to_wire_(txn, val, __LINE__)
377 
378 void dns_ttl_to_wire_(dns_towire_state_t *NONNULL txn, int32_t val, int line);
379 #define dns_ttl_to_wire(txn, val) dns_ttl_to_wire_(txn, val, __LINE__)
380 
381 void dns_rdlength_begin_(dns_towire_state_t *NONNULL txn, int line);
382 #define dns_rdlength_begin(txn) dns_rdlength_begin_(txn, __LINE__)
383 
384 void dns_rdlength_end_(dns_towire_state_t *NONNULL txn, int line);
385 #define dns_rdlength_end(txn) dns_rdlength_end_(txn, __LINE__)
386 
387 void dns_rdata_a_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL ip_address, int line);
388 #define dns_rdata_a_to_wire(txn, ip_address) dns_rdata_a_to_wire_(txn, ip_address, __LINE__)
389 
390 void dns_rdata_aaaa_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL ip_address, int line);
391 #define dns_rdata_aaaa_to_wire(txn, ip_address) dns_rdata_aaaa_to_wire_(txn, ip_address, __LINE__)
392 
393 uint16_t dns_rdata_key_to_wire_(dns_towire_state_t *NONNULL txn,
394                                 unsigned key_type,
395                                 unsigned name_type,
396                                 unsigned signatory,
397                                 srp_key_t *NONNULL key, int line);
398 #define dns_rdata_key_to_wire(txn, key_type, name_type, signatory, key) \
399     dns_rdata_key_to_wire_(txn, key_type, name_type, signatory, key, __LINE__)
400 
401 void dns_rdata_txt_to_wire_(dns_towire_state_t *NONNULL txn, const char *NONNULL txt_record, int line);
402 #define dns_rdata_txt_to_wire(txn, txt_record) dns_rdata_txt_to_wire_(txn, txt_record, __LINE__)
403 
404 void dns_rdata_raw_data_to_wire_(dns_towire_state_t *NONNULL txn,
405                                  const void *NONNULL raw_data, size_t length, int line);
406 #define dns_rdata_raw_data_to_wire(txn, raw_data, length) dns_rdata_raw_data_to_wire_(txn, raw_data, length, __LINE__)
407 
408 void dns_edns0_header_to_wire_(dns_towire_state_t *NONNULL txn,
409                                int mtu, int xrcode, int version, int DO, int line);
410 #define dns_edns0_header_to_wire(txn, mtu, xrcode, version, DO) \
411     dns_edns0_header_to_wire_(txn, mtu, xrcode, version, DO, __LINE__)
412 
413 void dns_edns0_option_begin_(dns_towire_state_t *NONNULL txn, int line);
414 #define dns_edns0_option_begin(txn) dns_edns0_option_begin_(txn, __LINE__)
415 
416 void dns_edns0_option_end_(dns_towire_state_t *NONNULL txn, int line);
417 #define dns_edns0_option_end(txn) dns_edns0_option_end_(txn, __LINE__)
418 
419 void dns_sig0_signature_to_wire_(dns_towire_state_t *NONNULL txn,
420                                  srp_key_t *NONNULL key, uint16_t key_tag,
421                                  dns_name_pointer_t *NONNULL signer, const char *NONNULL signer_hostname,
422                                  const char *NONNULL signer_domain, int line);
423 #define dns_sig0_signature_to_wire(txn, key, key_tag, signer, signer_hostname, signer_domain) \
424     dns_sig0_signature_to_wire_(txn, key, key_tag, signer, signer_hostname, signer_domain, __LINE__)
425 
426 int dns_send_to_server(dns_transaction_t *NONNULL txn,
427                        const char *NONNULL anycast_address, uint16_t port,
428                        dns_response_callback_t NONNULL callback);
429 
430 // fromwire.c:
431 dns_label_t *NULLABLE dns_label_parse(const uint8_t *NONNULL buf, unsigned mlen, unsigned *NONNULL offp);
432 bool dns_opt_parse(dns_edns0_t *NONNULL *NULLABLE ret, dns_rr_t *NONNULL rrset);
433 bool dns_name_parse(dns_label_t *NONNULL *NULLABLE ret, const uint8_t *NONNULL buf, unsigned len,
434                     unsigned *NONNULL offp, unsigned base);
435 bool dns_u8_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint8_t *NONNULL ret);
436 bool dns_u16_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint16_t *NONNULL ret);
437 bool dns_u32_parse(const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, uint32_t *NONNULL ret);
438 bool dns_rdata_parse_data(dns_rr_t *NONNULL rr, const uint8_t *NONNULL buf, unsigned *NONNULL offp,
439                           unsigned target, unsigned rdlen, unsigned rrstart);
440 bool dns_rr_parse(dns_rr_t *NONNULL rrset,
441                   const uint8_t *NONNULL buf, unsigned len, unsigned *NONNULL offp, bool rrdata_permitted);
442 void dns_name_free(dns_label_t *NONNULL name);
443 void dns_rrdata_free(dns_rr_t *NONNULL rr);
444 void dns_message_free(dns_message_t *NONNULL message);
445 bool dns_rdata_parse_data(dns_rr_t *NONNULL rr, const uint8_t *NONNULL buf, unsigned *NONNULL offp,
446                           unsigned target, unsigned rdlen, unsigned rrstart);
447 bool dns_wire_parse(dns_message_t *NONNULL *NULLABLE ret, dns_wire_t *NONNULL message, unsigned len);
448 bool dns_names_equal(dns_label_t *NONNULL name1, dns_label_t *NONNULL name2);
449 
450 // wireutils.c
451 dns_name_t *NULLABLE dns_name_copy(dns_name_t *NONNULL original);
452 void dns_u48_to_wire_(dns_towire_state_t *NONNULL txn, uint64_t val, int line);
453 #define dns_u48_to_wire(txn, val) dns_u48_to_wire_(txn, val, __LINE__)
454 
455 void dns_concatenate_name_to_wire_(dns_towire_state_t *NONNULL towire,
456                                    dns_name_t *NULLABLE labels_prefix,
457                                    const char *NULLABLE prefix, const char *NULLABLE suffix, int line);
458 #define dns_concatenate_name_to_wire(txn, labels_prefix, prefix, suffix) \
459     dns_concatenate_name_to_wire_(txn, labels_prefix, prefix, suffix, __LINE__)
460 
461 const char *NONNULL dns_name_print_to_limit(dns_name_t *NONNULL name, dns_name_t *NULLABLE limit, char *NULLABLE buf,
462                                             int bufmax);
463 const char *NONNULL dns_name_print(dns_name_t *NONNULL name, char *NONNULL buf, int bufmax);
464 bool dns_labels_equal(const char *NONNULL label1, const char *NONNULL label2, size_t len);
465 bool dns_names_equal_text(dns_label_t *NONNULL name1, const char *NONNULL name2);
466 size_t dns_name_wire_length(dns_label_t *NONNULL name);
467 size_t dns_name_to_wire_canonical(uint8_t *NONNULL buf, size_t max, dns_label_t *NONNULL name);
468 dns_name_t *NULLABLE dns_pres_name_parse(const char *NONNULL pname);
469 dns_name_t *NULLABLE dns_name_subdomain_of(dns_name_t *NONNULL name, dns_name_t *NONNULL domain);
470 const char *NONNULL dns_rcode_name(int rcode);
471 bool dns_keys_rdata_equal(dns_rr_t *NONNULL key1, dns_rr_t *NONNULL key2);
472 
473 #endif // _DNS_MSG_H
474 
475 // Local Variables:
476 // mode: C
477 // tab-width: 4
478 // c-file-style: "bsd"
479 // c-basic-offset: 4
480 // fill-column: 108
481 // indent-tabs-mode: nil
482 // End:
483