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
//! Parsed domain names.

use std::borrow::Cow;
use std::cmp;
use std::fmt;
use std::hash;
use super::super::{Parser, ParseError, ParseResult};
use super::{DName, DNameBuf, DNameSlice, Label, NameLabels, NameLabelettes};
use super::plain::slice_from_bytes_unsafe;


//------------ ParsedDName ---------------------------------------------------

/// A domain name parsed from a DNS message.
///
/// In an attempt to keep messages small, DNS uses a procedure called name
/// compression. It tries to minimize the space used for repeated domain names
/// by simply refering to the first occurence of the name. This works not only
/// for complete names but also for suffixes. In this case, the first unique
/// labels of the name are included and then a pointer is included for the
/// rest of the name.
///
/// A consequence of this is that when parsing a domain name, its labels can
/// be scattered all over the message and we would need to allocate some
/// space to re-assemble the original name. However, in many cases we don’t
/// need the complete message. Many operations can be completed by just
/// iterating over the labels which we can do in place.
///
/// This is what the `ParsedDName` type does: It takes a reference to a
/// message and an indicator where inside the message the name starts and
/// then walks over the message as necessity dictates. When created while
/// parsing a message, the parser quickly walks over the labels to make sure
/// that the name indeed is valid. While this takes up a bit of time, it
/// avoids late surprises and provides for a nicer interface with less
/// `Result`s.
///
/// Obviously, `ParsedDName` implements the [`DName`] trait and provides all
/// operations required by this trait. It also implements `PartialEq` and
/// `Eq`, as well as `PartialOrd` and `Ord` against all other domain name
/// types, plus `Hash` with the same as the other types.
///
/// [`DName`]: trait.DName.html
#[derive(Clone)]
pub struct ParsedDName<'a> {
    message: &'a [u8],
    start: usize
}


/// # Creation and Conversion
///
impl<'a> ParsedDName<'a> {
    /// Creates a new parsed domain name.
    ///
    /// This parses out the leading uncompressed labels from the parser and
    /// then quickly jumps over any possible remaining compressing to check
    /// that the name is valid.
    pub fn parse(parser: &mut Parser<'a>) -> ParseResult<Self> {
        let res = ParsedDName{message: parser.bytes(), start: parser.pos()};

        // Step 1: Walk over uncompressed labels to advance the parser.
        let pos;
        loop {
            match try!(Self::parse_label(parser)) {
                Ok(true) => return Ok(res),
                Ok(false) => { }
                Err(x) => {
                    pos = x;
                    break
                }
            }
        }

        // Step 2: Walk over the rest to see if the name is valid.
        let mut parser = parser.clone();
        parser.remove_limit();
        try!(parser.seek(pos));
        loop {
            let step = try!(Self::parse_label(&mut parser));
            match step {
                Ok(true) => return Ok(res),
                Ok(false) => { }
                Err(pos) => try!(parser.seek(pos))
            }
        }
    }

    /// Unpacks the name.
    ///
    /// This will return the cow’s borrowed variant for any parsed name that
    /// isn’t in fact compressed. Otherwise it will assemble all the labels
    /// into an owned domain name.
    pub fn unpack(&self) -> Cow<'a, DNameSlice> {
        match self.split_uncompressed() {
            (Some(slice), None) => Cow::Borrowed(slice),
            (None, Some(packed)) => packed.unpack(),
            (None, None) => Cow::Borrowed(DNameSlice::empty()),
            (Some(slice), Some(packed)) => {
                let mut res = slice.to_owned();
                for label in packed.labels() {
                    res.push(label).unwrap()
                }
                Cow::Owned(res)
            }
        }
    }

    /// Returns a slice if the name is uncompressed.
    pub fn as_slice(&self) -> Option<&'a DNameSlice> {
        if let (Some(slice), None) = self.split_uncompressed() {
            Some(slice)
        }
        else {
            None
        }
    }
}


