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
use crate::error::*;
use crate::rr::rdata::opt::{self, EdnsCode, EdnsOption};
use crate::rr::rdata::OPT;
use crate::rr::{DNSClass, Name, RData, Record, RecordType};
use crate::serialize::binary::{BinEncodable, BinEncoder};
#[derive(Debug, PartialEq, Clone)]
pub struct Edns {
rcode_high: u8,
version: u8,
dnssec_ok: bool,
max_payload: u16,
options: OPT,
}
impl Default for Edns {
fn default() -> Self {
Edns {
rcode_high: 0,
version: 0,
dnssec_ok: false,
max_payload: 512,
options: OPT::default(),
}
}
}
impl Edns {
pub fn new() -> Self {
Default::default()
}
pub fn rcode_high(&self) -> u8 {
self.rcode_high
}
pub fn version(&self) -> u8 {
self.version
}
pub fn dnssec_ok(&self) -> bool {
self.dnssec_ok
}
pub fn max_payload(&self) -> u16 {
self.max_payload
}
pub fn option(&self, code: EdnsCode) -> Option<&EdnsOption> {
self.options.get(code)
}
pub fn options(&self) -> &OPT {
&self.options
}
pub fn set_rcode_high(&mut self, rcode_high: u8) {
self.rcode_high = rcode_high
}
pub fn set_version(&mut self, version: u8) {
self.version = version
}
pub fn set_dnssec_ok(&mut self, dnssec_ok: bool) {
self.dnssec_ok = dnssec_ok
}
pub fn set_max_payload(&mut self, max_payload: u16) {
self.max_payload = max_payload.max(512);
}
pub fn set_option(&mut self, option: EdnsOption) {
self.options.insert(option);
}
}
impl<'a> From<&'a Record> for Edns {
fn from(value: &'a Record) -> Self {
assert!(value.rr_type() == RecordType::OPT);
let rcode_high: u8 = ((value.ttl() & 0xFF00_0000u32) >> 24) as u8;
let version: u8 = ((value.ttl() & 0x00FF_0000u32) >> 16) as u8;
let dnssec_ok: bool = value.ttl() & 0x0000_8000 == 0x0000_8000;
let max_payload: u16 = u16::from(value.dns_class());
let options: OPT = match *value.rdata() {
RData::NULL(..) => {
OPT::default()
}
RData::OPT(ref option_data) => {
option_data.clone()
}
_ => {
panic!("rr_type doesn't match the RData: {:?}", value.rdata())
}
};
Edns {
rcode_high,
version,
dnssec_ok,
max_payload,
options,
}
}
}
impl<'a> From<&'a Edns> for Record {
fn from(value: &'a Edns) -> Record {
let mut record: Record = Record::new();
record.set_name(Name::root());
record.set_rr_type(RecordType::OPT);
record.set_dns_class(DNSClass::for_opt(value.max_payload()));
let mut ttl: u32 = u32::from(value.rcode_high()) << 24;
ttl |= u32::from(value.version()) << 16;
if value.dnssec_ok() {
ttl |= 0x0000_8000;
}
record.set_ttl(ttl);
record.set_rdata(RData::OPT(value.options().clone()));
record
}
}
impl BinEncodable for Edns {
fn emit(&self, encoder: &mut BinEncoder) -> ProtoResult<()> {
encoder.emit(0)?;
RecordType::OPT.emit(encoder)?;
DNSClass::for_opt(self.max_payload()).emit(encoder)?;
let mut ttl: u32 = u32::from(self.rcode_high()) << 24;
ttl |= u32::from(self.version()) << 16;
if self.dnssec_ok() {
ttl |= 0x0000_8000;
}
encoder.emit_u32(ttl)?;
let place = encoder.place::<u16>()?;
opt::emit(encoder, &self.options)?;
let len = encoder.len_since_place(&place);
assert!(len <= u16::max_value() as usize);
place.replace(encoder, len as u16)?;
Ok(())
}
}
#[cfg(feature = "dnssec")]
#[test]
fn test_encode_decode() {
use crate::rr::dnssec::SupportedAlgorithms;
let mut edns: Edns = Edns::new();
edns.set_dnssec_ok(true);
edns.set_max_payload(0x8008);
edns.set_version(0x40);
edns.set_rcode_high(0x01);
edns.set_option(EdnsOption::DAU(SupportedAlgorithms::all()));
let record: Record = (&edns).into();
let edns_decode: Edns = (&record).into();
assert_eq!(edns.dnssec_ok(), edns_decode.dnssec_ok());
assert_eq!(edns.max_payload(), edns_decode.max_payload());
assert_eq!(edns.version(), edns_decode.version());
assert_eq!(edns.rcode_high(), edns_decode.rcode_high());
assert_eq!(edns.options(), edns_decode.options());
}