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
//! Domain name-related traits.
//!
//! This is a private module. Its public traits are re-exported by the parent.

use super::chain::{Chain, LongChainError};
use super::dname::Dname;
use super::label::Label;
use super::relative::RelativeDname;
#[cfg(feature = "bytes")]
use bytes::Bytes;
use core::cmp;
use core::convert::Infallible;
use octseq::builder::{
    infallible, BuilderAppendError, EmptyBuilder, FreezeBuilder, FromBuilder,
    OctetsBuilder, ShortBuf,
};
#[cfg(feature = "std")]
use std::borrow::Cow;

//------------ ToLabelIter ---------------------------------------------------

/// A type that can produce an iterator over its labels.
///
/// This trait is used as a trait bound for both [`ToDname`] and
/// [`ToRelativeDname`]. It is separate since it has to be generic over the
/// lifetime of the label reference but we don’t want to have this lifetime
/// parameter pollute those traits.
///
/// [`ToDname`]: trait.ToDname.html
/// [`ToRelativeDname`]: trait ToRelativeDname.html
#[allow(clippy::len_without_is_empty)]
pub trait ToLabelIter {
    /// The type of the iterator over the labels.
    ///
    /// This iterator types needs to be double ended so that we can deal with
    /// name suffixes. It needs to be cloneable to be able to cascade over
    /// parents of a name.
    type LabelIter<'a>: Iterator<Item = &'a Label>
        + DoubleEndedIterator
        + Clone
    where
        Self: 'a;

    /// Returns an iterator over the labels.
    fn iter_labels(&self) -> Self::LabelIter<'_>;

    /// Returns the length in octets of the encoded name.
    fn compose_len(&self) -> u16 {
        self.iter_labels().map(|label| label.compose_len()).sum()
    }

    /// Determines whether `base` is a prefix of `self`.
    fn starts_with<N: ToLabelIter + ?Sized>(&self, base: &N) -> bool {
        let mut self_iter = self.iter_labels();
        let mut base_iter = base.iter_labels();
        loop {
            match (self_iter.next(), base_iter.next()) {
                (Some(sl), Some(bl)) => {
                    if sl != bl {
                        return false;
                    }
                }
                (_, None) => return true,
                (None, Some(_)) => return false,
            }
        }
    }

    /// Determines whether `base` is a suffix of `self`.
    fn ends_with<N: ToLabelIter + ?Sized>(&self, base: &N) -> bool {
        let mut self_iter = self.iter_labels();
        let mut base_iter = base.iter_labels();
        loop {
            match (self_iter.next_back(), base_iter.next_back()) {
                (Some(sl), Some(bl)) => {
                    if sl != bl {
                        return false;
                    }
                }
                (_, None) => return true,
                (None, Some(_)) => return false,
            }
        }
    }
}

impl<'r, N: ToLabelIter + ?Sized> ToLabelIter for &'r N {
    type LabelIter<'a> = N::LabelIter<'a> where 'r: 'a, N: 'a;

    fn iter_labels(&self) -> Self::LabelIter<'_> {
        (*self).iter_labels()
    }
}

//------------ ToDname -------------------------------------------------------

/// A type that represents an absolute domain name.
///
/// An absolute domain name is a sequence of labels where the last label is
/// the root label and where the wire-format representation is not longer than
/// 255 characters. Implementers of this trait need to provide access to the
/// label sequence via an iterator and know how to compose the wire-format
/// representation into a buffer.
///
/// The most common types implementing this trait are [`Dname`],
/// [`ParsedDname`], and [`Chain<L, R>`] where `R` is `ToDname` itself.
///
/// [`Chain<L, R>`]: struct.Chain.html
/// [`Dname`]: struct.Dname.html
/// [`ParsedDname`]: struct.ParsedDname.html
pub trait ToDname: ToLabelIter {
    /// Converts the name into a single, uncompressed name.
    ///
    /// The default implementation provided by the trait iterates over the
    /// labels of the name and adds them one by one to [`Dname`]. This will
    /// work for any name but an optimized implementation can be provided for
    /// some types of names.
    ///
    /// [`Dname`]: struct.Dname.html
    fn to_dname<Octets>(
        &self,
    ) -> Result<Dname<Octets>, BuilderAppendError<Octets>>
    where
        Octets: FromBuilder,
        <Octets as FromBuilder>::Builder: EmptyBuilder,
    {
        let mut builder =
            Octets::Builder::with_capacity(self.compose_len().into());
        self.iter_labels()
            .try_for_each(|label| label.compose(&mut builder))?;
        Ok(unsafe { Dname::from_octets_unchecked(builder.freeze()) })
    }

