domain_core/bits/opt/
rfc7901.rs

1//! EDNS Options from RFC 7901
2
3use bytes::BufMut;
4use ::bits::compose::Compose;
5use ::bits::message_builder::OptBuilder;
6use ::bits::name::{Dname, ToDname};
7use ::bits::parse::{ParseAll, Parser, ShortBuf};
8use ::iana::OptionCode;
9use super::CodeOptData;
10
11
12//------------ Chain --------------------------------------------------------
13
14#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
15pub struct Chain {
16    start: Dname,
17}
18
19impl Chain {
20    pub fn new(start: Dname) -> Self {
21        Chain { start }
22    }
23
24    pub fn push<N: ToDname>(builder: &mut OptBuilder, start: &N)
25                            -> Result<(), ShortBuf> {
26        let len = start.compose_len();
27        assert!(len <= ::std::u16::MAX as usize);
28        builder.build(OptionCode::Chain, len as u16, |buf| {
29            buf.compose(start)
30        })
31    }
32
33    pub fn start(&self) -> &Dname {
34        &self.start
35    }
36}
37
38
39//--- ParseAll and Compose
40
41impl ParseAll for Chain {
42    type Err = <Dname as ParseAll>::Err;
43
44    fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
45        Dname::parse_all(parser, len).map(Self::new)
46    }
47}
48
49impl Compose for Chain {
50    fn compose_len(&self) -> usize {
51        self.start.compose_len()
52    }
53
54    fn compose<B: BufMut>(&self, buf: &mut B) {
55        self.start.compose(buf)
56    }
57}
58
59
60//--- CodeOptData
61
62impl CodeOptData for Chain {
63    const CODE: OptionCode = OptionCode::Chain;
64}
65