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
use std::{borrow::Cow, convert::TryInto};
use crate::dns::DnsPacketContent;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct WKS<'a> {
pub address: u32,
pub protocol: u8,
pub bit_map: Cow<'a, [u8]>,
}
impl<'a> WKS<'a> {
pub fn into_owned<'b>(self) -> WKS<'b> {
WKS {
address: self.address,
protocol: self.protocol,
bit_map: self.bit_map.into_owned().into(),
}
}
}
impl<'a> DnsPacketContent<'a> for WKS<'a> {
fn parse(data: &'a [u8], position: usize) -> crate::Result<Self>
where
Self: Sized,
{
let address = u32::from_be_bytes(data[position..position + 4].try_into()?);
Ok(Self {
address,
protocol: data[position + 4],
bit_map: Cow::Borrowed(&data[position + 5..]),
})
}
fn append_to_vec(&self, out: &mut Vec<u8>) -> crate::Result<()> {
out.extend(self.address.to_be_bytes());
out.push(self.protocol);
out.extend(self.bit_map.iter());
Ok(())
}
fn len(&self) -> usize {
self.bit_map.len() + 5
}
}