    /// Converts the name into a single name in canonical form.
    fn to_canonical_dname<Octets>(
        &self,
    ) -> Result<Dname<Octets>, BuilderAppendError<Octets>>
    where
        Octets: FromBuilder,
        <Octets as FromBuilder>::Builder: EmptyBuilder,
    {
        let mut builder =
            Octets::Builder::with_capacity(self.compose_len().into());
        self.iter_labels()
            .try_for_each(|label| label.compose_canonical(&mut builder))?;
        Ok(unsafe { Dname::from_octets_unchecked(builder.freeze()) })
    }

    /// Returns an octets slice of the content if possible.
    ///
    /// If a value stores the domain name as one single octets sequence, it
    /// should return a reference to this sequence here. If the name is
    /// composed from multiple such sequences, it should return `None`.
    ///
    /// This method is used to optimize comparision operations between
    /// two values that are indeed flat names.
    fn as_flat_slice(&self) -> Option<&[u8]> {
        None
    }

    fn compose<Target: OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        if let Some(slice) = self.as_flat_slice() {
            target.append_slice(slice)
        } else {
            for label in self.iter_labels() {
                label.compose(target)?;
            }
            Ok(())
        }
    }

    fn compose_canonical<Target: OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        for label in self.iter_labels() {
            label.compose_canonical(target)?;
        }
        Ok(())
    }

    /// Returns a cow of the domain name.
    ///
    /// If the name is available as one single slice – i.e.,
    /// [`as_flat_slice`] returns ‘some,’ creates the borrowed variant from
    /// that slice. Otherwise assembles an owned variant via [`to_dname`].
    ///
    /// [`as_flat_slice`]: #method.as_flat_slice
    /// [`to_dname`]: #method.to_dname
    #[cfg(feature = "std")]
    fn to_cow(&self) -> Dname<std::borrow::Cow<[u8]>> {
        let octets = self
            .as_flat_slice()
            .map(Cow::Borrowed)
            .unwrap_or_else(|| Cow::Owned(self.to_vec().into_octets()));
        unsafe { Dname::from_octets_unchecked(octets) }
    }

    /// Returns the domain name assembled into a `Vec<u8>`.
    #[cfg(feature = "std")]
    fn to_vec(&self) -> Dname<std::vec::Vec<u8>> {
        infallible(self.to_dname())
    }

    /// Returns the domain name assembled into a bytes value.
    #[cfg(feature = "bytes")]
    fn to_bytes(&self) -> Dname<Bytes> {
        infallible(self.to_dname())
    }

    /// Tests whether `self` and `other` are equal.
    ///
    /// This method can be used to implement `PartialEq` on types implementing
    /// `ToDname` since a blanket implementation for all pairs of `ToDname`
    /// is currently impossible.
    ///
    /// Domain names are compared ignoring ASCII case.
    fn name_eq<N: ToDname + ?Sized>(&self, other: &N) -> bool {
        if let (Some(left), Some(right)) =
            (self.as_flat_slice(), other.as_flat_slice())
        {
            // We can do this because the length octets of each label are in
            // the ranged 0..64 which is before all ASCII letters.
            left.eq_ignore_ascii_case(right)
        } else {
            self.iter_labels().eq(other.iter_labels())
        }
    }

    /// Returns the ordering between `self` and `other`.
    ///
    /// This method can be used to implement both `PartialOrd` and `Ord` on
    /// types implementing `ToDname` since a blanket implementation for all
    /// pairs of `ToDname`s is currently not possible.
    ///
    /// Domain name order is determined according to the ‘canonical DNS
    /// name order’ as defined in [section 6.1 of RFC 4034][RFC4034-6.1].
    ///
    /// [RFC4034-6.1]: https://tools.ietf.org/html/rfc4034#section-6.1
    fn name_cmp<N: ToDname + ?Sized>(&self, other: &N) -> cmp::Ordering {
        let mut self_iter = self.iter_labels();
        let mut other_iter = other.iter_labels();
        loop {
            match (self_iter.next_back(), other_iter.next_back()) {
                (Some(left), Some(right)) => match left.cmp(right) {
                    cmp::Ordering::Equal => {}
                    res => return res,
                },
                (None, Some(_)) => return cmp::Ordering::Less,
                (Some(_), None) => return cmp::Ordering::Greater,
                (None, None) => return cmp::Ordering::Equal,
            }
        }
    }

    /// Returns the composed name ordering.
    fn composed_cmp<N: ToDname + ?Sized>(&self, other: &N) -> cmp::Ordering {
        if let (Some(left), Some(right)) =
            (self.as_flat_slice(), other.as_flat_slice())
        {
            return left.cmp(right);
        }
        let mut self_iter = self.iter_labels();
        let mut other_iter = other.iter_labels();
        loop {
            match (self_iter.next(), other_iter.next()) {
                (Some(left), Some(right)) => match left.composed_cmp(right) {
                    cmp::Ordering::Equal => {}
                    other => return other,
                },
                (None, None) => return cmp::Ordering::Equal,
                _ => {
                    // The root label sorts before any other label, so we
                    // can never end up in a situation where one name runs
                    // out of labels while comparing equal.
                    unreachable!()
                }
            }
        }
    }

    /// Returns the lowercase composed ordering.
    fn lowercase_composed_cmp<N: ToDname + ?Sized>(
        &self,
        other: &N,
    ) -> cmp::Ordering {
        // Since there isn’t a `cmp_ignore_ascii_case` on slice, we don’t
        // gain much from the shortcut.
        let mut self_iter = self.iter_labels();
        let mut other_iter = other.iter_labels();
        loop {
            match (self_iter.next(), other_iter.next()) {
                (Some(left), Some(right)) => {
                    match left.lowercase_composed_cmp(right) {
                        cmp::Ordering::Equal => {}
                        other => return other,
                    }
                }
                (None, None) => return cmp::Ordering::Equal,
                _ => {
                    // The root label sorts before any other label, so we
                    // can never end up in a situation where one name runs
                    // out of labels while comparing equal.
                    unreachable!()
                }
            }
        }
    }

    /// Returns the number of labels for the RRSIG Labels field.
    ///
    /// This is the actual number of labels without counting the root label
    /// or a possible initial asterisk label.
    fn rrsig_label_count(&self) -> u8 {
        let mut labels = self.iter_labels();
        if labels.next().unwrap().is_wildcard() {
            (labels.count() - 1) as u8
        } else {
            labels.count() as u8
        }
    }
}

