domain 0.12.0

A DNS library for Rust.
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! Zone tree related types.

use core::future::{ready, Future};
use core::pin::Pin;
use core::task::{Context, Poll};

use std::boxed::Box;
use std::collections::{hash_map, HashMap};
use std::ops;
use std::sync::Arc;
use std::vec::Vec;

use bytes::Bytes;
use futures_util::stream;
use serde::{Deserialize, Serialize};
use tracing::trace;

use super::traits::{ZoneDiff, ZoneDiffItem};
use crate::base::name::Name;
use crate::base::rdata::RecordData;
use crate::base::record::Record;
use crate::base::{iana::Rtype, Ttl};
use crate::base::{Serial, ToName};
use crate::rdata::ZoneRecordData;

//------------ Type Aliases --------------------------------------------------

/// A [`Bytes`] backed [`Name`].
pub type StoredName = Name<Bytes>;

/// A [`Bytes`] backed [`ZoneRecordData`].
pub type StoredRecordData = ZoneRecordData<Bytes, StoredName>;

/// A [`Bytes`] backed [`Record`].`
pub type StoredRecord = Record<StoredName, StoredRecordData>;

//------------ SharedRr ------------------------------------------------------

/// A cheaply clonable resource record.
///
/// A [`Bytes`] backed resource record which is cheap to [`Clone`] because
/// [`Bytes`] is cheap to clone.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct SharedRr {
    ttl: Ttl,
    data: StoredRecordData,
}

impl SharedRr {
    /// Create a new [`SharedRr`] instance.
    pub fn new(ttl: Ttl, data: StoredRecordData) -> Self {
        SharedRr { ttl, data }
    }

    /// Gets the type of this resource record.
    pub fn rtype(&self) -> Rtype {
        self.data.rtype()
    }

    /// Gets the TTL of this resource record.
    pub fn ttl(&self) -> Ttl {
        self.ttl
    }

    /// Gets a reference to the data of this resource record.
    pub fn data(&self) -> &StoredRecordData {
        &self.data
    }
}

impl From<StoredRecord> for SharedRr {
    fn from(record: StoredRecord) -> Self {
        SharedRr {
            ttl: record.ttl(),
            data: record.into_data(),
        }
    }
}

//------------ Rrset ---------------------------------------------------------

/// A set of related resource records for use with [`Zone`]s.
///
/// This type should be used to create and edit one or more resource records
/// for use with a [`Zone`]. RRset records should all have the same type and
/// TTL but differing data, as defined by [RFC 9499 section 5.1.3].
///
/// [`Zone`]: crate::zonetree::Zone
/// [RFC 9499 section 5.1.3]:
///     https://datatracker.ietf.org/doc/html/rfc9499#section-5-1.3
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Rrset {
    rtype: Rtype,
    ttl: Ttl,
    data: Vec<StoredRecordData>,
}

impl Rrset {
    /// Creates a new RRset.
    pub fn new(rtype: Rtype, ttl: Ttl) -> Self {
        Rrset {
            rtype,
            ttl,
            data: Vec::new(),
        }
    }

    /// Gets the common type of each record in the RRset.
    pub fn rtype(&self) -> Rtype {
        self.rtype
    }

    /// Gets the common TTL of each record in the RRset.
    pub fn ttl(&self) -> Ttl {
        self.ttl
    }

    /// Gets the data for each record in the RRset.
    pub fn data(&self) -> &[StoredRecordData] {
        &self.data
    }

    /// Returns true if this RRset has no resource records, false otherwise.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Gets the first RRset record, if any.
    pub fn first(&self) -> Option<SharedRr> {
        self.data.first().map(|data| SharedRr {
            ttl: self.ttl,
            data: data.clone(),
        })
    }

    /// Changesthe TTL of every record in the RRset.
    pub fn set_ttl(&mut self, ttl: Ttl) {
        self.ttl = ttl;
    }

    /// Limits the TTL of every record in the RRSet.
    ///
    /// If the TTL currently exceeds the given limit it will be set to the
    /// limit.
    pub fn limit_ttl(&mut self, ttl: Ttl) {
        if self.ttl > ttl {
            self.ttl = ttl
        }
    }

