diff --git a/src/networking/mod.rs b/src/networking/mod.rs index fdd32d4..a9097ba 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -1,8 +1,6 @@ -use std::{str::FromStr, f32::consts::E}; +use std::str::FromStr; use ip::IpAddr; -use self::ip::InvalidIPErr; - pub mod ip; #[allow(dead_code)] @@ -15,7 +13,7 @@ pub enum NetworkingErr { } /// enum to allow the identification of the class of the network -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] enum NetworkClass { A = 8, B = 16, @@ -23,6 +21,7 @@ enum NetworkClass { } #[allow(unused)] +#[derive(Debug)] pub struct Network { network_address: IpAddr, broadcast_addr: IpAddr, @@ -54,11 +53,21 @@ impl Network { /// ``` pub fn create_subnet(network_address: &IpAddr, cidr: u8) -> Result, NetworkingErr> { //Get number of host and network bits. - let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?; + 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()?; - // Determine the network class that the given address is in. + // If the CIDR < class network enum associated value, then there is an invalid subnet. + if cidr <= network_class as u8 { + return Err(NetworkingErr::InvalidCIDRErr) + } + + let num_borrowed_bits = cidr - network_class as u8; + // Determine the starting network + // Determine the networking gaps + // Determine how many networks available + let networks = vec![]; Ok(networks) } @@ -184,8 +193,9 @@ mod tests { } #[test] - fn test_network_class() { + fn network_class_test() { use super::*; + assert_eq!(Network::get_network_class(&IpAddr::V4(127, 0, 0, 1)).unwrap(), NetworkClass::A); assert_eq!(Network::get_network_class(&IpAddr::V4(172, 6, 8, 10)).unwrap(), NetworkClass::B); assert_eq!(Network::get_network_class(&IpAddr::V4(192, 168, 0, 1)).unwrap(), NetworkClass::C); @@ -195,4 +205,11 @@ mod tests { assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 0)).unwrap_err(), NetworkingErr::InvalidIPErr); assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 1)).unwrap_err(), NetworkingErr::InvalidIPErr); } + + #[test] + fn subnet_creation_test() { + use super::*; + + assert_eq!(Network::create_subnet(&IpAddr::V4(192, 168, 0, 1), 23).unwrap_err(), NetworkingErr::InvalidCIDRErr); + } } \ No newline at end of file