diff --git a/src/networking/mod.rs b/src/networking/mod.rs index 95ebe83..52a5c2a 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -85,14 +85,15 @@ impl Network { // Determine the networking gaps //If there is only 1 network, returning the base network will be sufficient if num_networks > 1 { - let network_spacing: u32 = u32::pow(2, 32 - network_class as u32 - num_borrowed_bits as u32); - - for i in 0..=(num_borrowed_bits/8) as u8{ - //Add remaming subnets (-1 is because we have already added the first address) - for _ in 0..(num_networks-1) { - base_network[(most_sig_octet + i) as usize] += network_spacing as u8; - networks.push(IpAddr::from_arr(&base_network.clone())?); - } + //Get the base network and next added address as u32's + let mut base_address = u32::from(IpAddr::from_arr(&base_network)?); + let subnet_mask = u32::from(Network::gen_subnet_mask(cidr)?); + let addr_increment = u32::pow(2, subnet_mask.trailing_zeros()); + + for _ in 0..num_networks-1 { + //Increment address and append to list + base_address += addr_increment; + networks.push(IpAddr::from(base_address)); } } @@ -256,8 +257,12 @@ mod tests { assert_eq!(networks.len(), 2); assert_eq!(networks.first().unwrap(), &IpAddr::V4(127, 0, 0, 0)); + let networks = Network::create_subnet(&IpAddr::V4(168, 20, 0, 0), 17).unwrap(); + assert_eq!(networks.len(), 2); + assert_eq!(networks.last().unwrap(), &IpAddr::V4(168, 20, 128, 0)); + let networks = Network::create_subnet(&IpAddr::V4(127, 0, 0, 0), 17).unwrap(); - assert_eq!(networks.last().unwrap(), &IpAddr::V4(127, 0, 0, 0)); - assert_eq!(networks.len(), 1); + assert_eq!(networks.len(), 512); + assert_eq!(networks.last().unwrap(), &IpAddr::V4(127, 255, 128, 0)); } }