    /// Adds a resource record to the RRset.
    ///
    /// # Panics
    ///
    /// This function will panic if the provided record data is for a
    /// different type than the RRset.
    pub fn push_data(&mut self, data: StoredRecordData) {
        assert_eq!(data.rtype(), self.rtype);
        self.data.push(data);
    }

    /// Adds a resource record to the RRset, limiting the TTL to that of the
    /// new record.
    ///
    /// See [`Self::limit_ttl`] and [`Self::push_data`].
    pub fn push_record(&mut self, record: StoredRecord) {
        self.limit_ttl(record.ttl());
        self.push_data(record.into_data());
    }

    /// Converts this [`Rrset`] to an [`SharedRrset`].
    pub fn into_shared(self) -> SharedRrset {
        SharedRrset::new(self)
    }
}

impl From<StoredRecord> for Rrset {
    fn from(record: StoredRecord) -> Self {
        Rrset {
            rtype: record.rtype(),
            ttl: record.ttl(),
            data: vec![record.into_data()],
        }
    }
}

//------------ SharedRrset ---------------------------------------------------

/// An RRset behind an [`Arc`] for use with [`Zone`]s.
///
/// See [`Rrset`] for more information.
///
/// [`Zone`]: crate::zonetree::Zone
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SharedRrset(Arc<Rrset>);

impl SharedRrset {
    /// Creates a new RRset.
    pub fn new(rrset: Rrset) -> Self {
        SharedRrset(Arc::new(rrset))
    }

    /// Gets a reference to the inner [`Rrset`].
    pub fn as_rrset(&self) -> &Rrset {
        self.0.as_ref()
    }
}

//--- Deref, AsRef, Borrow

impl ops::Deref for SharedRrset {
    type Target = Rrset;

    fn deref(&self) -> &Self::Target {
        self.as_rrset()
    }
}

impl AsRef<Rrset> for SharedRrset {
    fn as_ref(&self) -> &Rrset {
        self.as_rrset()
    }
}

//--- Deserialize and Serialize

impl<'de> Deserialize<'de> for SharedRrset {
    fn deserialize<D: serde::Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Self, D::Error> {
        Rrset::deserialize(deserializer).map(SharedRrset::new)
    }
}

impl Serialize for SharedRrset {
    fn serialize<S: serde::Serializer>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        self.as_rrset().serialize(serializer)
    }
}

//------------ ZoneCut -------------------------------------------------------

/// The representation of a zone cut within a zone tree.
#[derive(Clone, Debug)]
pub struct ZoneCut {
    /// The owner name where the zone cut occurs.
    pub name: StoredName,

    /// The NS record at the zone cut.
    pub ns: SharedRrset,

    /// The DS record at the zone cut (optional).
    pub ds: Option<SharedRrset>,

    /// Zero or more glue records at the zone cut.
    pub glue: Vec<StoredRecord>,
}

//------------ InMemoryZoneDiffBuilder ----------------------------------------

/// An [`InMemoryZoneDiff`] builder.
///
/// Removes are assumed to occur before adds.
#[derive(Debug, Default)]
pub struct InMemoryZoneDiffBuilder {
    /// The records added to the Zone.
    added: HashMap<(StoredName, Rtype), SharedRrset>,

    /// The records removed from the Zone.
    removed: HashMap<(StoredName, Rtype), SharedRrset>,
}

impl InMemoryZoneDiffBuilder {
    /// Creates a new instance of the builder.
    pub fn new() -> Self {
        Default::default()
    }

    /// Record in the diff that a resource record was added.
    pub fn add(
        &mut self,
        owner: StoredName,
        rtype: Rtype,
        rrset: SharedRrset,
    ) {
        self.added.insert((owner, rtype), rrset);
    }

    /// Record in the diff that a resource record was removed.
    pub fn remove(
        &mut self,
        owner: StoredName,
        rtype: Rtype,
        rrset: SharedRrset,
    ) {
        self.removed.insert((owner, rtype), rrset);
    }

