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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use super::*;
use crate::error::Result;
use crate::error::*;
use crate::message::packer::*;
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct OptResource {
pub options: Vec<DnsOption>,
}
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct DnsOption {
pub code: u16, pub data: Vec<u8>,
}
impl fmt::Display for DnsOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"dnsmessage.Option{{Code: {}, Data: {:?}}}",
self.code, self.data
)
}
}
impl fmt::Display for OptResource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s: Vec<String> = self.options.iter().map(|o| o.to_string()).collect();
write!(f, "dnsmessage.OPTResource{{options: {}}}", s.join(","))
}
}
impl ResourceBody for OptResource {
fn real_type(&self) -> DnsType {
DnsType::Opt
}
fn pack(
&self,
mut msg: Vec<u8>,
_compression: &mut Option<HashMap<String, usize>>,
_compression_off: usize,
) -> Result<Vec<u8>> {
for opt in &self.options {
msg = pack_uint16(msg, opt.code);
msg = pack_uint16(msg, opt.data.len() as u16);
msg = pack_bytes(msg, &opt.data);
}
Ok(msg)
}
fn unpack(&mut self, msg: &[u8], mut off: usize, length: usize) -> Result<usize> {
let mut opts = vec![];
let old_off = off;
while off < old_off + length {
let (code, new_off) = unpack_uint16(msg, off)?;
off = new_off;
let (l, new_off) = unpack_uint16(msg, off)?;
off = new_off;
let mut opt = DnsOption {
code,
data: vec![0; l as usize],
};
if off + l as usize > msg.len() {
return Err(Error::ErrCalcLen);
}
opt.data.copy_from_slice(&msg[off..off + l as usize]);
off += l as usize;
opts.push(opt);
}
self.options = opts;
Ok(off)
}
}