From 3984451d93aa6f8c185bb60fe76018ebad62aa6d Mon Sep 17 00:00:00 2001 From: Abhishek Chanda Date: Sun, 26 Apr 2015 00:02:03 -0700 Subject: [PATCH] Add a method to get network address as an int Also fix a bug with IP to int conversion --- src/ipnetwork.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ipnetwork.rs b/src/ipnetwork.rs index 58e500d..d7bdc72 100644 --- a/src/ipnetwork.rs +++ b/src/ipnetwork.rs @@ -41,9 +41,13 @@ impl Ipv4Network { 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 { 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 { @@ -129,4 +133,10 @@ mod test { let cidr = Ipv4Network::new(Ipv4Addr::new(74, 125, 227, 0), 29); 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); + } }