    /// Exchange this builder instnace for an immutable [`ZoneDiff`].
    ///
    /// The start serial should be the zone version to which the diffs should
    /// be applied. The end serial denotes the zone version that results from
    /// applying this diff.
    ///
    /// Note: No check is currently done that the start and end serials match
    /// the SOA records in the removed and added records contained within the
    /// diff.
    pub fn build(self) -> Result<InMemoryZoneDiff, ZoneDiffError> {
        InMemoryZoneDiff::new(self.added, self.removed)
    }
}

//------------ InMemoryZoneDiff -----------------------------------------------

/// The differences between one serial and another for a DNS zone.
///
/// Removes are assumed to occur before adds.
#[derive(Clone, Debug)]
pub struct InMemoryZoneDiff {
    /// The serial number of the zone which was modified.
    pub start_serial: Serial,

    /// The serial number of the zone that resulted from the modifications.
    pub end_serial: Serial,

    /// The RRsets added to the zone.
    pub added: Arc<HashMap<(StoredName, Rtype), SharedRrset>>,

    /// The RRsets removed from the zone.
    pub removed: Arc<HashMap<(StoredName, Rtype), SharedRrset>>,
}

impl InMemoryZoneDiff {
    /// Creates a new immutable zone diff.
    ///
    /// Returns `Err(ZoneDiffError::MissingStartSoa)` If the removed records
    /// do not include a zone SOA.
    ///
    /// Returns `Err(ZoneDiffError::MissingEndSoa)` If the added records do
    /// not include a zone SOA.
    ///
    /// Returns Ok otherwise.
    fn new(
        added: HashMap<(Name<Bytes>, Rtype), SharedRrset>,
        removed: HashMap<(Name<Bytes>, Rtype), SharedRrset>,
    ) -> Result<Self, ZoneDiffError> {
        // Determine the old and new SOA serials by looking at the added and
        // removed records.
        let start_serial = removed
            .iter()
            .find_map(|((_, rtype), rrset)| {
                if *rtype == Rtype::SOA {
                    if let Some(ZoneRecordData::Soa(soa)) =
                        rrset.data().first()
                    {
                        return Some(soa.serial());
                    }
                }
                None
            })
            .ok_or(ZoneDiffError::MissingStartSoa)?;

        let end_serial = added
            .iter()
            .find_map(|((_, rtype), rrset)| {
                if *rtype == Rtype::SOA {
                    if let Some(ZoneRecordData::Soa(soa)) =
                        rrset.data().first()
                    {
                        return Some(soa.serial());
                    }
                }
                None
            })
            .ok_or(ZoneDiffError::MissingEndSoa)?;

        if start_serial == end_serial || end_serial < start_serial {
            trace!("Diff construction error: serial {start_serial} -> serial {end_serial}:\nremoved: {removed:#?}\nadded: {added:#?}\n");
            return Err(ZoneDiffError::InvalidSerialRange);
        }

        trace!(
            "Built diff from serial {start_serial} to serial {end_serial}"
        );

        Ok(Self {
            start_serial,
            end_serial,
            added: added.into(),
            removed: removed.into(),
        })
    }
}

//--- impl ZoneDiff

impl<'a> ZoneDiffItem for (&'a (StoredName, Rtype), &'a SharedRrset) {
    fn key(&self) -> &(StoredName, Rtype) {
        self.0
    }

    fn value(&self) -> &SharedRrset {
        self.1
    }
}

impl ZoneDiff for InMemoryZoneDiff {
    type Item<'a>
        = (&'a (StoredName, Rtype), &'a SharedRrset)
    where
        Self: 'a;

    type Stream<'a>
        = futures_util::stream::Iter<
        hash_map::Iter<'a, (StoredName, Rtype), SharedRrset>,
    >
    where
        Self: 'a;

