domain_core/bits/
parse.rs

1//! Parsing DNS wire-format data.
2//!
3//! This module provides a [`Parser`] that helps extracting data from DNS
4//! messages and two traits for types that know how to parse themselves:
5//! [`Parse`] for types that have an encoding of determinable length and
6//! [`ParseAll`] for types that fill the entire available space.
7//!
8//! [`Parser`]: struct.Parser.html
9//! [`Parse`]: trait.Parse.html
10//! [`ParseAll`]: trait.ParseAll.html
11
12use std::net::{Ipv4Addr, Ipv6Addr};
13use bytes::{BigEndian, ByteOrder, Bytes};
14use failure::Fail;
15
16
17//------------ Parser --------------------------------------------------------
18
19/// The raw data and state of a DNS message being parsed.
20///
21/// Because of name compression, a full message needs to be available for
22/// parsing of DNS data. This type is a small layer atop a [`Bytes`] value.
23/// You can wrap one using the [`from_bytes()`] function.
24///
25/// The parser allows you to successively parse one item after another
26/// out of the message via a few methods prefixed with `parse_`. Additional
27/// methods are available for repositioning the parser’s position or access
28/// the raw, underlying bytes.
29///
30/// The methods of a parser never panic if you try to go beyond the end of
31/// the parser’s data. Instead, they will return a [`ShortBuf`] error,
32/// making it more straightforward to implement a complex parser. Since an
33/// error normally leads to processing being aborted, functions that return
34/// an error can leave the parser at whatever position they like. In the
35/// rare case that you actually need to backtrack on the parser in case of
36/// an error, you will have to remember and reposition yourself. 
37///
38/// Parsers are `Clone`, so you can keep around a copy of a parser for later
39/// use. This is, for instance, done by [`ParsedDname`] in order to be able
40/// to rummage around the message bytes to find all its labels. Because
41/// copying a [`Bytes`] value is relatively cheap, cloning a parser is cheap,
42/// too.
43///
44/// [`from_bytes()`]: #method.from_bytes
45/// [`Bytes`]: ../../../bytes/struct.Bytes.html
46/// [`ParsedDname`]: ../name/struct.ParsedDname.html
47/// [`ShortBuf`]: ../struct.ShortBuf.html
48#[derive(Clone, Debug)]
49pub struct Parser {
50    /// The underlying data.
51    bytes: Bytes,
52
53    /// The index in `bytes` where parsing should continue.
54    pos: usize
55}
56
57impl Parser {
58    /// Creates a new parser atop a bytes value.
59    pub fn from_bytes(bytes: Bytes) -> Self {
60        Parser { bytes, pos: 0 }
61    }
62
63    /// Creates a new parser atop a static byte slice.
64    ///
65    /// This function is most useful for testing.
66    pub fn from_static(slice: &'static [u8]) -> Self {
67        Self::from_bytes(Bytes::from_static(slice))
68    }
69
70    /// Extracts the underlying bytes value from the parser.
71    ///
72    /// This will be the same bytes value the parser was created with. It
73    /// will not be modified by parsing at all.
74    pub fn unwrap(self) -> Bytes {
75        self.bytes
76    }
77}
78
79impl Parser {
80    /// Returns a reference to the underlying bytes.
81    pub fn as_bytes(&self) -> &Bytes {
82        &self.bytes
83    }
84
85    /// Returns a reference to the underlying byte slice.
86    pub fn as_slice(&self) -> &[u8] {
87        self.bytes.as_ref()
88    }
89
90    /// Returns the current parse position as an index into the byte slice.
91    pub fn pos(&self) -> usize {
92        self.pos
93    }
94
95    /// Returns the number of remaining bytes to parse.
96    pub fn remaining(&self) -> usize {
97        self.bytes.len() - self.pos
98    }
99
100    /// Returns a slice containing the next `len` bytes.
101    ///
102    /// If less than `len` bytes are left, returns an error.
103    pub fn peek(&self, len: usize) -> Result<&[u8], ShortBuf> {
104        self.check_len(len)?;
105        Ok(&self.peek_all()[..len])
106    }
107
108    /// Returns a byte slice of the data left to parse.
109    pub fn peek_all(&self) -> &[u8] {
110        &self.bytes.as_ref()[self.pos..]
111    }
112
113    /// Repositions the parser to the given index.
114    ///
115    /// If `pos` is larger than the length of the parser, an error is
116    /// returned.
117    pub fn seek(&mut self, pos: usize) -> Result<(), ShortBuf> {
118        if pos > self.bytes.len() {
119            Err(ShortBuf)
120        }
121        else {
122            self.pos = pos;
123            Ok(())
124        }
125    }
126
127    /// Advances the parser‘s position by `len` bytes.
128    ///
129    /// If this would take the parser beyond its end, an error is returned.
130    pub fn advance(&mut self, len: usize) -> Result<(), ShortBuf> {
131        if len > self.remaining() {
132            Err(ShortBuf)
133        }
134        else {
135            self.pos += len;
136            Ok(())
137        }
138    }
139
140    /// Checks that there are `len` bytes left to parse.
141    ///
142    /// If there aren’t, returns an error.
143    pub fn check_len(&self, len: usize) -> Result<(), ShortBuf> {
144        if self.remaining() < len {
145            Err(ShortBuf)
146        }
147        else {
148            Ok(())
149        }
150    }
151
152    /// Takes the next `len` bytes and returns them as a `Bytes` value.
153    ///
154    /// Advances the parser by `len` bytes. If there aren’t enough bytes left,
155    /// leaves the parser untouched and returns an error, instead.
156    pub fn parse_bytes(&mut self, len: usize) -> Result<Bytes, ShortBuf> {
157        let end = self.pos + len;
158        if end > self.bytes.len() {
159            return Err(ShortBuf)
160        }
161        let res = self.bytes.slice(self.pos, end);
162        self.pos = end;
163        Ok(res)
164    }
165
166    /// Fills the provided buffer by taking bytes from the parser.
167    pub fn parse_buf(&mut self, buf: &mut [u8]) -> Result<(), ShortBuf> {
168        let pos = self.pos;
169        self.advance(buf.len())?;
170        buf.copy_from_slice(&self.bytes.as_ref()[pos..self.pos]);
171        Ok(())
172    }
173
174    /// Takes an `i8` from the beginning of the parser.
175    ///
176    /// Advances the parser by one byte. If there aren’t enough bytes left,
177    /// leaves the parser untouched and returns an error, instead.
178    pub fn parse_i8(&mut self) -> Result<i8, ShortBuf> {
179        let res = self.peek(1)?[0] as i8;
180        self.pos += 1;
181        Ok(res)
182    }
183
184    /// Takes a `u8` from the beginning of the parser.
185    ///
186    /// Advances the parser by one byte. If there aren’t enough bytes left,
187    /// leaves the parser untouched and returns an error, instead.
188    pub fn parse_u8(&mut self) -> Result<u8, ShortBuf> {
189        let res = self.peek(1)?[0];
190        self.pos += 1;
191        Ok(res)
192    }
193
194    /// Takes an `i16` from the beginning of the parser.
195    ///
196    /// The value is converted from network byte order into the system’s own
197    /// byte order if necessary. The parser is advanced by two bytes. If there
198    /// aren’t enough bytes left, leaves the parser untouched and returns an
199    /// error, instead.
200    pub fn parse_i16(&mut self) -> Result<i16, ShortBuf> {
201        let res = BigEndian::read_i16(self.peek(2)?);
202        self.pos += 2;
203        Ok(res)
204    }
205
206    /// Takes a `u16` from the beginning of the parser.
207    ///
208    /// The value is converted from network byte order into the system’s own
209    /// byte order if necessary. The parser is advanced by two bytes. If there
210    /// aren’t enough bytes left, leaves the parser untouched and returns an
211    /// error, instead.
212    pub fn parse_u16(&mut self) -> Result<u16, ShortBuf> {
213        let res = BigEndian::read_u16(self.peek(2)?);
214        self.pos += 2;
215        Ok(res)
216    }
217
218    /// Takes an `i32` from the beginning of the parser.
219    ///
220    /// The value is converted from network byte order into the system’s own
221    /// byte order if necessary. The parser is advanced by four bytes. If
222    /// there aren’t enough bytes left, leaves the parser untouched and
223    /// returns an error, instead.
224    pub fn parse_i32(&mut self) -> Result<i32, ShortBuf> {
225        let res = BigEndian::read_i32(self.peek(4)?);
226        self.pos += 4;
227        Ok(res)
228    }
229
230    /// Takes a `u32` from the beginning of the parser.
231    ///
232    /// The value is converted from network byte order into the system’s own
233    /// byte order if necessary. The parser is advanced by four bytes. If
234    /// there aren’t enough bytes left, leaves the parser untouched and
235    /// returns an error, instead.
236    pub fn parse_u32(&mut self) -> Result<u32, ShortBuf> {
237        let res = BigEndian::read_u32(self.peek(4)?);
238        self.pos += 4;
239        Ok(res)
240    }
241}
242
243
244//------------ Parse ------------------------------------------------------
245
246/// A type that can extract a value from the beginning of a parser.
247///
248/// Types that implement this trait must use an encoding where the end of a
249/// value in the parser can be determined from data read so far. These are
250/// either fixed length types like `u32` or types that either contain length
251/// bytes or boundary markers.
252pub trait Parse: Sized {
253    /// The type of an error returned when parsing fails.
254    type Err: From<ShortBuf>;
255
256    /// Extracts a value from the beginning of `parser`.
257    ///
258    /// If parsing fails and an error is returned, the parser’s position
259    /// should be considered to be undefined. If it supposed to be reused in
260    /// this case, you should store the position before attempting to parse
261    /// and seek to that position again before continuing.
262    fn parse(parser: &mut Parser) -> Result<Self, Self::Err>;
263
264    /// Skips over a value of this type at the beginning of `parser`.
265    ///
266    /// This function is the same as `parse` but doesn’t return the result.
267    /// It can be used to check if the content of `parser` is correct or to
268    /// skip over unneeded parts of a message.
269    fn skip(parser: &mut Parser) -> Result<(), Self::Err>;
270}
271
272impl Parse for i8 {
273    type Err = ShortBuf;
274    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
275        parser.parse_i8()
276    }
277
278    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
279        parser.advance(1)
280    }
281}
282
283impl Parse for u8 {
284    type Err = ShortBuf;
285    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
286        parser.parse_u8()
287    }
288
289    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
290        parser.advance(1)
291    }
292}
293
294impl Parse for i16 {
295    type Err = ShortBuf;
296    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
297        parser.parse_i16()
298    }
299
300    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
301        parser.advance(2)
302    }
303}
304
305impl Parse for u16 {
306    type Err = ShortBuf;
307    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
308        parser.parse_u16()
309    }
310
311    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
312        parser.advance(2)
313    }
314}
315
316impl Parse for i32 {
317    type Err = ShortBuf;
318    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
319        parser.parse_i32()
320    }
321
322    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
323        parser.advance(4)
324    }
325}
326
327impl Parse for u32 {
328    type Err = ShortBuf;
329    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
330        parser.parse_u32()
331    }
332
333    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
334        parser.advance(4)
335    }
336}
337
338impl Parse for Ipv4Addr {
339    type Err = ShortBuf;
340
341    fn parse(parser: &mut Parser) -> Result<Self, ShortBuf> {
342        Ok(Self::new(
343            u8::parse(parser)?,
344            u8::parse(parser)?,
345            u8::parse(parser)?,
346            u8::parse(parser)?
347        ))
348    }
349
350    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
351        parser.advance(4)
352    }
353}
354
355impl Parse for Ipv6Addr {
356    type Err = ShortBuf;
357
358    fn parse(parser: &mut Parser) -> Result<Self, Self::Err> {
359        let mut buf = [0u8; 16];
360        parser.parse_buf(&mut buf)?;
361        Ok(buf.into())
362    }
363
364    fn skip(parser: &mut Parser) -> Result<(), ShortBuf> {
365        parser.advance(16)
366    }
367}
368
369
370//------------ ParseAll ------------------------------------------------------
371
372/// A type that can extract a value from a given part of a parser.
373///
374/// This trait is used when the length of a value is known before and the
375/// value is expected to stretch over this entire length. There are types
376/// that can implement `ParseAll` but not [`Parse`] because they simply take
377/// all remaining bytes.
378pub trait ParseAll: Sized {
379    /// The type returned when parsing fails.
380    type Err: From<ShortBuf> + Fail;
381
382    /// Parses a value `len` bytes long from the beginning of the parser.
383    ///
384    /// An implementation must read exactly `len` bytes from the parser or
385    /// fail. If it fails, the position of the parser is considered
386    /// undefined.
387    fn parse_all(parser: &mut Parser, len: usize)
388                 -> Result<Self, Self::Err>;
389}
390
391impl ParseAll for u8 {
392    type Err = ParseAllError;
393
394    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
395        if len < 1 {
396            Err(ParseAllError::ShortField)
397        }
398        else if len > 1 {
399            Err(ParseAllError::TrailingData)
400        }
401        else {
402            Ok(Self::parse(parser)?)
403        }
404    }
405}
406
407impl ParseAll for u16 {
408    type Err = ParseAllError;
409
410    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
411        if len < 2 {
412            Err(ParseAllError::ShortField)
413        }
414        else if len > 2 {
415            Err(ParseAllError::TrailingData)
416        }
417        else {
418            Ok(Self::parse(parser)?)
419        }
420    }
421}
422
423impl ParseAll for u32 {
424    type Err = ParseAllError;
425
426    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
427        if len < 4 {
428            Err(ParseAllError::ShortField)
429        }
430        else if len > 4 {
431            Err(ParseAllError::TrailingData)
432        }
433        else {
434            Ok(Self::parse(parser)?)
435        }
436    }
437}
438
439impl ParseAll for Bytes {
440    type Err = ShortBuf;
441
442    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
443        parser.parse_bytes(len)
444    }
445}
446
447impl ParseAll for Ipv4Addr {
448    type Err = ParseAllError;
449
450    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
451        if len < 4 {
452            Err(ParseAllError::ShortField)
453        }
454        else if len > 4 {
455            Err(ParseAllError::TrailingData)
456        }
457        else {
458            Ok(Self::parse(parser)?)
459        }
460    }
461}
462
463impl ParseAll for Ipv6Addr {
464    type Err = ParseAllError;
465
466    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
467        if len < 16 {
468            Err(ParseAllError::ShortField)
469        }
470        else if len > 16 {
471            Err(ParseAllError::TrailingData)
472        }
473        else {
474            Ok(Self::parse(parser)?)
475        }
476    }
477}
478
479
480//------------ ParseOpenError ------------------------------------------------
481
482/// An error happened when parsing all of a minimum length, open size type.
483#[derive(Clone, Copy, Debug, Eq, Fail, PartialEq)]
484pub enum ParseOpenError {
485    #[fail(display="short field")]
486    ShortField,
487
488    #[fail(display="unexpected end of buffer")]
489    ShortBuf
490}
491
492impl From<ShortBuf> for ParseOpenError {
493    fn from(_: ShortBuf) -> Self {
494        ParseOpenError::ShortBuf
495    }
496}
497
498
499//------------ ShortBuf ------------------------------------------------------
500
501/// An attempt was made to go beyond the end of a buffer.
502#[derive(Clone, Debug, Eq, Fail, PartialEq)]
503#[fail(display="unexpected end of buffer")]
504pub struct ShortBuf;
505
506
507//--------- ParseAllError ----------------------------------------------------
508
509/// An error happened while trying to length-parse a type with built-in limit.
510///
511/// This error type is used for type that have their own length indicators
512/// and where any possible byte combination is valid.
513#[derive(Clone, Copy, Debug, Eq, Fail, PartialEq)]
514pub enum ParseAllError {
515    #[fail(display="trailing data")]
516    TrailingData,
517
518    #[fail(display="short field")]
519    ShortField,
520
521    #[fail(display="unexpected end of buffer")]
522    ShortBuf
523}
524
525impl ParseAllError {
526    pub fn check(expected: usize, got: usize) -> Result<(), Self> {
527        if expected < got {
528            Err(ParseAllError::TrailingData)
529        }
530        else if expected > got {
531            Err(ParseAllError::ShortField)
532        }
533        else {
534            Ok(())
535        }
536    }
537}
538
539impl From<ShortBuf> for ParseAllError {
540    fn from(_: ShortBuf) -> Self {
541        ParseAllError::ShortBuf
542    }
543}
544
545
546//============ Testing =======================================================
547
548#[cfg(test)]
549mod test {
550    use super::*;
551
552    #[test]
553    fn pos_seek_remaining() {
554        let mut parser = Parser::from_static(b"0123456789");
555        assert_eq!(parser.peek(1).unwrap(), b"0");
556        assert_eq!(parser.pos(), 0);
557        assert_eq!(parser.remaining(), 10);
558        assert_eq!(parser.seek(2), Ok(()));
559        assert_eq!(parser.pos(), 2);
560        assert_eq!(parser.remaining(), 8);
561        assert_eq!(parser.peek(1).unwrap(), b"2");
562        assert_eq!(parser.seek(10), Ok(()));
563        assert_eq!(parser.pos(), 10);
564        assert_eq!(parser.remaining(), 0);
565        assert_eq!(parser.peek_all(), b"");
566        assert_eq!(parser.seek(11), Err(ShortBuf));
567        assert_eq!(parser.pos(), 10);
568        assert_eq!(parser.remaining(), 0);
569    }
570
571    #[test]
572    fn peek_check_len() {
573        let mut parser = Parser::from_static(b"0123456789");
574        assert_eq!(parser.peek(2), Ok(b"01".as_ref()));
575        assert_eq!(parser.check_len(2), Ok(()));
576        assert_eq!(parser.peek(10), Ok(b"0123456789".as_ref()));
577        assert_eq!(parser.check_len(10), Ok(()));
578        assert_eq!(parser.peek(11), Err(ShortBuf));
579        assert_eq!(parser.check_len(11), Err(ShortBuf));
580        parser.advance(2).unwrap();
581        assert_eq!(parser.peek(2), Ok(b"23".as_ref()));
582        assert_eq!(parser.check_len(2), Ok(()));
583        assert_eq!(parser.peek(8), Ok(b"23456789".as_ref()));
584        assert_eq!(parser.check_len(8), Ok(()));
585        assert_eq!(parser.peek(9), Err(ShortBuf));
586        assert_eq!(parser.check_len(9), Err(ShortBuf));
587    }
588
589    #[test]
590    fn peek_all() {
591        let mut parser = Parser::from_static(b"0123456789");
592        assert_eq!(parser.peek_all(), b"0123456789");
593        parser.advance(2).unwrap();
594        assert_eq!(parser.peek_all(), b"23456789");
595    }
596
597    #[test]
598    fn advance() {
599        let mut parser = Parser::from_static(b"0123456789");
600        assert_eq!(parser.pos(), 0);
601        assert_eq!(parser.peek(1).unwrap(), b"0");
602        assert_eq!(parser.advance(2), Ok(()));
603        assert_eq!(parser.pos(), 2);
604        assert_eq!(parser.peek(1).unwrap(), b"2");
605        assert_eq!(parser.advance(9), Err(ShortBuf));
606        assert_eq!(parser.advance(8), Ok(()));
607        assert_eq!(parser.pos(), 10);
608        assert_eq!(parser.peek_all(), b"");
609    }
610
611    #[test]
612    fn parse_bytes() {
613        let mut parser = Parser::from_static(b"0123456789");
614        assert_eq!(parser.parse_bytes(2).unwrap().as_ref(), b"01");
615        assert_eq!(parser.parse_bytes(2).unwrap().as_ref(), b"23");
616        assert_eq!(parser.parse_bytes(7), Err(ShortBuf));
617        assert_eq!(parser.parse_bytes(6).unwrap().as_ref(), b"456789");
618    }
619
620    #[test]
621    fn parse_buf() {
622        let mut parser = Parser::from_static(b"0123456789");
623        let mut buf = [0u8; 2];
624        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
625        assert_eq!(&buf, b"01");
626        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
627        assert_eq!(&buf, b"23");
628        let mut buf = [0u8; 7];
629        assert_eq!(parser.parse_buf(&mut buf), Err(ShortBuf));
630        let mut buf = [0u8; 6];
631        assert_eq!(parser.parse_buf(&mut buf), Ok(()));
632        assert_eq!(&buf, b"456789");
633    }
634
635    #[test]
636    fn parse_i8() {
637        let mut parser = Parser::from_static(b"\x12\xd6");
638        assert_eq!(parser.parse_i8(), Ok(0x12));
639        assert_eq!(parser.parse_i8(), Ok(-42));
640        assert_eq!(parser.parse_i8(), Err(ShortBuf));
641    }
642
643    #[test]
644    fn parse_u8() {
645        let mut parser = Parser::from_static(b"\x12\xd6");
646        assert_eq!(parser.parse_u8(), Ok(0x12));
647        assert_eq!(parser.parse_u8(), Ok(0xd6));
648        assert_eq!(parser.parse_u8(), Err(ShortBuf));
649    }
650
651    #[test]
652    fn parse_i16() {
653        let mut parser = Parser::from_static(b"\x12\x34\xef\x6e\0");
654        assert_eq!(parser.parse_i16(), Ok(0x1234));
655        assert_eq!(parser.parse_i16(), Ok(-4242));
656        assert_eq!(parser.parse_i16(), Err(ShortBuf));
657    }
658
659    #[test]
660    fn parse_u16() {
661        let mut parser = Parser::from_static(b"\x12\x34\xef\x6e\0");
662        assert_eq!(parser.parse_u16(), Ok(0x1234));
663        assert_eq!(parser.parse_u16(), Ok(0xef6e));
664        assert_eq!(parser.parse_u16(), Err(ShortBuf));
665    }
666
667    #[test]
668    fn parse_i32() {
669        let mut parser = Parser::from_static(
670            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\0\0\0");
671        assert_eq!(parser.parse_i32(), Ok(0x12345678));
672        assert_eq!(parser.parse_i32(), Ok(-42424242));
673        assert_eq!(parser.parse_i32(), Err(ShortBuf));
674    }
675
676    #[test]
677    fn parse_u32() {
678        let mut parser = Parser::from_static(
679            b"\x12\x34\x56\x78\xfd\x78\xa8\x4e\0\0\0");
680        assert_eq!(parser.parse_u32(), Ok(0x12345678));
681        assert_eq!(parser.parse_u32(), Ok(0xfd78a84e));
682        assert_eq!(parser.parse_u32(), Err(ShortBuf));
683    }
684
685
686}