crafter 0.3.0

Packet-level network interaction for Rust tools and agents.
Documentation
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
//! DNS question and resource-record types.

use core::net::{Ipv4Addr, Ipv6Addr};

use crate::error::{CrafterError, Result};

use super::dnssec::DnsTypeBitmaps;
use super::edns::{encode_edns_ttl, EdnsOption};
use super::name::DnsName;
use super::rdata::DnsRecordData;
use super::svcb::SvcParams;
use super::{
    DNS_CLASS_IN, DNS_EDNS_EXTENDED_RCODE_SHIFT, DNS_EDNS_FLAGS_MASK, DNS_EDNS_FLAG_DO,
    DNS_EDNS_VERSION_SHIFT, DNS_TYPE_A, DNS_TYPE_AAAA, DNS_TYPE_CNAME, DNS_TYPE_DNSKEY,
    DNS_TYPE_DS, DNS_TYPE_HTTPS, DNS_TYPE_NSEC, DNS_TYPE_NSEC3, DNS_TYPE_OPT, DNS_TYPE_RRSIG,
    DNS_TYPE_SOA, DNS_TYPE_SRV, DNS_TYPE_SVCB,
};

/// Parsed or constructible DNS question.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DnsQuestion {
    pub(super) name: DnsName,
    pub(super) question_type: u16,
    pub(super) question_class: u16,
}

impl DnsQuestion {
    /// Create a DNS question with an explicit type and IN class.
    pub fn new(name: impl Into<DnsName>, question_type: u16) -> Self {
        Self {
            name: name.into(),
            question_type,
            question_class: DNS_CLASS_IN,
        }
    }

    /// Create an A question.
    pub fn a(name: impl Into<DnsName>) -> Self {
        Self::new(name, DNS_TYPE_A)
    }

    /// Create an AAAA question.
    pub fn aaaa(name: impl Into<DnsName>) -> Self {
        Self::new(name, DNS_TYPE_AAAA)
    }

    /// Set the question class.
    pub fn class(mut self, question_class: u16) -> Self {
        self.question_class = question_class;
        self
    }

    /// Compatibility alias for question type.
    pub fn qtype(mut self, question_type: u16) -> Self {
        self.question_type = question_type;
        self
    }

    /// Compatibility alias for question class.
    pub fn qclass(mut self, question_class: u16) -> Self {
        self.question_class = question_class;
        self
    }

    /// Question name in canonical trailing-dot presentation form.
    ///
    /// Non-text labels are rendered with `\DDD` escapes; use
    /// [`DnsQuestion::dns_name`] or [`DnsQuestion::name_labels`] for the exact
    /// wire bytes.
    pub fn name(&self) -> &str {
        self.name.presentation()
    }

    /// Typed owner name preserving the exact wire labels.
    pub fn dns_name(&self) -> &DnsName {
        &self.name
    }

    /// Exact wire-label bytes of the question name.
    pub fn name_labels(&self) -> &[Vec<u8>] {
        self.name.labels()
    }

    /// Question type value.
    pub const fn question_type(&self) -> u16 {
        self.question_type
    }

    /// Question class value.
    pub const fn question_class(&self) -> u16 {
        self.question_class
    }

    pub(super) fn encoded_len(&self) -> usize {
        self.name.encoded_len() + 4
    }

    pub(super) fn encode(&self, out: &mut Vec<u8>) -> Result<()> {
        self.name.encode(out)?;
        out.extend_from_slice(&self.question_type.to_be_bytes());
        out.extend_from_slice(&self.question_class.to_be_bytes());
        Ok(())
    }
}

/// DNS resource record.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DnsRecord {
    pub(super) name: DnsName,
    pub(super) record_type: u16,
    pub(super) class: u16,
    pub(super) ttl: u32,
    pub(super) data: DnsRecordData,
}

