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
use std::net::IpAddr;
use crate::{Error, Result};
pub fn is_global_ip(ip: &IpAddr) -> bool {
if !ip.is_unspecified() && !ip.is_loopback() && !ip.is_multicast() {
match *ip {
IpAddr::V4(ref ip) => !ip.is_broadcast() && !ip.is_link_local() && !ip.is_private(),
IpAddr::V6(_) => true,
}
} else {
false
}
}
pub fn read_hex(s: &str) -> Result<Vec<u8>> {
if s.chars().nth(2) == Some(':') {
Ok(s.split(':')
.map(|s| u8::from_str_radix(s, 16))
.collect::<::std::result::Result<Vec<_>, _>>()
.map_err(Error::InvalidHex)?)
} else if s.chars().nth(2) == Some(' ') {
Ok(s.split(' ')
.map(|s| u8::from_str_radix(s, 16))
.collect::<::std::result::Result<Vec<_>, _>>()
.map_err(Error::InvalidHex)?)
} else if s.starts_with("0x") {
let s: String = s.chars().filter(|c| !c.is_whitespace()).collect();
Ok(s[2..]
.split(',')
.map(|s| u8::from_str_radix(s.trim_start_matches("0x"), 16))
.collect::<::std::result::Result<Vec<_>, _>>()
.map_err(Error::InvalidHex)?)
} else {
let s: String = s.chars().filter(|c| !c.is_whitespace()).collect();
Ok(s.as_bytes()
.chunks(2)
.map(|s| u8::from_str_radix(::std::str::from_utf8(s).unwrap(), 16))
.collect::<::std::result::Result<Vec<_>, _>>()
.map_err(Error::InvalidHex)?)
}
}