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
//! EDNS Options from RFC 7314

use ::bits::{Composer, ComposeResult, Parser, ParseError, ParseResult};
use ::iana::OptionCode;
use super::{OptData, ParsedOptData};


//------------ Expire --------------------------------------------------------

#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Expire(Option<u32>);

impl Expire {
    pub fn new(expire: Option<u32>) -> Self {
        Expire(expire)
    }

    pub fn expire(&self) -> Option<u32> {
        self.0
    }
}

impl OptData for Expire {
    fn compose<C: AsMut<Composer>>(&self, mut target: C) -> ComposeResult<()> {
        let target = target.as_mut();
        target.compose_u16(OptionCode::EdnsExpire.into())?;
        match self.0 {
            Some(expire) => {
                target.compose_u16(4)?;
                target.compose_u32(expire)
            }
            None => {
                target.compose_u16(0)
            }
        }
    }
}

impl<'a> ParsedOptData<'a> for Expire {
    fn parse(code: OptionCode, parser: &mut Parser<'a>)
             -> ParseResult<Option<Self>> {
        if code != OptionCode::EdnsExpire {
            return Ok(None)
        }
        match parser.remaining() {
            0 => Ok(Some(Self::new(None))),
            4 => Ok(Some(Self::new(Some(parser.parse_u32()?)))),
            _ => Err(ParseError::FormErr)
        }
    }
}