1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Basic DNS.
//!
//! This module provides the essential types and functionality for working
//! with DNS. In particular, it allows building and parsing DNS messages to
//! and from the wire format.
//!
//! This provides a mid-level and low-level API. It guides users towards the
//! most efficient solutions for their needs, and (where necessary) provides
//! fallbacks that trade efficiency for flexibility and/or ergonomics.
//!
//! # A quick reference on types
//!
//! [`Message`] is the top-level type, representing a whole DNS message. It
//! stores data in the wire format, making it trivial to parse into or build
//! from. It can provide direct access to the message [`Header`], and the
//! questions and records within it (collectively called [`MessageItem`]s) can
//! be parsed/traversed using [`Message::parse()`].
//!
//! [`Question`] and [`Record`] are exactly what they look like, and are
//! simple `struct`s so they can be constructed and inspected easily. They
//! are generic over a _domain name type_ (discussed below), which you will
//! need to pick explicitly. [`Record`] is also generic over the record data
//! type; you probably want [`new::rdata::RecordData`]. See the documentation
//! on [`Record`] and [`new::rdata`] for more information.
//!
//! [`new::rdata`]: crate::new::rdata
//! [`new::rdata::RecordData`]: crate::new::rdata::RecordData
//!
//! The [`name`] module provides various types that represent domain
//! names, and describes the situations each type is most appropriate
//! for. As a quick summary: try to use [`RevNameBuf`] by default, or
//! <code>Box<[RevName]></code> if lots of domain names need to be
//! stored. If DNSSEC canonical ordering is necessary, use [`NameBuf`] or
//! <code>Box<[Name]></code> respectively. There are more efficient
//! alternatives in some cases; see [`name`].
//!
//! [Name]: name::Name
//! [RevName]: name::RevName
//! [`NameBuf`]: name::NameBuf
//! [`RevNameBuf`]: name::RevNameBuf
//!
//! # Parsing DNS messages
//!
//! The [`parse`] module provides mid-level and low-level APIs for parsing
//! DNS messages from the wire format. To parse the questions and records in
//! a [`Message`], use [`Message::parse()`]. To parse a message (including
//! questions and records) from bytes, use [`MessageParser::new()`].
//!
//! [`MessageParser::new()`]: parse::MessageParser::new()
//!
//! ```
//! # use domain::new::base::MessageItem;
//! # use domain::new::base::parse::MessageParser;
//! #
//! // The bytes to be parsed.
//! let bytes: &[u8] = &[
//! // The message header.
//! 0, 42, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1,
//! // A question: www.example.org. A IN
//! 3, b'w', b'w', b'w',
//! 7, b'e', b'x', b'a', b'm', b'p', b'l', b'e',
//! 3, b'o', b'r', b'g', 0,
//! 0, 1, 0, 1,
//! // An answer: www.example.org. A IN 3600 127.0.0.1
//! 192, 12, 0, 1, 0, 1, 0, 0, 14, 16, 0, 4, 127, 0, 0, 1,
//! // An OPT record.
//! 0, 0, 41, 4, 208, 0, 0, 128, 0, 0, 12,
//! // An EDNS client cookie.
//! 0, 10, 0, 8, 6, 148, 57, 104, 176, 18, 234, 57,
//! ];
//!
//! // Construct a 'MessageParser' directly from bytes.
//! let Ok(mut message) = MessageParser::new(bytes) else {
//! panic!("'bytes' was too small to be a valid 'Message'")
//! };
//! println!("Header: {:?}", message.header());
//! while let Some(item) = message.next() {
//! let Ok(item) = item else {
//! panic!("Could not parse a message item (at offset {})",
//! message.offset());
//! };
//!
//! match item {
//! MessageItem::Question(question) => {
//! println!("Got a question: {question:?}");
//! }
//! MessageItem::Answer(answer) => {
//! println!("Got an answer record: {answer:?}");
//! }
//! MessageItem::Authority(authority) => {
//! println!("Got an authority record: {authority:?}");
//! }
//! MessageItem::Additional(additional) => {
//! println!("Got an additional record: {additional:?}");
//! }
//! MessageItem::Edns(edns) => {
//! println!("Got an EDNS record: {edns:?}");
//! }
//! }
//! }
//! ```
//!
//! # Building DNS messages
//!
//! The [`build`] module provides mid-level and low-level APIs for building
//! DNS messages in the wire format. [`MessageBuilder`] is the primary entry
//! point; it writes into a user-provided byte buffer. To begin building a
//! DNS message, use [`MessageBuilder::new()`].
//!
//! [`MessageBuilder`]: build::MessageBuilder
//! [`MessageBuilder::new()`]: build::MessageBuilder::new()
//!
//! ```
//! use domain::new::base::{Header, HeaderFlags, Message, Question, QType, QClass};
//! use domain::new::base::build::{AsBytes, MessageBuilder, NameCompressor};
//! use domain::new::base::name::RevNameBuf;
//! use domain::new::base::wire::U16;
//!
//! // Initialize a DNS message builder.
//! let mut buffer = [0u8; 512];
//! let mut compressor = NameCompressor::default();
//! let mut builder = MessageBuilder::new(
//! &mut buffer,
//! &mut compressor,
//! // Select a randomized ID here.
//! U16::new(1234),
//! // A recursive query for authoritative data.
//! *HeaderFlags::default()
//! .set_qr(false)
//! .set_opcode(0)
//! .set_aa(true)
//! .set_rd(true));
//!
//! // Add a question for an A record.
//! builder.push_question(&Question {
//! qname: "www.example.org".parse::<RevNameBuf>().unwrap(),
//! qtype: QType::A,
//! qclass: QClass::IN,
//! }).unwrap();
//!
//! // Use the built message (e.g. send it).
//! let message: &mut Message = builder.finish();
//! let bytes: &[u8] = message.as_bytes();
//! # let _ = bytes;
//! ```
//!
//! # Representing variable-length DNS data
//!
//! In order to efficiently serialize and deserialize DNS messages, and to be
//! easier to approach for users already familiar with DNS, this module
//! structures its DNS types to match the underlying wire format.
//!
//! Because many elements of DNS messages have variable-length encodings in
//! the wire format, this module relies on Rust's language support for
//! _dynamically sized types_ (DSTs) to represent them. The top-level
//! [`Message`] type, [`CharStr`], [`Name`], etc. are all DSTs.
//!
//! [`Name`]: name::Name
//!
//! DSTs cannot be passed around by value because the compiler needs to know
//! (at compile-time) how much stack space to allocate for them. As such, a
//! DST has to be held indirectly, by reference or in a container like
//! [`Box`]. The former work well in "short-term" contexts (e.g. within a
//! function), while the latter are necessary in long-term contexts.
//!
//! [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
//!
//! Container types that implement [`UnsizedCopyFrom`] automatically work with
//! any [`UnsizedCopy`] types. This trait allows DSTs to be copied into such
//! container types, which is especially useful to store a DST for long-term
//! use. It is already implemented for [`Box`], [`Arc`], [`Vec`], etc., and
//! users can implement it on their own container types too.
//!
//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
//! [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
//! [`UnsizedCopy`]: crate::utils::dst::UnsizedCopy
//! [`UnsizedCopyFrom`]: crate::utils::dst::UnsizedCopyFrom
//--- DNS messages
pub use ;
pub use ;
pub use ;
//--- Elements of DNS messages
pub use ;
pub use Serial;
//--- Wire format
//--- Compatibility exports
/// A compatibility module with [`domain::base`].
///
/// This re-exports a large part of the `new::base` API surface using the same
/// import paths as the old `base` module. It is a stopgap measure to help
/// users port existing code over to `new::base`. Every export comes with a
/// deprecation message to help users switch to the right tools.