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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::binutils::*;
use crate::ParseError;

use thiserror::Error;

use std::borrow::Cow;
use std::fmt;
use std::iter::zip;
use std::ops::Deref;
use std::str;

const INIT_NUM_LABELS: usize = 8;

pub(crate) const MAX_JUMPS: u8 = 5;

pub(crate) const MAX_LABEL_SIZE: usize = 63;
pub(crate) const MAX_NAME_SIZE: usize = 255;

/// An error was encountered when trying to work with a domain name
#[derive(Error, Debug)]
pub enum NameError {
    /// Some label in the DNS packet it too long, overflowing the packet or not following the DNS specification.
    #[error(
        "Specified label length ({0}) is empty or is bigger than DNS specification (maximum {}).",
        MAX_LABEL_SIZE
    )]
    LabelLength(usize),
    /// Some label in one of the domain names is not valid because it contains characters that are not alphanumeric or `-`.
    #[error("The provided label is not a valid domain name label")]
    LabelContent,
    /// One of the labels in the packet has a length that is bigger than the DNS specification.
    #[error(
        "Name length ({0}) is too long, is bigger than DNS specification (maximum {}).",
        MAX_NAME_SIZE
    )]
    NameLength(usize),
}

/// A domain name represented as an inverted list of labels.
#[derive(Clone)]
pub struct Name<'a> {
    /// Domain name labels
    labels: Vec<Cow<'a, str>>,
    /// Length of the domain name
    len: u8,
}

impl fmt::Display for Name<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for l in self.iter_human() {
            write!(f, "{}.", l)?;
        }
        Ok(())
    }
}

impl fmt::Debug for Name<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for l in self.iter_human() {
            write!(f, "{}.", l)?;
        }
        Ok(())
    }
}

impl Default for Name<'_> {
    #[inline]
    fn default() -> Self {
        Self::new()
    }
}

impl From<Name<'_>> for Vec<u8> {
    #[inline]
    fn from(name: Name<'_>) -> Self {
        let mut out = Vec::with_capacity(name.len as _);
        name.serialize(&mut out);
        out
    }
}

impl<'a> TryFrom<&'a str> for Name<'a> {
    type Error = NameError;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        let mut name = Name::default();
        for label in value.rsplit('.') {
            name.push_label(label.into())?;
        }
        Ok(name)
    }
}

impl<'a> Name<'a> {
    /// Parse from the specified `buff`, starting at position `pos`.
    ///
    /// # Errors
    ///
    /// It will error if the buffer does not contain a valid domain name. If the domain name
    /// has been compressed the buffer should include all previous bytes from the DNS packet
    /// to be considered valid. Jump pointers should only point backwards inside the `buf`.
    #[inline]
    pub fn parse(buff: &'a [u8], pos: usize) -> Result<(Self, usize), ParseError> {
        let mut name = Name::new();
        let blen = buff.len();
        let (mut pos, mut size, mut jumps) = (pos, 0, 0);
        loop {
            if jumps > MAX_JUMPS {
                Err(ParseError::ExcesiveJumps(jumps))?;
            }
            match read_label_metadata(buff, pos)? {
                LabelMeta::Pointer(ptr) if ptr >= pos => Err(ParseError::InvalidJump)?,
                LabelMeta::Size(s) if s > MAX_LABEL_SIZE => Err(NameError::LabelLength(s))?,
                LabelMeta::Size(s) if blen <= pos + s => Err(NameError::LabelLength(s))?,
                LabelMeta::Size(s) if name.len as usize + s > MAX_NAME_SIZE => {
                    Err(NameError::NameLength(name.len as usize + s))?
                }
                LabelMeta::Size(s) if jumps == 0 => {
                    name.push_bytes(&buff[pos + 1..pos + s + 1])?;
                    pos += s + 1;
                    size += s + 1;
                }
                LabelMeta::Size(s) => {
                    name.push_bytes(&buff[pos + 1..pos + s + 1])?;
                    pos += s + 1;
                }
                LabelMeta::Pointer(ptr) if jumps == 0 => {
                    (pos, size, jumps) = (ptr, size + 2, jumps + 1);
                }
                LabelMeta::Pointer(ptr) => (pos, jumps) = (ptr, jumps + 1),
                LabelMeta::End if jumps == 0 => {
                    name.labels.reverse();
                    return Ok((name, size + 1));
                }
                LabelMeta::End => {
                    name.labels.reverse();
                    return Ok((name, size));
                }
            }
        }
    }

    /// Safely push a slice of bytes as as a subdomain label.
    fn push_bytes(&mut self, bytes: &'a [u8]) -> Result<(), NameError> {
        if valid_label(bytes) {
            // SAFETY: Because we have verified that the label is only ASCII alphanumeric + `-`
            // we now the label is valid UTF8.
            let label = unsafe { str::from_utf8_unchecked(bytes) };
            self.labels.push(label.into());
            // SAFETY: It wont overflow because valid labels have a length that fits in one byte.
            self.len += bytes.len() as u8 + 1;
            Ok(())
        } else {
            Err(NameError::LabelContent)
        }
    }