    fn start_serial(
        &self,
    ) -> Pin<Box<dyn Future<Output = Serial> + Send + '_>> {
        Box::pin(ready(self.start_serial))
    }

    fn end_serial(
        &self,
    ) -> Pin<Box<dyn Future<Output = Serial> + Send + '_>> {
        Box::pin(ready(self.end_serial))
    }

    fn added(&self) -> Self::Stream<'_> {
        stream::iter(self.added.iter())
    }

    fn removed(&self) -> Self::Stream<'_> {
        stream::iter(self.removed.iter())
    }

    fn get_added(
        &self,
        name: impl ToName,
        rtype: Rtype,
    ) -> Pin<Box<dyn Future<Output = Option<&SharedRrset>> + Send + '_>> {
        Box::pin(ready(self.added.get(&(name.to_name(), rtype))))
    }

    fn get_removed(
        &self,
        name: impl ToName,
        rtype: Rtype,
    ) -> Pin<Box<dyn Future<Output = Option<&SharedRrset>> + Send + '_>> {
        Box::pin(ready(self.removed.get(&(name.to_name(), rtype))))
    }
}

/// The item type used by [`EmptyZoneDiff`].
pub struct EmptyZoneDiffItem;

impl ZoneDiffItem for EmptyZoneDiffItem {
    fn key(&self) -> &(StoredName, Rtype) {
        unreachable!()
    }

    fn value(&self) -> &SharedRrset {
        unreachable!()
    }
}

/// The stream type used by [`EmptyZoneDiff`].
#[derive(Debug)]
pub struct EmptyZoneDiffStream;

impl futures_util::stream::Stream for EmptyZoneDiffStream {
    type Item = EmptyZoneDiffItem;

    fn poll_next(
        self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
    ) -> Poll<Option<Self::Item>> {
        Poll::Ready(None)
    }
}

/// A [`ZoneDiff`] implementation that is always empty.
///
/// Useful when a [`ZoneDiff`] type is needed in a type declaration but for use
/// by a type that does not support zone difference data.
#[derive(Debug)]
pub struct EmptyZoneDiff;

impl ZoneDiff for EmptyZoneDiff {
    type Item<'a>
        = EmptyZoneDiffItem
    where
        Self: 'a;

    type Stream<'a>
        = EmptyZoneDiffStream
    where
        Self: 'a;

    fn start_serial(
        &self,
    ) -> Pin<Box<dyn Future<Output = Serial> + Send + '_>> {
        Box::pin(ready(Serial(0)))
    }

    fn end_serial(
        &self,
    ) -> Pin<Box<dyn Future<Output = Serial> + Send + '_>> {
        Box::pin(ready(Serial(0)))
    }

    fn added(&self) -> Self::Stream<'_> {
        EmptyZoneDiffStream
    }

    fn removed(&self) -> Self::Stream<'_> {
        EmptyZoneDiffStream
    }

    fn get_added(
        &self,
        _name: impl ToName,
        _rtype: Rtype,
    ) -> Pin<Box<dyn Future<Output = Option<&SharedRrset>> + Send + '_>> {
        Box::pin(ready(None))
    }

    fn get_removed(
        &self,
        _name: impl ToName,
        _rtype: Rtype,
    ) -> Pin<Box<dyn Future<Output = Option<&SharedRrset>> + Send + '_>> {
        Box::pin(ready(None))
    }
}

//------------ ZoneDiffError --------------------------------------------------

/// Creating a [`ZoneDiff`] failed for some reason.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ZoneDiffError {
    /// Missing start SOA.
    ///
    /// A zone diff requires a starting SOA.
    MissingStartSoa,

    /// Missing end SOA.
    ///
    /// A zone diff requires a starting SOA.
    MissingEndSoa,

    /// End SOA serial is equal to or less than the start SOA serial.
    InvalidSerialRange,
}

//--- Display

impl std::fmt::Display for ZoneDiffError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ZoneDiffError::MissingStartSoa => f.write_str("MissingStartSoa"),
            ZoneDiffError::MissingEndSoa => f.write_str("MissingEndSoa"),
            ZoneDiffError::InvalidSerialRange => {
                f.write_str("InvalidSerialRange")
            }
        }
    }
}

//------------ ZoneUpdate -----------------------------------------------------

