From aad67836df8126ca6012af1dca2ea0d26d0a0de1 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 30 Apr 2023 14:16:24 +0100 Subject: [PATCH] create_subnet function now works when the CIDR borrows 8 or less bits, need to figure out a way to have it so that we can modify the other octest sequentially --- src/main.rs | 5 ----- src/networking/mod.rs | 11 +++++++---- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 75699f3..d34b067 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,11 +7,6 @@ mod networking; #[allow(unused_variables, unused_mut, unused_assignments)] fn main() { - - let networks = Network::create_subnet(&networking::ip::IpAddr::V4(192, 222, 222, 222), 25); - if networks.is_err() { - println!("Hello!"); - } println!("Enter the IP and cidr for your given network"); let ip_and_cidr: (networking::ip::IpAddr, u8); diff --git a/src/networking/mod.rs b/src/networking/mod.rs index 88eff8e..c01c42c 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -55,7 +55,7 @@ impl Network { //Get number of host and network bits. let network_class = Network::get_network_class(network_address)?; let network_address = network_address.to_arr()?; - let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?; + //let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?; // If the CIDR < class network enum associated value, then there is an invalid subnet. if cidr <= network_class as u8 { @@ -89,11 +89,14 @@ impl Network { let network_spacing: u8 = u8::pow(2, 8 - num_borrowed_bits as u32); //Create the subnets - let mut networks = vec![]; - for i in 0..num_networks { - networks.push(IpAddr::from_arr(&base_network.clone())?); + let mut networks = vec![IpAddr::from_arr(&base_network)?]; + + //Add remaming subnets (-1 is because we have already added the first address) + for _ in 0..(num_networks-1) { base_network[most_sig_octet as usize] += network_spacing; + networks.push(IpAddr::from_arr(&base_network.clone())?); } + println!("{:#?}", networks); Ok(networks) }