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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use crate::{
dns::{header::Header, PacketPart},
RCODE,
};
use std::{borrow::Cow, collections::HashMap};
use super::RR;
pub mod masks {
pub const RCODE_MASK: u32 = 0b0000_0000_0000_0000_0000_0000_1111_1111;
pub const VERSION_MASK: u32 = 0b0000_0000_0000_0000_1111_1111_0000_0000;
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct OPT<'a> {
pub opt_codes: Vec<OPTCode<'a>>,
pub udp_packet_size: u16,
pub version: u8,
}
impl<'a> RR for OPT<'a> {
const TYPE_CODE: u16 = 41;
}
impl<'a> PacketPart<'a> for OPT<'a> {
fn parse(data: &'a [u8], mut position: usize) -> crate::Result<Self>
where
Self: Sized,
{
if position < 8 {
return Err(crate::SimpleDnsError::InsufficientData);
}
let udp_packet_size = u16::from_be_bytes(data[position - 8..position - 6].try_into()?);
let ttl = u32::from_be_bytes(data[position - 6..position - 2].try_into()?);
let version = ((ttl & masks::VERSION_MASK) >> masks::VERSION_MASK.trailing_zeros()) as u8;
let mut opt_codes = Vec::new();
while position < data.len() {
if position + 4 > data.len() {
return Err(crate::SimpleDnsError::InsufficientData);
}
let code = u16::from_be_bytes(data[position..position + 2].try_into()?);
let length = u16::from_be_bytes(data[position + 2..position + 4].try_into()?) as usize;
if position + 4 + length > data.len() {
return Err(crate::SimpleDnsError::InsufficientData);
}
let inner_data = Cow::Borrowed(&data[position + 4..position + 4 + length]);
opt_codes.push(OPTCode {
code,
data: inner_data,
});
position += 4 + length as usize;
}
Ok(Self {
opt_codes,
udp_packet_size,
version,
})
}
fn append_to_vec(
&self,
out: &mut Vec<u8>,
_name_refs: &mut Option<&mut HashMap<u64, usize>>,
) -> crate::Result<()> {
for code in self.opt_codes.iter() {
out.extend(code.code.to_be_bytes());
out.extend((code.data.len() as u16).to_be_bytes());
out.extend(&code.data[..]);
}
Ok(())
}
fn len(&self) -> usize {
self.opt_codes.iter().map(|o| o.data.len() + 4).sum()
}
}
impl<'a> OPT<'a> {
pub(crate) fn extract_rcode_from_ttl(ttl: u32, header: &Header) -> RCODE {
let mut rcode = (ttl & masks::RCODE_MASK) << 4;
rcode |= header.response_code as u32;
RCODE::from(rcode as u16)
}
pub(crate) fn encode_ttl(&self, header: &Header) -> u32 {
let mut ttl: u32 = (header.response_code as u32 & masks::RCODE_MASK) >> 4;
ttl |= (self.version as u32) << masks::VERSION_MASK.trailing_zeros();
ttl
}
pub fn into_owned<'b>(self) -> OPT<'b> {
OPT {
udp_packet_size: self.udp_packet_size,
version: self.version,
opt_codes: self.opt_codes.into_iter().map(|o| o.into_owned()).collect(),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct OPTCode<'a> {
pub code: u16,
pub data: Cow<'a, [u8]>,
}
impl<'a> OPTCode<'a> {
pub fn into_owned<'b>(self) -> OPTCode<'b> {
OPTCode {
code: self.code,
data: self.data.into_owned().into(),
}
}
}
#[cfg(test)]
mod tests {
use crate::{rdata::RData, Name, ResourceRecord};
use super::*;
#[test]
fn parse_and_write_opt_empty() {
let header = Header::new_reply(1, crate::OPCODE::StandardQuery);
let opt = OPT {
udp_packet_size: 500,
version: 2,
opt_codes: Vec::new(),
};
let opt_rr = ResourceRecord {
ttl: opt.encode_ttl(&header),
name: Name::new_unchecked("."),
class: crate::CLASS::IN,
cache_flush: false,
rdata: RData::OPT(opt),
};
let mut data = Vec::new();
assert!(opt_rr.append_to_vec(&mut data, &mut None).is_ok());
let opt = match ResourceRecord::parse(&data, 0)
.expect("failed to parse")
.rdata
{
RData::OPT(rdata) => rdata,
_ => unreachable!(),
};
assert_eq!(data.len(), opt_rr.len());
assert_eq!(500, opt.udp_packet_size);
assert_eq!(2, opt.version);
assert!(opt.opt_codes.is_empty());
}
#[test]
fn parse_and_write_opt() {
let header = Header::new_reply(1, crate::OPCODE::StandardQuery);
let opt = OPT {
udp_packet_size: 500,
version: 2,
opt_codes: vec![
OPTCode {
code: 1,
data: Cow::Owned(vec![255, 255]),
},
OPTCode {
code: 2,
data: Cow::Owned(vec![255, 255, 255]),
},
],
};
let opt_rr = ResourceRecord {
ttl: opt.encode_ttl(&header),
name: Name::new_unchecked("."),
class: crate::CLASS::IN,
cache_flush: false,
rdata: RData::OPT(opt),
};
let mut data = Vec::new();
assert!(opt_rr.append_to_vec(&mut data, &mut None).is_ok());
let mut opt = match ResourceRecord::parse(&data, 0)
.expect("failed to parse")
.rdata
{
RData::OPT(rdata) => rdata,
_ => unreachable!(),
};
assert_eq!(data.len(), opt_rr.len());
assert_eq!(500, opt.udp_packet_size);
assert_eq!(2, opt.version);
assert_eq!(2, opt.opt_codes.len());
let opt_code = opt.opt_codes.pop().unwrap();
assert_eq!(2, opt_code.code);
assert_eq!(vec![255, 255, 255], *opt_code.data);
let opt_code = opt.opt_codes.pop().unwrap();
assert_eq!(1, opt_code.code);
assert_eq!(vec![255, 255], *opt_code.data);
}
}