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
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
pub use rdata::RData;
pub use record::Record;
pub use record_type::RecordType;
use crate::{Error, Name, Result};
use smallvec::SmallVec;
use std::num::ParseIntError;
pub mod rdata;
pub mod record;
pub mod record_type;
pub trait NameToIpAddr {
fn to_ip_addr(&self) -> Result<IpAddr>;
fn to_ip_addr_string(&self) -> String {
self.to_ip_addr()
.map(|x| x.to_string())
.unwrap_or_else(|_| "-".to_string())
}
}
impl NameToIpAddr for Name {
#[allow(clippy::many_single_char_names)]
fn to_ip_addr(&self) -> Result<IpAddr> {
let str = self.to_string();
let err = || Error::ParserError {
what: str.clone(),
to: "IpAddr",
why: "is not a ptr name".to_string(),
};
if str.ends_with(".in-addr.arpa.") {
let ip = &str.as_str()[..str.len() - 14];
let elements: SmallVec<[std::result::Result<u8, ParseIntError>; 4]> =
ip.splitn(4, '.').map(str::parse::<u8>).collect();
let octets: std::result::Result<SmallVec<[u8; 4]>, _> = elements.into_iter().collect();
let mut octets = octets.map_err(|_| err())?;
octets.reverse();
match *octets.as_slice() {
[a, b, c, d] => Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d))),
_ => Err(err()),
}
} else if str.ends_with(".ip6.arpa.") {
let ip = &str.as_str()[..str.len() - 10];
let elements: SmallVec<[&str; 32]> = ip.split('.').collect();
let nibble: SmallVec<[std::result::Result<u8, ParseIntError>; 32]> =
elements.into_iter().map(|x| u8::from_str_radix(&x, 16)).collect();
let nibble: std::result::Result<SmallVec<[u8; 32]>, _> = nibble.into_iter().collect();
let mut nibble = nibble.map_err(|_| err())?;
nibble.reverse();
let octets: SmallVec<[u8; 16]> = nibble
.as_slice()
.chunks_exact(2)
.map(|byte| byte[0] % 16 * 16 + byte[1] % 16)
.collect();
match *octets.as_slice() {
[a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p] => Ok(IpAddr::V6(Ipv6Addr::from([
a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p,
]))),
_ => Err(err()),
}
} else {
Err(err())
}
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use spectral::prelude::*;
use crate::IntoName;
use super::*;
#[test]
fn test_192_168_101_1() {
crate::utils::tests::logging::init();
let ip_expected = Ipv4Addr::new(192, 168, 101, 1);
let name: Name = ip_expected.into_name().unwrap();
let ip = name.to_ip_addr();
assert_that(&ip).is_ok().is_equal_to(IpAddr::from(ip_expected));
}
#[test]
#[allow(non_snake_case)]
fn test_2a0a_a955_bef6__ad7_fad3_c233_2c() {
crate::utils::tests::logging::init();
let ip_expected = Ipv6Addr::from_str("2a0a:a955:bef6::ad7:fad3:c233:2c").unwrap();
let name: Name = ip_expected.into_name().unwrap();
let ip = name.to_ip_addr();
assert_that(&ip).is_ok().is_equal_to(IpAddr::from(ip_expected));
}
#[test]
#[allow(non_snake_case)]
fn fuzz_a_a_aB_ip6_arpa_() {
crate::utils::tests::logging::init();
let name: Name = "a.a.aB.ip6.arpa.".into_name().unwrap();
let ip = name.to_ip_addr();
assert_that(&ip).is_err();
}
#[test]
#[allow(non_snake_case)]
fn fuzz_a_a_aa_a_ip6_arpa_() {
crate::utils::tests::logging::init();
let name: Name = "a.a.aa.a.ip6.arpa.".into_name().unwrap();
let ip = name.to_ip_addr();
assert_that(&ip).is_err();
}
}