impl<'a, N: ToDname + ?Sized + 'a> ToDname for &'a N {}

//------------ ToRelativeDname -----------------------------------------------

/// A type that represents a relative domain name.
///
/// In order to be a relative domain name, a type needs to be able to
/// provide a sequence of labels via an iterator where the last label is not
/// the root label. The type also needs to be able to compose the wire-format
/// representation of the domain name it represents which must not be longer
/// than 254 characters. This limit has been chosen so that by attaching the
/// one character long root label, a valid absolute name can be constructed
/// from the relative name.
///
/// The most important types implementing this trait are [`RelativeDname`]
/// and [`Chain<L,R>`] where `R` is a `ToRelativeDname` itself.
///
/// [`Chain<L, R>`]: struct.Chain.html
/// [`RelativeDname`]: struct.RelativeDname.html
pub trait ToRelativeDname: ToLabelIter {
    /// Converts the name into a single, continous name.
    ///
    /// The canonical implementation provided by the trait iterates over the
    /// labels of the name and adds them one by one to [`RelativeDname`].
    /// This will work for any name but an optimized implementation can be
    /// provided for
    /// some types of names.
    ///
    /// [`RelativeDname`]: struct.RelativeDname.html
    fn to_relative_dname<Octets>(
        &self,
    ) -> Result<RelativeDname<Octets>, BuilderAppendError<Octets>>
    where
        Octets: FromBuilder,
        <Octets as FromBuilder>::Builder: EmptyBuilder,
    {
        let mut builder =
            Octets::Builder::with_capacity(self.compose_len().into());
        self.iter_labels()
            .try_for_each(|label| label.compose(&mut builder))?;
        Ok(unsafe { RelativeDname::from_octets_unchecked(builder.freeze()) })
    }

    /// Converts the name into a single name in canonical form.
    fn to_canonical_relative_dname<Octets>(
        &self,
    ) -> Result<RelativeDname<Octets>, BuilderAppendError<Octets>>
    where
        Octets: FromBuilder,
        <Octets as FromBuilder>::Builder: EmptyBuilder,
    {
        let mut builder =
            Octets::Builder::with_capacity(self.compose_len().into());
        self.iter_labels()
            .try_for_each(|label| label.compose_canonical(&mut builder))?;
        Ok(unsafe { RelativeDname::from_octets_unchecked(builder.freeze()) })
    }

    /// Returns a byte slice of the content if possible.
    ///
    /// This method can is used to optimize comparision operations between
    /// two values that are indeed flat names.
    fn as_flat_slice(&self) -> Option<&[u8]> {
        None
    }

