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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use proto::rr::domain::{Label, Name};
pub use proto::rr::domain::usage::*;
use proto::serialize::binary::BinEncodable;
use radix_trie::{Trie, TrieKey};
lazy_static! {
pub static ref IN_ADDR_ARPA_10: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("10").unwrap().append_domain(&*IN_ADDR_ARPA));
static ref IN_ADDR_ARPA_172: Name = Name::from_ascii("172").unwrap().append_domain(&*IN_ADDR_ARPA);
pub static ref IN_ADDR_ARPA_172_16: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("16").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_17: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("17").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_18: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("18").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_19: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("19").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_20: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("20").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_21: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("21").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_22: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("22").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_23: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("23").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_24: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("24").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_25: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("25").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_26: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("26").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_27: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("27").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_28: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("28").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_29: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("29").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_30: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("30").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_172_31: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("31").unwrap().append_domain(&*IN_ADDR_ARPA_172));
pub static ref IN_ADDR_ARPA_192_168: ZoneUsage = ZoneUsage::reverse(Name::from_ascii("168.192").unwrap().append_domain(&*IN_ADDR_ARPA));
}
lazy_static! {
static ref COM: Label = Label::from_ascii("com").unwrap();
static ref NET: Label = Label::from_ascii("net").unwrap();
static ref ORG: Label = Label::from_ascii("org").unwrap();
static ref EXAMPLE_L: Label = Label::from_ascii("example").unwrap();
pub static ref EXAMPLE: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone()]).unwrap());
pub static ref EXAMPLE_COM: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), COM.clone()]).unwrap());
pub static ref EXAMPLE_NET: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), NET.clone()]).unwrap());
pub static ref EXAMPLE_ORG: ZoneUsage = ZoneUsage::example(Name::from_labels(vec![EXAMPLE_L.clone(), ORG.clone()]).unwrap());
}
lazy_static! {
pub static ref TEST: ZoneUsage = ZoneUsage::test(Name::from_ascii("test.").unwrap());
}
#[derive(Clone, Eq, PartialEq)]
struct TrieName(Name);
impl From<Name> for TrieName {
fn from(n: Name) -> Self {
TrieName(n)
}
}
impl TrieKey for TrieName {
fn encode_bytes(&self) -> Vec<u8> {
let mut bytes = self.0.to_bytes().expect("bad name for trie");
bytes.reverse();
bytes
}
}
#[derive(Clone, Eq, PartialEq)]
struct TrieNameRef<'n>(&'n Name);
impl<'n> From<&'n Name> for TrieNameRef<'n> {
fn from(n: &'n Name) -> Self {
TrieNameRef(n)
}
}
impl<'n> TrieKey for TrieNameRef<'n> {
fn encode_bytes(&self) -> Vec<u8> {
let mut bytes = self.0.to_bytes().expect("bad name for trie");
bytes.reverse();
bytes
}
}
pub struct UsageTrie(Trie<TrieName, &'static ZoneUsage>);
impl UsageTrie {
fn default() -> Self {
let mut trie: Trie<TrieName, &'static ZoneUsage> = Trie::new();
assert!(trie.insert(DEFAULT.clone().into(), &DEFAULT).is_none());
assert!(trie.insert(IN_ADDR_ARPA_10.clone().into(), &IN_ADDR_ARPA_10).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_16.clone().into(), &IN_ADDR_ARPA_172_16).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_17.clone().into(), &IN_ADDR_ARPA_172_17).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_18.clone().into(), &IN_ADDR_ARPA_172_18).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_19.clone().into(), &IN_ADDR_ARPA_172_19).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_20.clone().into(), &IN_ADDR_ARPA_172_20).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_21.clone().into(), &IN_ADDR_ARPA_172_21).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_22.clone().into(), &IN_ADDR_ARPA_172_22).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_23.clone().into(), &IN_ADDR_ARPA_172_23).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_24.clone().into(), &IN_ADDR_ARPA_172_24).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_25.clone().into(), &IN_ADDR_ARPA_172_25).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_26.clone().into(), &IN_ADDR_ARPA_172_26).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_27.clone().into(), &IN_ADDR_ARPA_172_27).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_28.clone().into(), &IN_ADDR_ARPA_172_28).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_29.clone().into(), &IN_ADDR_ARPA_172_29).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_30.clone().into(), &IN_ADDR_ARPA_172_30).is_none());
assert!(trie.insert(IN_ADDR_ARPA_172_31.clone().into(), &IN_ADDR_ARPA_172_31).is_none());
assert!(trie.insert(IN_ADDR_ARPA_192_168.clone().into(), &IN_ADDR_ARPA_192_168).is_none());
assert!(trie.insert(TEST.clone().into(), &TEST).is_none());
assert!(trie.insert(LOCALHOST.clone().into(), &LOCALHOST).is_none());
assert!(trie.insert(IN_ADDR_ARPA_127.clone().into(), &IN_ADDR_ARPA_127).is_none());
assert!(trie.insert(IP6_ARPA_1.clone().into(), &IP6_ARPA_1).is_none());
assert!(trie.insert(INVALID.clone().into(), &INVALID).is_none());
assert!(trie.insert(EXAMPLE.clone().into(), &EXAMPLE).is_none());
assert!(trie.insert(EXAMPLE_COM.clone().into(), &EXAMPLE_COM).is_none());
assert!(trie.insert(EXAMPLE_NET.clone().into(), &EXAMPLE_NET).is_none());
assert!(trie.insert(EXAMPLE_ORG.clone().into(), &EXAMPLE_ORG).is_none());
UsageTrie(trie)
}
pub fn get(&self, name: &Name) -> &'static ZoneUsage {
self.0.get_ancestor_value(&TrieName::from(name.clone())).expect("DEFAULT root ZoneUsage should have been returned")
}
}
lazy_static!{
pub static ref USAGE: UsageTrie = UsageTrie::default();
}
#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, Ipv6Addr};
use super::*;
#[test]
fn test_root() {
let name = Name::from_ascii("com.").unwrap();
let usage = USAGE.get(&name);
assert!(usage.is_root());
}
#[test]
fn test_local_networks() {
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(9,0,0,1))).name(), DEFAULT.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(10,0,0,1))).name(), IN_ADDR_ARPA_10.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(11,0,0,1))).name(), DEFAULT.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,16,0,0))).name(), IN_ADDR_ARPA_172_16.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,17,0,0))).name(), IN_ADDR_ARPA_172_17.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,18,0,0))).name(), IN_ADDR_ARPA_172_18.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,19,0,0))).name(), IN_ADDR_ARPA_172_19.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,20,0,0))).name(), IN_ADDR_ARPA_172_20.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,21,0,0))).name(), IN_ADDR_ARPA_172_21.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,22,0,0))).name(), IN_ADDR_ARPA_172_22.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,23,0,0))).name(), IN_ADDR_ARPA_172_23.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,24,0,0))).name(), IN_ADDR_ARPA_172_24.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,25,0,0))).name(), IN_ADDR_ARPA_172_25.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,26,0,0))).name(), IN_ADDR_ARPA_172_26.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,27,0,0))).name(), IN_ADDR_ARPA_172_27.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,28,0,0))).name(), IN_ADDR_ARPA_172_28.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,29,0,0))).name(), IN_ADDR_ARPA_172_29.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,30,0,0))).name(), IN_ADDR_ARPA_172_30.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,31,0,0))).name(), IN_ADDR_ARPA_172_31.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,15,0,0))).name(), DEFAULT.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(172,32,0,0))).name(), DEFAULT.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(192,167,255,255))).name(), DEFAULT.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(192,168,2,3))).name(), IN_ADDR_ARPA_192_168.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(192,169,0,0))).name(), DEFAULT.name());
}
#[test]
fn test_example() {
let name = Name::from_ascii("example.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), EXAMPLE.name());
let name = Name::from_ascii("example.com.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), EXAMPLE_COM.name());
let name = Name::from_ascii("example.net.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), EXAMPLE_NET.name());
let name = Name::from_ascii("example.org.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), EXAMPLE_ORG.name());
let name = Name::from_ascii("www.example.org.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), EXAMPLE_ORG.name());
}
#[test]
fn test_localhost() {
let name = Name::from_ascii("localhost.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), LOCALHOST.name());
let name = Name::from_ascii("this.localhost.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), LOCALHOST.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(127,0,0,1))).name(), IN_ADDR_ARPA_127.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(127,0,0,2))).name(), IN_ADDR_ARPA_127.name());
assert_eq!(USAGE.get(&Name::from(Ipv4Addr::new(127,255,0,0))).name(), IN_ADDR_ARPA_127.name());
assert_eq!(USAGE.get(&Name::from(Ipv6Addr::new(0,0,0,0,0,0,0,1))).name(), IP6_ARPA_1.name());
}
#[test]
fn test_invalid() {
let name = Name::from_ascii("invalid.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), INVALID.name());
let name = Name::from_ascii("something.invalid.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), INVALID.name());
}
#[test]
fn test_test() {
let name = Name::from_ascii("test.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), TEST.name());
let name = Name::from_ascii("foo.bar.test.").unwrap();
let usage = USAGE.get(&name);
assert_eq!(usage.name(), TEST.name());
}
}