domain_core/bits/opt/
rfc7828.rs

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