/// # Working with Labels
///
impl<'a> ParsedDName<'a> {
    /// Returns an iterator over the labels of the name.
    pub fn labels(&self) -> NameLabels<'a> {
        NameLabels::from_parsed(self.clone())
    }

    /// Returns an iterator over the labelettes of the name.
    pub fn labelettes(&self) -> NameLabelettes<'a> {
        NameLabelettes::new(self.labels())
    }

    /// Splits off the first label from the name.
    ///
    /// For correctly encoded names, this function will always return
    /// `Some(_)`. The first element will be the parsed out label. The
    /// second element will be a parsed name of the remainder of the name
    /// if the label wasn’t the root label or `None` otherwise.
    pub fn split_first(&self) -> Option<(&'a Label, Option<Self>)> {
        let mut name = self.clone();
        loop {
            let new_name = match name.split_label() {
                Ok(x) => return Some(x),
                Err(Some(x)) => x,
                Err(None) => return None
            };
            name = new_name;
        }
    }

    /// Splits a label or goes to where a pointer points.
    ///
    /// Ok((label, tail)) -> a label and what is left.
    /// Err(Some(tail)) -> re-positioned tail.
    /// Err(None) -> broken
    fn split_label(&self) -> Result<(&'a Label, Option<Self>), Option<Self>> {
        if self.message[self.start] & 0xC0 == 0xC0 {
            // Pointer label.
            let start = ((self.message[self.start] & 0x3f) as usize) << 8
                      | match self.message.get(self.start + 1) {
                          Some(two) => *two as usize,
                          None => return Err(None)
                      };
            if start >= self.message.len() {
                Err(None)
            }
            else {
                Err(Some(ParsedDName{message: self.message, start: start}))
            }
        }
        else {
            // "Real" label.
            let (label, _) = match Label::split_from(
                                                &self.message[self.start..]) {
                Some(x) => x,
                None => return Err(None)
            };
            let start = self.start + label.len();
            if label.is_root() {
                Ok((label, None))
            }
            else {
                Ok((label, Some(ParsedDName{message: self.message,
                                            start: start})))
            }
        }
    }

    /// Splits off the part that is uncompressed.
    fn split_uncompressed(&self) -> (Option<&'a DNameSlice>, Option<Self>) {
        let mut name = self.clone();
        loop {
            name = match name.split_label() {
                Ok((_, Some(new_name))) => new_name,
                Ok((label, None)) => {
                    let end = name.start + label.len();
                    let bytes = &self.message[self.start..end];
                    return (Some(unsafe { slice_from_bytes_unsafe(bytes) }),
                            None)
                }
                Err(Some(new_name)) => {
                    let bytes = &self.message[self.start..name.start];
                    return (Some(unsafe { slice_from_bytes_unsafe(bytes) }),
                            Some(new_name))
                }
                Err(None) => unreachable!()
            };
        }
    }

    /// Parses a label.
    ///
    /// Returns `Ok(is_root)` if the label is a normal label. Returns
    /// `Err(pos)` with the position of the next label.
    fn parse_label(parser: &mut Parser<'a>)
                   -> ParseResult<Result<bool, usize>> {
        let head = try!(parser.parse_u8());
        match head {
            0 => Ok(Ok(true)),
            1 ... 0x3F => parser.skip(head as usize).map(|_| Ok(false)),
            0x41 => {
                let count = try!(parser.parse_u8());
                let len = if count == 0 { 32 }
                          else { ((count - 1) / 8 + 1) as usize };
                parser.skip(len).map(|_| Ok(false))
            }
            0xC0 ... 0xFF => {
                Ok(Err(try!(parser.parse_u8()) as usize
                       + (((head & 0x3F) as usize) << 8)))
            }
            _ => Err(ParseError::UnknownLabel)
        }
    }        
}


//--- DName

impl<'a> DName for ParsedDName<'a> {
    fn to_cow(&self) -> Cow<DNameSlice> {
        self.unpack()
    }

    fn labels(&self) -> NameLabels {
        NameLabels::from_parsed(self.clone())
    }
}


//--- PartialEq and Eq

impl<'a, N: DName> PartialEq<N> for ParsedDName<'a> {
    fn eq(&self, other: &N) -> bool {
        let self_iter = self.labelettes();
        let other_iter = other.labelettes();
        self_iter.eq(other_iter)
    }
}

impl<'a> PartialEq<str> for ParsedDName<'a> {
    fn eq(&self, other: &str) -> bool {
        use std::str::FromStr;

        let other = match DNameBuf::from_str(other) {
            Ok(other) => other,
            Err(_) => return false
        };
        self.eq(&other)
    }
}

impl<'a> Eq for ParsedDName<'a> { }


//--- PartialOrd and Ord

impl<'a, N: DName> PartialOrd<N> for ParsedDName<'a> {
    fn partial_cmp(&self, other: &N) -> Option<cmp::Ordering> {
        let self_iter = self.labelettes().rev();
        let other_iter = other.labelettes().rev();
        self_iter.partial_cmp(other_iter)
    }
}

impl<'a> Ord for ParsedDName<'a> {
    fn cmp(&self, other: &Self) -> cmp::Ordering {
        let self_iter = self.labelettes().rev();
        let other_iter = other.labelettes().rev();
        self_iter.cmp(other_iter)
    }
}


//--- Hash

impl<'a> hash::Hash for ParsedDName<'a> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        for item in self.labelettes() {
            item.hash(state)
        }
    }
}


//--- std::fmt traits

impl<'a> fmt::Display for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.labels();
        if let Some(label) = labels.next() {
            try!(write!(f, "{}", label));
        }
        for label in labels {
            try!(write!(f, ".{}", label))
        }
        Ok(())
    }
}

impl<'a> fmt::Octal for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.labels();
        if let Some(label) = labels.next() {
            try!(write!(f, "{:o}", label));
        }
        for label in labels {
            try!(write!(f, ".{:o}", label))
        }
        Ok(())
    }
}

impl<'a> fmt::LowerHex for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.labels();
        if let Some(label) = labels.next() {
            try!(write!(f, "{:x}", label));
        }
        for label in labels {
            try!(write!(f, ".{:x}", label))
        }
        Ok(())
    }
}

impl<'a> fmt::UpperHex for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.labels();
        if let Some(label) = labels.next() {
            try!(write!(f, "{:X}", label));
        }
        for label in labels {
            try!(write!(f, ".{:X}", label))
        }
        Ok(())
    }
}

impl<'a> fmt::Binary for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut labels = self.labels();
        if let Some(label) = labels.next() {
            try!(write!(f, "{:b}", label));
        }
        for label in labels {
            try!(write!(f, ".{:b}", label))
        }
        Ok(())
    }
}

impl<'a> fmt::Debug for ParsedDName<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(f.write_str("ParsedDName("));
        try!(fmt::Display::fmt(self, f));
        f.write_str(")")
    }
}