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

This commit is contained in:
Luke Else 2023-04-30 14:16:24 +01:00
parent 2dcbd405e4
commit aad67836df
2 changed files with 7 additions and 9 deletions

View File

@ -7,11 +7,6 @@ mod networking;
#[allow(unused_variables, unused_mut, unused_assignments)] #[allow(unused_variables, unused_mut, unused_assignments)]
fn main() { 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"); println!("Enter the IP and cidr for your given network");
let ip_and_cidr: (networking::ip::IpAddr, u8); let ip_and_cidr: (networking::ip::IpAddr, u8);

View File

@ -55,7 +55,7 @@ impl Network {
//Get number of host and network bits. //Get number of host and network bits.
let network_class = Network::get_network_class(network_address)?; let network_class = Network::get_network_class(network_address)?;
let network_address = network_address.to_arr()?; 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 the CIDR < class network enum associated value, then there is an invalid subnet.
if cidr <= network_class as u8 { 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); let network_spacing: u8 = u8::pow(2, 8 - num_borrowed_bits as u32);
//Create the subnets //Create the subnets
let mut networks = vec![]; let mut networks = vec![IpAddr::from_arr(&base_network)?];
for i in 0..num_networks {
networks.push(IpAddr::from_arr(&base_network.clone())?); //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; base_network[most_sig_octet as usize] += network_spacing;
networks.push(IpAddr::from_arr(&base_network.clone())?);
} }
println!("{:#?}", networks);
Ok(networks) Ok(networks)
} }