    /// Serialize the [Name] and append it tho the end of the provided `packet`
    #[inline]
    pub fn serialize(&self, packet: &mut Vec<u8>) {
        for label in self.iter_human() {
            packet.push(label.len() as _);
            packet.extend(label.as_bytes());
        }
        packet.push(0u8);
    }

    /// Create a new, empty, domain name.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let name = Name::new();
    /// assert_eq!(name.to_string(), "".to_string())
    /// ```
    #[inline]
    pub fn new() -> Self {
        Name {
            labels: Vec::with_capacity(INIT_NUM_LABELS),
            len: 0,
        }
    }

    /// Obtain the top level domain (TLD) of the provided domain name.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::try_from("example.com").unwrap();
    /// assert_eq!(name.tld(), Some("com"))
    /// ```
    #[inline]
    pub fn tld(&self) -> Option<&'_ str> {
        self.labels.first().map(|cow| cow.deref())
    }

    /// Push a new label to the end of the domain name, as a subdomain of the current one.
    ///
    /// # Error
    ///
    /// Will error if the label is not a valid DNS label, or if the resulting Domain name is too big.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::new();
    /// name.push_label("com".into()).unwrap();
    /// name.push_label("example".into()).unwrap();
    /// assert_eq!(name.to_string(), "example.com.".to_string())
    /// ```
    #[inline]
    pub fn push_label(&mut self, label: Cow<'a, str>) -> Result<(), NameError> {
        let len = label.len();
        if label.is_empty() || len > MAX_LABEL_SIZE {
            Err(NameError::LabelLength(len))
        } else if len + self.len as usize > MAX_NAME_SIZE {
            Err(NameError::NameLength(len + self.len as usize))
        } else if !valid_label(label.as_bytes()) {
            Err(NameError::LabelContent)
        } else {
            // SAFETY: It wont overflow because we have checked that the domain name length is not bigger than 255.
            self.len += len as u8;
            self.labels.push(label);
            Ok(())
        }
    }

    /// Get the number of labels in the domain name.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::try_from("example.com").unwrap();
    /// assert_eq!(2, name.label_count())
    /// ```
    #[inline]
    pub fn label_count(&self) -> usize {
        self.labels.len()
    }

    /// Check if `sub` is a subdomain of the current domain name.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::try_from("example.com").unwrap();
    /// let mut sub = Name::try_from("subdomain.example.com").unwrap();
    ///
    /// assert!(name.is_subdomain(&sub))
    /// ```
    #[inline]
    pub fn is_subdomain(&self, sub: &Name<'_>) -> bool {
        if self.labels.len() >= sub.labels.len() {
            false
        } else {
            zip(self.iter_hierarchy(), sub.iter_hierarchy()).fold(true, |acc, (x, y)| acc && x == y)
        }
    }

    /// Return an iterator over the labels in human order.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::try_from("subdomain.example.com").unwrap();
    /// let mut human = name.iter_human();
    ///
    /// assert_eq!(human.next(), Some("subdomain"));
    /// assert_eq!(human.next(), Some("example"));
    /// assert_eq!(human.next(), Some("com"));
    /// ```
    #[inline]
    pub fn iter_human(&self) -> impl DoubleEndedIterator<Item = &'_ str> {
        self.iter_hierarchy().rev()
    }

    /// Return an iterator over the labels in hierarchical order.
    ///
    /// ```
    /// # use dominion_parser::body::name::Name;
    /// let mut name = Name::try_from("subdomain.example.com").unwrap();
    /// let mut hierarchy = name.iter_hierarchy();
    ///
    /// assert_eq!(hierarchy.next(), Some("com"));
    /// assert_eq!(hierarchy.next(), Some("example"));
    /// assert_eq!(hierarchy.next(), Some("subdomain"));
    /// ```
    #[inline]
    pub fn iter_hierarchy(&self) -> impl DoubleEndedIterator<Item = &'_ str> {
        self.labels.iter().map(|cow| cow.deref())
    }
}

/// A label can only contain a `-` or alphanumeric characters, and must begin with a letter.
fn valid_label(label: &[u8]) -> bool {
    label
        .iter()
        .all(|&b| b.is_ascii_alphanumeric() || b == b'-')
}

enum LabelMeta {
    End,
    // Although it is really an u8 because it is used for indexing we give an usize
    Size(usize),
    // Although it is really an u16 because it is used for indexing we give an usize
    Pointer(usize),
}

