-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetwork.rs
62 lines (54 loc) · 1.55 KB
/
network.rs
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
use config::NETWORK_MAGIC_MAC_START;
use std::net::Ipv4Addr;
use std::num::ParseIntError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MacToIpError {
#[error("Could not parse octed from string: {}", self)]
ParseIntError(#[from] ParseIntError),
#[error("No valid IP adddress in mac address: {}", .0)]
NoIpInMac(String),
}
// Convert an IP address to a MAC address
pub fn ip_to_mac(ip: &Ipv4Addr) -> String {
format!(
"{}:{:02x}:{:02x}:{:02x}:{:02x}",
NETWORK_MAGIC_MAC_START,
ip.octets()[0],
ip.octets()[1],
ip.octets()[2],
ip.octets()[3]
)
}
// Convert an IP address to a MAC address
pub fn mac_to_ip(mac: &str) -> Result<Ipv4Addr, MacToIpError> {
let octets = mac
.replace("06:00:", "")
.split(':')
.map(|octet| u8::from_str_radix(octet, 16))
.collect::<Result<Vec<u8>, ParseIntError>>()
.map_err(|_| MacToIpError::NoIpInMac(mac.to_string()))?;
let address = Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]);
Ok(address)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mac_to_ip() {
let mac = "06:00:ac:10:c9:01";
let ip = super::mac_to_ip(mac).unwrap();
assert_eq!(ip, Ipv4Addr::new(172, 16, 201, 1));
}
#[test]
fn test_ip_to_mac() {
assert_eq!(
ip_to_mac(&Ipv4Addr::new(172, 16, 0, 1)),
"06:00:ac:10:00:01"
);
assert_eq!(
ip_to_mac(&Ipv4Addr::new(172, 16, 10, 2)),
"06:00:ac:10:0a:02"
);
}
}