#2 Completed the implementation of create_subnet function, all subnet network addresses are now ALL correctly appended to the vector. Would like to add some more resilient unit testing at some point down the line to verify the accuracy / robustness of the function
This commit is contained in:
parent
8c88d7945a
commit
625233ab86
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user