#[inline]
fn read_label_metadata(buff: &[u8], pos: usize) -> Result<LabelMeta, ParseError> {
    let b = safe_u8_read(buff, pos)?;
    match b {
        0 => Ok(LabelMeta::End),
        1..=0b0011_1111 => Ok(LabelMeta::Size(b as _)),
        0b1100_0000..=0xFF => Ok(LabelMeta::Pointer(
            (safe_u16_read(buff, pos)? ^ 0b1100_0000_0000_0000) as _,
        )),
        _ => Err(ParseError::LabelPrefix(b))?,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn valid_labels() {
        let valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";
        let invalid = "hello.world";
        assert!(valid_label(valid.as_bytes()));
        assert!(!valid_label(invalid.as_bytes()));
    }

    #[test]
    fn no_jumps() {
        let buff = [
            5, 104, 101, 108, 108, 111, // hello
            5, 119, 111, 114, 108, 100, // world
            3, 99, 111, 109, // com
            0, 1, 1, 1, // <end>
        ];
        let (name, n) = Name::parse(&buff[..], 0).unwrap();
        assert_eq!(n, 17);
        assert_eq!(name.to_string(), "hello.world.com.".to_string())
    }

    #[test]
    fn with_jumps() {
        let buff = [
            5, 119, 111, 114, 108, 100, // world
            3, 99, 111, 109, // com
            0, 1, 1, 1, // <end>
            5, 104, 101, 108, 108, 111, // hello
            192, 0, 1, 1, 1, 1, 1, 1, // <jump to 0>
        ];
        let (name, n) = Name::parse(&buff[..], 14).unwrap();
        assert_eq!(n, 8);
        assert_eq!(name.to_string(), "hello.world.com.".to_string())
    }

    #[test]
    fn name_parse_with_jumps() {
        let buff = [
            5, 119, 111, 114, 108, 100, // world
            3, 99, 111, 109, // com
            0, 1, 1, 1, // <end>
            5, 104, 101, 108, 108, 111, // hello
            192, 0, 1, 1, 1, 1, 1, 1, // <jump to 0>
        ];
        let (name, n) = Name::parse(&buff[..], 14).unwrap();
        assert_eq!(n, 8);
        assert_eq!(name.to_string(), "hello.world.com.".to_string())
    }

    #[test]
    fn serialize() {
        let buff = [
            5, 104, 101, 108, 108, 111, // hello
            5, 119, 111, 114, 108, 100, // world
            3, 99, 111, 109, // com
            0, 1, 1, 1, // <end>
        ];
        let (name, _) = Name::parse(&buff[..], 0).unwrap();
        assert_eq!(name.to_string(), "hello.world.com.".to_string());
        let out: Vec<u8> = name.into();
        assert_eq!(&buff[..17], &out[..17])
    }

    #[test]
    fn get_tld() {
        let mut name = Name::new();
        name.push_label("com".into()).unwrap();
        name.push_label("world".into()).unwrap();
        name.push_label("hello".into()).unwrap();

        let tld = name.tld();
        assert_eq!(tld, Some("com"));
    }

    #[test]
    fn add_str_subdomain() {
        let buff = [5, 119, 111, 114, 108, 100, 3, 99, 111, 109, 0, 1, 1, 1]; // world.com
        let (mut name, _) = Name::parse(&buff[..], 0).unwrap();
        name.push_label("hello".into()).unwrap();
        assert_eq!(name.to_string(), "hello.world.com.".to_string())
    }

    #[test]
    fn add_string_subdomain() {
        let sub = String::from("hello");
        let buff = [5, 119, 111, 114, 108, 100, 3, 99, 111, 109, 0, 1, 1, 1]; // world.com
        let (mut name, _) = Name::parse(&buff[..], 0).unwrap();
        name.push_label(sub.into()).unwrap();
        assert_eq!(name.to_string(), "hello.world.com.".to_string())
    }

    #[test]
    fn iterate_human() {
        let mut name = Name::new();
        name.push_label("com".into()).unwrap();
        name.push_label("world".into()).unwrap();
        name.push_label("hello".into()).unwrap();

        let mut human = name.iter_human();
        assert_eq!(human.next(), Some("hello"));
        assert_eq!(human.next(), Some("world"));
        assert_eq!(human.next(), Some("com"));
    }

    #[test]
    fn iterate_hierarchy() {
        let mut name = Name::new();
        name.push_label("com".into()).unwrap();
        name.push_label("world".into()).unwrap();
        name.push_label("hello".into()).unwrap();

        let mut human = name.iter_hierarchy();
        assert_eq!(human.next(), Some("com"));
        assert_eq!(human.next(), Some("world"));
        assert_eq!(human.next(), Some("hello"));
    }

    #[test]
    fn check_subdomain() {
        let mut parent = Name::new();
        parent.push_label("com".into()).unwrap();
        parent.push_label("world".into()).unwrap();

        let mut sub = Name::new();
        sub.push_label("com".into()).unwrap();
        sub.push_label("world".into()).unwrap();
        sub.push_label("hello".into()).unwrap();

        assert!(parent.is_subdomain(&sub));
        assert!(!sub.is_subdomain(&parent));
    }

    #[test]
    fn root_subdomain() {
        let root = Name::default();
        let subd = Name::try_from("example.com").unwrap();

        assert!(root.is_subdomain(&subd));
        assert!(!subd.is_subdomain(&root));
    }
}