    fn compose<Target: OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        if let Some(slice) = self.as_flat_slice() {
            target.append_slice(slice)
        } else {
            for label in self.iter_labels() {
                label.compose(target)?;
            }
            Ok(())
        }
    }

    fn compose_canonical<Target: OctetsBuilder + ?Sized>(
        &self,
        target: &mut Target,
    ) -> Result<(), Target::AppendError> {
        for label in self.iter_labels() {
            label.compose_canonical(target)?;
        }
        Ok(())
    }

    /// Returns a cow of the relative domain name.
    ///
    /// If the name is available as one single slice – i.e.,
    /// [`as_flat_slice`] returns ‘some,’ creates the borrowed variant from
    /// that slice. Otherwise assembles an owned variant via [`to_dname`].
    ///
    /// [`as_flat_slice`]: #method.as_flat_slice
    /// [`to_dname`]: #method.to_dname
    #[cfg(feature = "std")]
    fn to_cow(&self) -> RelativeDname<std::borrow::Cow<[u8]>> {
        let octets = self
            .as_flat_slice()
            .map(Cow::Borrowed)
            .unwrap_or_else(|| Cow::Owned(self.to_vec().into_octets()));
        unsafe { RelativeDname::from_octets_unchecked(octets) }
    }

    /// Returns the domain name assembled into a `Vec<u8>`.
    #[cfg(feature = "std")]
    fn to_vec(&self) -> RelativeDname<std::vec::Vec<u8>> {
        infallible(self.to_relative_dname())
    }

    /// Returns the domain name assembled into a bytes value.
    #[cfg(feature = "bytes")]
    fn to_bytes(&self) -> RelativeDname<Bytes> {
        infallible(self.to_relative_dname())
    }

    /// Returns whether the name is empty.
    fn is_empty(&self) -> bool {
        self.iter_labels().next().is_none()
    }

    /// Returns a chain of this name and the provided name.
    fn chain<N: ToLabelIter>(
        self,
        suffix: N,
    ) -> Result<Chain<Self, N>, LongChainError>
    where
        Self: Sized,
    {
        Chain::new(self, suffix)
    }

    /// Returns the absolute name by chaining it with the root label.
    fn chain_root(self) -> Chain<Self, Dname<&'static [u8]>>
    where
        Self: Sized,
    {
        // Appending the root label will always work.
        Chain::new(self, Dname::root()).unwrap()
    }

    /// Tests whether `self` and `other` are equal.
    ///
    /// This method can be used to implement `PartialEq` on types implementing
    /// `ToDname` since a blanket implementation for all pairs of `ToDname`
    /// is currently impossible.
    ///
    /// Domain names are compared ignoring ASCII case.
    fn name_eq<N: ToRelativeDname + ?Sized>(&self, other: &N) -> bool {
        if let (Some(left), Some(right)) =
            (self.as_flat_slice(), other.as_flat_slice())
        {
            left.eq_ignore_ascii_case(right)
        } else {
            self.iter_labels().eq(other.iter_labels())
        }
    }

    /// Returns the ordering between `self` and `other`.
    ///
    /// This method can be used to implement both `PartialOrd` and `Ord` on
    /// types implementing `ToDname` since a blanket implementation for all
    /// pairs of `ToDname`s is currently not possible.
    ///
    /// Domain name order is determined according to the ‘canonical DNS
    /// name order’ as defined in [section 6.1 of RFC 4034][RFC4034-6.1].
    /// This section describes how absolute domain names are ordered only.
    /// We will order relative domain names according to these rules as if
    /// they had the same origin, i.e., as if they were relative to the
    /// same name.
    ///
    /// [RFC4034-6.1]: https://tools.ietf.org/html/rfc4034#section-6.1
    fn name_cmp<N: ToRelativeDname + ?Sized>(
        &self,
        other: &N,
    ) -> cmp::Ordering {
        let mut self_iter = self.iter_labels();
        let mut other_iter = other.iter_labels();
        loop {
            match (self_iter.next_back(), other_iter.next_back()) {
                (Some(left), Some(right)) => match left.cmp(right) {
                    cmp::Ordering::Equal => {}
                    res => return res,
                },
                (None, Some(_)) => return cmp::Ordering::Less,
                (Some(_), None) => return cmp::Ordering::Greater,
                (None, None) => return cmp::Ordering::Equal,
            }
        }
    }
}

impl<'a, N: ToRelativeDname + ?Sized + 'a> ToRelativeDname for &'a N {}

//------------ FlattenInto ---------------------------------------------------

pub trait FlattenInto<Target>: Sized {
    type AppendError: Into<ShortBuf>;

    fn try_flatten_into(self) -> Result<Target, Self::AppendError>;

    fn flatten_into(self) -> Target
    where
        Self::AppendError: Into<Infallible>,
    {
        infallible(self.try_flatten_into())
    }
}