/// An update to be applied to a [`Zone`].
///
/// # Design
///
/// The variants of this enum are modelled after the way the AXFR and IXFR
/// protocols represent updates to zones.
///
/// AXFR responses can be represented as a sequence of
/// [`ZoneUpdate::AddRecord`]s.
///
/// IXFR responses can be represented as a sequence of batches, each
/// consisting of:
/// - [`ZoneUpdate::BeginBatchDelete`]
/// - [`ZoneUpdate::DeleteRecord`]s _(zero or more)_
/// - [`ZoneUpdate::BeginBatchAdd`]
/// - [`ZoneUpdate::AddRecord`]s _(zero or more)_
///
/// Both AXFR and IXFR responses encoded using this enum are terminated by a
/// final [`ZoneUpdate::Finished`].
///
/// # Use within this crate
///  
/// [`XfrResponseInterpreter`] can convert received XFR responses into
/// sequences of [`ZoneUpdate`]s. These can then be consumed by a
/// [`ZoneUpdater`] to effect changes to an existing [`Zone`].
///
/// # Future extensions
///
/// This enum is marked as `#[non_exhaustive]` to permit addition of more
/// update operations in future, e.g. to support RFC 2136 Dynamic Updates
/// operations.
///
/// [`XfrResponseInterpreter`]:
///     crate::net::xfr::protocol::XfrResponseInterpreter
/// [`Zone`]: crate::zonetree::zone::Zone
/// [`ZoneUpdater`]: crate::zonetree::update::ZoneUpdater
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ZoneUpdate<R> {
    /// Delete all records in the zone.
    DeleteAllRecords,

    /// Delete record R from the zone.
    DeleteRecord(R),

    /// Add record R to the zone.
    AddRecord(R),

    /// Start a batch delete for the version of the zone with the given SOA
    /// record.
    ///
    /// If not already in batching mode, this signals the start of batching
    /// mode. In batching mode one or more batches of updates will be
    /// signalled, each consisting of the sequence:
    ///
    /// - ZoneUpdate::BeginBatchDelete
    /// - ZoneUpdate::DeleteRecord (zero or more)
    /// - ZoneUpdate::BeginBatchAdd
    /// - ZoneUpdate::AddRecord (zero or more)
    ///
    /// Batching mode can only be terminated by `UpdateComplete` or
    /// `UpdateIncomplete`.
    ///
    /// Batching mode makes updates more predictable for the receiver to work
    /// with by limiting the updates that can be signalled next, enabling
    /// receiver logic to be simpler and more efficient.
    ///
    /// The record must be a SOA record that matches the SOA record of the
    /// zone version in which the subsequent [`ZoneUpdate::DeleteRecord`]s
    /// should be deleted.
    BeginBatchDelete(R),

    /// Start a batch add for the version of the zone with the given SOA
    /// record.
    ///
    /// This can only be signalled when already in batching mode, i.e. when
    /// `BeginBatchDelete` has already been signalled.
    ///
    /// The record must be the SOA record to use for the new version of the
    /// zone under which the subsequent [`ZoneUpdate::AddRecord`]s will be
    /// added.
    ///
    /// See `BeginBatchDelete` for more information.
    BeginBatchAdd(R),

    /// In progress updates for the zone can now be finalized.
    ///
    /// This signals the end of a group of related changes for the given SOA
    /// record of the zone.
    ///
    /// For example this could be used to trigger an atomic commit of a set of
    /// related pending changes.
    Finished(R),
}

//--- Display

impl<R> std::fmt::Display for ZoneUpdate<R> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            ZoneUpdate::DeleteAllRecords => f.write_str("DeleteAllRecords"),
            ZoneUpdate::DeleteRecord(_) => f.write_str("DeleteRecord"),
            ZoneUpdate::AddRecord(_) => f.write_str("AddRecord"),
            ZoneUpdate::BeginBatchDelete(_) => {
                f.write_str("BeginBatchDelete")
            }
            ZoneUpdate::BeginBatchAdd(_) => f.write_str("BeginBatchAdd"),
            ZoneUpdate::Finished(_) => f.write_str("Finished"),
        }
    }
}