mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-16 08:48:51 +00:00
Add a method to get network address as an int
Also fix a bug with IP to int conversion
This commit is contained in:
@ -41,9 +41,13 @@ impl Ipv4Network {
|
|||||||
Ipv4Network::int_to_ip(mask).to_string()
|
Ipv4Network::int_to_ip(mask).to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn network_int(&self) -> u32 {
|
||||||
|
Ipv4Network::ip_to_int(*(self.ip())) & self.mask()
|
||||||
|
}
|
||||||
|
|
||||||
fn ip_to_int(addr: Ipv4Addr) -> u32 {
|
fn ip_to_int(addr: Ipv4Addr) -> u32 {
|
||||||
let ip = addr.octets();
|
let ip = addr.octets();
|
||||||
(ip[0] as u32) << 24 + (ip[1] as u32) << 16 + (ip[2] as u32) << 8 + (ip[3] as u32)
|
((ip[0] as u32) << 24) + ((ip[1] as u32) << 16) + ((ip[2] as u32) << 8) + (ip[3] as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn int_to_ip(ip: u32) -> Ipv4Addr {
|
fn int_to_ip(ip: u32) -> Ipv4Addr {
|
||||||
@ -129,4 +133,10 @@ mod test {
|
|||||||
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 29);
|
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 29);
|
||||||
assert_eq!(cidr.mask_to_string(), "255.255.255.248");
|
assert_eq!(cidr.mask_to_string(), "255.255.255.248");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn network_as_int() {
|
||||||
|
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 25);
|
||||||
|
assert_eq!(cidr.network_int(), 1249764096);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user