2023-02-20 18:06:57 +00:00
|
|
|
use std::{str::FromStr};
|
2023-02-11 16:15:28 +00:00
|
|
|
use ip::IpAddr;
|
|
|
|
|
2023-04-09 09:07:15 +00:00
|
|
|
use self::ip::InvalidIPErr;
|
|
|
|
|
2023-02-20 18:06:57 +00:00
|
|
|
pub mod ip;
|
|
|
|
|
2023-02-11 16:15:28 +00:00
|
|
|
#[allow(unused)]
|
|
|
|
pub struct Network {
|
|
|
|
network_address: IpAddr,
|
|
|
|
broadcast_addr: IpAddr,
|
|
|
|
subnet_mask: Option<IpAddr>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Network {
|
2023-04-09 09:07:15 +00:00
|
|
|
// pub fn new(given_address: IpAddr, CIDR: u8) -> Network {
|
|
|
|
// //Generate subnet Mask
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
/// Function that takes in a u8 CIDR and converts it to an IP address.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let CIDR: u8 = 22
|
|
|
|
/// let subnet_mask: [u8; 4] = gen_subnet_mask(CIDR);
|
|
|
|
///
|
|
|
|
/// >> IpAddr::V4(255, 255, 252, 0)
|
|
|
|
/// ```
|
|
|
|
pub fn gen_subnet_mask(mut CIDR: u8) -> Result<IpAddr, InvalidIPErr> {
|
|
|
|
if CIDR > 32 {
|
|
|
|
return Err(InvalidIPErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut oct: [u8; 4] = [0; 4];
|
|
|
|
|
|
|
|
for octet in oct.iter_mut() {
|
|
|
|
*octet = if usize::from(CIDR) >= 8 {
|
|
|
|
CIDR -= 8;
|
|
|
|
u8::MAX
|
|
|
|
}else{
|
|
|
|
// Count the number of remaining 1s and convert to binary
|
|
|
|
let mut count: u8 = 0;
|
|
|
|
for i in ((8-CIDR)..8).rev() {
|
|
|
|
count += u8::pow(2, u32::from(i));
|
|
|
|
}
|
|
|
|
CIDR = 0;
|
|
|
|
count
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(IpAddr::V4(oct[0], oct[1], oct[2], oct[3]))
|
|
|
|
}
|
|
|
|
//pub fn generate_subnets(self) -> Vec<Network> {}
|
|
|
|
//fn get_net_id(self) -> u8 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum NetworkClassBits {
|
|
|
|
A = 8,
|
|
|
|
B = 16,
|
|
|
|
C = 24
|
2023-02-20 18:06:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 21:25:37 +00:00
|
|
|
/// Function that takes in a string reference and returns the result of splitting a string into
|
|
|
|
/// both its Address and CIDR
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let ip_string = String::from("192.168.0.1/24");
|
|
|
|
/// let result = match ip_and_cidr_from_string(&ip_string) {
|
|
|
|
/// Err(_) => panic!(),
|
|
|
|
/// Ok(ip_and_cidr) => ip_and_cidr
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// >> (IpAddr::V4(192, 168, 0, 1), 24)
|
|
|
|
/// ```
|
2023-02-20 18:06:57 +00:00
|
|
|
pub fn ip_and_cidr_from_string(ip_and_cidr: &String) -> Result<(IpAddr, u8), ip::InvalidIPErr>{
|
|
|
|
let mut cidr: u8 = Default::default();
|
|
|
|
let mut ip: String = Default::default();
|
|
|
|
|
|
|
|
if ip_and_cidr.contains("/") {
|
|
|
|
let split_ip_cidr = ip_and_cidr.split("/");
|
|
|
|
if split_ip_cidr.clone().count() > 2 {
|
|
|
|
return Err(ip::InvalidIPErr);
|
|
|
|
}
|
|
|
|
ip = split_ip_cidr.clone().into_iter().next().unwrap_or("0.0.0.0").to_owned();
|
|
|
|
cidr = match split_ip_cidr.into_iter().last() {
|
|
|
|
None => return Err(ip::InvalidIPErr),
|
|
|
|
Some(cidr) => match cidr.trim().parse::<u8>() {
|
|
|
|
Err(_) => return Err(ip::InvalidIPErr),
|
|
|
|
Ok(cidr) => cidr
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let ip_address: IpAddr = match IpAddr::from_str(&ip.trim()) {
|
|
|
|
Err(_) => return Err(ip::InvalidIPErr),
|
|
|
|
Ok(ip) => ip,
|
|
|
|
};
|
|
|
|
|
|
|
|
return Ok((ip_address, cidr))
|
|
|
|
}
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
#[cfg(test)]
|
|
|
|
#[test]
|
|
|
|
fn string_to_ip_cidr_test() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
let ip_string = String::from("127.0.0.1/8");
|
|
|
|
let result = match ip_and_cidr_from_string(&ip_string) {
|
|
|
|
Err(_) => panic!(),
|
|
|
|
Ok(ip_and_cidr) => ip_and_cidr
|
|
|
|
};
|
|
|
|
assert_eq!(result.0, IpAddr::V4(127, 0, 0, 1));
|
|
|
|
assert_eq!(result.1, 8);
|
|
|
|
}
|
2023-04-09 09:07:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cidr_to_ip_test() {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
assert_eq!(Network::gen_subnet_mask(22).unwrap(), IpAddr::V4(255, 255, 252, 0));
|
|
|
|
assert_eq!(Network::gen_subnet_mask(24).unwrap(), IpAddr::V4(255, 255, 255, 0));
|
|
|
|
assert_eq!(Network::gen_subnet_mask(0).unwrap(), IpAddr::V4(0, 0, 0, 0));
|
|
|
|
assert_eq!(Network::gen_subnet_mask(4).unwrap(), IpAddr::V4(240, 0, 0, 0));
|
|
|
|
assert_eq!(Network::gen_subnet_mask(35).unwrap_err(), InvalidIPErr);
|
|
|
|
}
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|