mirror of
https://github.com/achanda/ipnetwork.git
synced 2025-06-16 16:58:50 +00:00
Add a method to get network as an IPv4 address
This commit is contained in:
@ -45,6 +45,10 @@ impl Ipv4Network {
|
|||||||
Ipv4Network::ip_to_int(*(self.ip())) & self.mask()
|
Ipv4Network::ip_to_int(*(self.ip())) & self.mask()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn network(&self) -> Ipv4Addr {
|
||||||
|
Ipv4Network::int_to_ip(self.network_int())
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
@ -135,8 +139,14 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn network_as_int() {
|
fn network_int_v4() {
|
||||||
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 25);
|
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 25);
|
||||||
assert_eq!(cidr.network_int(), 1249764096);
|
assert_eq!(cidr.network_int(), 1249764096);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn network_v4() {
|
||||||
|
let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 25);
|
||||||
|
assert_eq!(cidr.network(), Ipv4Addr::new(74, 125, 227, 0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user