impl DnsRecord {
    /// Create a DNS resource record.
    pub fn new(
        name: impl Into<DnsName>,
        record_type: u16,
        class: u16,
        ttl: u32,
        data: DnsRecordData,
    ) -> Self {
        Self {
            name: name.into(),
            record_type,
            class,
            ttl,
            data,
        }
    }

    /// Create an A answer.
    pub fn a(name: impl Into<DnsName>, address: Ipv4Addr, ttl: u32) -> Self {
        Self::new(
            name,
            DNS_TYPE_A,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::A(address),
        )
    }

    /// Create an AAAA answer.
    pub fn aaaa(name: impl Into<DnsName>, address: Ipv6Addr, ttl: u32) -> Self {
        Self::new(
            name,
            DNS_TYPE_AAAA,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Aaaa(address),
        )
    }

    /// Create a CNAME answer.
    pub fn cname(name: impl Into<DnsName>, target: impl Into<DnsName>, ttl: u32) -> Self {
        Self::new(
            name,
            DNS_TYPE_CNAME,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::name(target),
        )
    }

    /// Create a SOA record (RFC 1035 Section 3.3.13).
    #[allow(clippy::too_many_arguments)]
    pub fn soa(
        name: impl Into<DnsName>,
        ttl: u32,
        mname: impl Into<DnsName>,
        rname: impl Into<DnsName>,
        serial: u32,
        refresh: u32,
        retry: u32,
        expire: u32,
        minimum: u32,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_SOA,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Soa {
                mname: mname.into(),
                rname: rname.into(),
                serial,
                refresh,
                retry,
                expire,
                minimum,
            },
        )
    }

    /// Create a SRV record (RFC 2782).
    pub fn srv(
        name: impl Into<DnsName>,
        ttl: u32,
        priority: u16,
        weight: u16,
        port: u16,
        target: impl Into<DnsName>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_SRV,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Srv {
                priority,
                weight,
                port,
                target: target.into(),
            },
        )
    }

    /// Create a DS (Delegation Signer) record (RFC 4034 Section 5.1).
    ///
    /// The algorithm and digest type stay raw numeric fields and the digest is
    /// carried verbatim; no cryptographic validation is performed.
    pub fn ds(
        name: impl Into<DnsName>,
        ttl: u32,
        key_tag: u16,
        algorithm: u8,
        digest_type: u8,
        digest: impl Into<Vec<u8>>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_DS,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Ds {
                key_tag,
                algorithm,
                digest_type,
                digest: digest.into(),
            },
        )
    }

    /// Create a DNSKEY record (RFC 4034 Section 2.1).
    ///
    /// The flags and algorithm stay raw numeric fields and the public key is
    /// carried verbatim; no cryptographic validation is performed.
    pub fn dnskey(
        name: impl Into<DnsName>,
        ttl: u32,
        flags: u16,
        protocol: u8,
        algorithm: u8,
        public_key: impl Into<Vec<u8>>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_DNSKEY,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Dnskey {
                flags,
                protocol,
                algorithm,
                public_key: public_key.into(),
            },
        )
    }

    /// Create an RRSIG record (RFC 4034 Section 3.1).
    ///
    /// The signer's name is emitted uncompressed and the signature is carried
    /// verbatim; no cryptographic validation is performed.
    #[allow(clippy::too_many_arguments)]
    pub fn rrsig(
        name: impl Into<DnsName>,
        ttl: u32,
        type_covered: u16,
        algorithm: u8,
        labels: u8,
        original_ttl: u32,
        signature_expiration: u32,
        signature_inception: u32,
        key_tag: u16,
        signer_name: impl Into<DnsName>,
        signature: impl Into<Vec<u8>>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_RRSIG,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Rrsig {
                type_covered,
                algorithm,
                labels,
                original_ttl,
                signature_expiration,
                signature_inception,
                key_tag,
                signer_name: signer_name.into(),
                signature: signature.into(),
            },
        )
    }

    /// Create an NSEC record (RFC 4034 Section 4.1).
    ///
    /// The next domain name is emitted uncompressed and the type bitmaps carry
    /// the present RR type values verbatim.
    pub fn nsec(
        name: impl Into<DnsName>,
        ttl: u32,
        next_domain_name: impl Into<DnsName>,
        present_types: impl IntoIterator<Item = u16>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_NSEC,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Nsec {
                next_domain_name: next_domain_name.into(),
                type_bitmaps: DnsTypeBitmaps::from_types(present_types),
            },
        )
    }

    /// Create an NSEC3 record (RFC 5155 Section 3.2).
    ///
    /// The hash algorithm and flags stay raw numeric fields; the salt, next
    /// hashed owner name, and type bitmaps carry their wire bytes verbatim.
    #[allow(clippy::too_many_arguments)]
    pub fn nsec3(
        name: impl Into<DnsName>,
        ttl: u32,
        hash_algorithm: u8,
        flags: u8,
        iterations: u16,
        salt: impl Into<Vec<u8>>,
        next_hashed_owner_name: impl Into<Vec<u8>>,
        present_types: impl IntoIterator<Item = u16>,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_NSEC3,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Nsec3 {
                hash_algorithm,
                flags,
                iterations,
                salt: salt.into(),
                next_hashed_owner_name: next_hashed_owner_name.into(),
                type_bitmaps: DnsTypeBitmaps::from_types(present_types),
            },
        )
    }

    /// Create an SVCB service-binding record (RFC 9460 Section 2.2).
    ///
    /// The target name is emitted uncompressed (and may be the root `.`), and
    /// the SvcParams are sorted into strictly increasing SvcParamKey order. The
    /// SvcParam values are carried verbatim; no resolver or selection behavior is
    /// applied.
    pub fn svcb(
        name: impl Into<DnsName>,
        ttl: u32,
        priority: u16,
        target: impl Into<DnsName>,
        params: SvcParams,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_SVCB,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Svcb {
                priority,
                target: target.into(),
                params,
            },
        )
    }

    /// Create an HTTPS service-binding record (RFC 9460 Section 2.2).
    ///
    /// HTTPS shares the SVCB wire format; the target name is emitted
    /// uncompressed (and may be the root `.`), and the SvcParams are sorted into
    /// strictly increasing SvcParamKey order with their values carried verbatim.
    pub fn https(
        name: impl Into<DnsName>,
        ttl: u32,
        priority: u16,
        target: impl Into<DnsName>,
        params: SvcParams,
    ) -> Self {
        Self::new(
            name,
            DNS_TYPE_HTTPS,
            DNS_CLASS_IN,
            ttl,
            DnsRecordData::Https {
                priority,
                target: target.into(),
                params,
            },
        )
    }

    /// Create an EDNS(0) OPT pseudo-record (RFC 6891 Section 6.1).
    ///
    /// The OPT pseudo-record reuses ordinary resource-record fields with EDNS
    /// meanings: the owner name MUST be root, the CLASS field carries the
    /// requestor's UDP payload size, and the TTL field carries the extended
    /// RCODE, EDNS version, the DO flag, and the Z bits. This builder packs
    /// those EDNS fields into the underlying `class` and `ttl` so the record
    /// still encodes through the normal name/type/class/ttl/rdlength/RDATA path;
    /// inspect or override the raw fields with [`DnsRecord::class`],
    /// [`DnsRecord::ttl`], and the EDNS getters.
    pub fn opt(
        udp_payload_size: u16,
        extended_rcode: u8,
        version: u8,
        dnssec_ok: bool,
        options: Vec<EdnsOption>,
    ) -> Self {
        let ttl = encode_edns_ttl(extended_rcode, version, dnssec_ok, 0);
        Self::new(
            DnsName::root(),
            DNS_TYPE_OPT,
            udp_payload_size,
            ttl,
            DnsRecordData::Opt(options),
        )
    }

    /// Record name in canonical trailing-dot presentation form.
    ///
    /// Non-text labels are rendered with `\DDD` escapes; use
    /// [`DnsRecord::dns_name`] or [`DnsRecord::name_labels`] for the exact wire
    /// bytes.
    pub fn name(&self) -> &str {
        self.name.presentation()
    }

    /// Typed owner name preserving the exact wire labels.
    pub fn dns_name(&self) -> &DnsName {
        &self.name
    }

    /// Exact wire-label bytes of the record name.
    pub fn name_labels(&self) -> &[Vec<u8>] {
        self.name.labels()
    }

    /// Record type value.
    pub const fn record_type(&self) -> u16 {
        self.record_type
    }

    /// Record class value.
    pub const fn class(&self) -> u16 {
        self.class
    }

    /// Record TTL.
    pub const fn ttl(&self) -> u32 {
        self.ttl
    }

    /// Record data.
    pub const fn data(&self) -> &DnsRecordData {
        &self.data
    }

    /// True when this record is an EDNS(0) OPT pseudo-record (TYPE 41).
    pub const fn is_opt(&self) -> bool {
        self.record_type == DNS_TYPE_OPT
    }

    /// EDNS(0) requestor UDP payload size, taken from the OPT CLASS field
    /// (RFC 6891 Section 6.1.2).
    ///
    /// This reads the raw CLASS field; it is only meaningful when
    /// [`DnsRecord::is_opt`] is true. The underlying value stays available
    /// through [`DnsRecord::class`].
    pub const fn edns_udp_payload_size(&self) -> u16 {
        self.class
    }

    /// EDNS(0) EXTENDED-RCODE: the upper 8 bits of the 12-bit extended RCODE,
    /// taken from the OPT TTL field (RFC 6891 Section 6.1.3).
    pub const fn edns_extended_rcode(&self) -> u8 {
        (self.ttl >> DNS_EDNS_EXTENDED_RCODE_SHIFT) as u8
    }

    /// EDNS(0) VERSION, taken from the OPT TTL field (RFC 6891 Section 6.1.3).
    pub const fn edns_version(&self) -> u8 {
        (self.ttl >> DNS_EDNS_VERSION_SHIFT) as u8
    }

    /// EDNS(0) DO ("DNSSEC OK") flag, taken from the OPT TTL field
    /// (RFC 6891 Section 6.1.3; IANA EDNS Header Flags bit 0).
    pub const fn edns_dnssec_ok(&self) -> bool {
        ((self.ttl as u16) & DNS_EDNS_FLAG_DO) != 0
    }

    /// EDNS(0) header flags word (the lower 16 bits of the OPT TTL field,
    /// including the DO bit and the Z bits) returned verbatim.
    pub const fn edns_flags(&self) -> u16 {
        (self.ttl & DNS_EDNS_FLAGS_MASK) as u16
    }

    /// EDNS(0) option list when this record's RDATA is typed as OPT, or `None`
    /// for any other record data (including an OPT type whose RDATA decoded as
    /// raw bytes).
    pub fn edns_options(&self) -> Option<&[EdnsOption]> {
        match &self.data {
            DnsRecordData::Opt(options) => Some(options),
            _ => None,
        }
    }

    pub(super) fn encoded_len(&self) -> usize {
        self.name.encoded_len() + 10 + self.data.encoded_len()
    }

    pub(super) fn encode(&self, out: &mut Vec<u8>) -> Result<()> {
        self.name.encode(out)?;
        out.extend_from_slice(&self.record_type.to_be_bytes());
        out.extend_from_slice(&self.class.to_be_bytes());
        out.extend_from_slice(&self.ttl.to_be_bytes());

        let mut rdata = Vec::with_capacity(self.data.encoded_len());
        self.data.encode(self.record_type, &mut rdata)?;
        let rdlength = u16::try_from(rdata.len()).map_err(|_| {
            CrafterError::invalid_field_value("dns.rdlength", "record data exceeds 65535 bytes")
        })?;
        out.extend_from_slice(&rdlength.to_be_bytes());
        out.extend_from_slice(&rdata);
        Ok(())
    }
}