domain_core/bits/opt/
rfc7873.rs

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