2023-02-11 16:15:28 +00:00
|
|
|
use std::str::FromStr;
|
2023-04-17 16:24:35 +00:00
|
|
|
use super::NetworkingErr;
|
2023-02-11 16:15:28 +00:00
|
|
|
|
2023-02-11 16:49:23 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
|
|
|
pub struct InvalidIPErr;
|
|
|
|
|
2023-02-11 16:15:28 +00:00
|
|
|
/// Ip address enum that includes associated type
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// //Loopback Addresses:
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
/// use std::str::FromStr;
|
|
|
|
///
|
|
|
|
/// IpAddr::V4(127, 0, 0, 1);
|
|
|
|
/// IpAddr::V6(String::from("::1"));
|
2023-02-11 16:15:28 +00:00
|
|
|
/// ```
|
|
|
|
#[allow(unused)]
|
2023-04-17 16:24:35 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
2023-02-11 16:15:28 +00:00
|
|
|
pub enum IpAddr {
|
|
|
|
V4(u8, u8, u8, u8),
|
|
|
|
V6(String)
|
|
|
|
}
|
|
|
|
|
2023-04-20 20:42:13 +00:00
|
|
|
impl IpAddr {
|
|
|
|
/// Function that generates ar array of 4 octets / error
|
|
|
|
/// from an IpAddr
|
|
|
|
///
|
|
|
|
/// # Limitation
|
|
|
|
/// Currently only works for IPs of type IPv4
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
///
|
2023-04-20 20:42:13 +00:00
|
|
|
/// let ip: IpAddr = IpAddr::V4(127, 0, 0, 1);
|
|
|
|
///
|
2023-05-26 13:04:21 +00:00
|
|
|
/// let ip_add: [u8; 4] = ip.to_arr().unwrap();
|
|
|
|
/// assert_eq!(ip_add, [127, 0, 0, 1]);
|
2023-04-20 20:42:13 +00:00
|
|
|
/// ```
|
|
|
|
pub fn to_arr(&self) -> Result<[u8; 4], NetworkingErr> {
|
|
|
|
match self {
|
|
|
|
IpAddr::V4(o1, o2, o3, o4) => {
|
|
|
|
Ok([*o1, *o2, *o3, *o4])
|
|
|
|
}
|
|
|
|
_ => Err(NetworkingErr::InvalidIPErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Function that generates a IpAddr / Err from an array of
|
|
|
|
/// a octests
|
|
|
|
///
|
|
|
|
/// # Limitation
|
|
|
|
/// Currently only works for IPs of type IPv4
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
///
|
2023-04-20 20:42:13 +00:00
|
|
|
/// let ip_add: [u8; 4] = [127, 0, 0, 1];
|
|
|
|
///
|
2023-05-26 13:04:21 +00:00
|
|
|
/// let ip: IpAddr = IpAddr::from_arr(&ip_add).unwrap();
|
|
|
|
/// assert_eq!(ip, IpAddr::V4(127, 0, 0, 1));
|
2023-04-20 20:42:13 +00:00
|
|
|
/// ```
|
|
|
|
pub fn from_arr(arr: &[u8; 4]) -> Result<IpAddr, NetworkingErr> {
|
|
|
|
Ok(IpAddr::V4(arr[0], arr[1], arr[2], arr[3]))
|
|
|
|
}
|
2023-05-15 18:35:07 +00:00
|
|
|
}
|
2023-05-15 18:22:02 +00:00
|
|
|
|
2023-05-15 18:51:26 +00:00
|
|
|
impl From<IpAddr> for u32 {
|
|
|
|
/// Function that converts an Ip address
|
|
|
|
/// into an Unsigned 32 bit integer.
|
2023-05-15 18:22:02 +00:00
|
|
|
///
|
|
|
|
/// # Limitation
|
2023-05-15 18:51:26 +00:00
|
|
|
/// Currently only works for IPs of type IPv4
|
2023-05-15 18:22:02 +00:00
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
///
|
2023-05-15 18:51:26 +00:00
|
|
|
/// let ip: IpAddr = IpAddr::V4(127, 0, 0, 1);
|
2023-05-15 18:22:02 +00:00
|
|
|
///
|
2023-05-15 18:35:07 +00:00
|
|
|
/// let ip_u32: u32 = u32::from(ip);
|
2023-05-26 13:04:21 +00:00
|
|
|
/// assert_eq!(ip_u32, 2130706433);
|
2023-05-15 18:22:02 +00:00
|
|
|
/// ```
|
2023-05-15 18:35:07 +00:00
|
|
|
fn from(ip: IpAddr) -> Self {
|
|
|
|
u32::from_be_bytes(ip.to_arr().unwrap())
|
2023-05-15 18:22:02 +00:00
|
|
|
}
|
2023-04-20 20:42:13 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 18:51:26 +00:00
|
|
|
impl From<u32> for IpAddr {
|
|
|
|
/// Function that converts an Unsigned 32-bit Ip address
|
|
|
|
/// into an IpAddr
|
|
|
|
///
|
|
|
|
/// # Limitation
|
|
|
|
/// Currently only works for IPs of type IPv4
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
///
|
2023-05-15 18:51:26 +00:00
|
|
|
/// let ip: u32 = 2_130_706_433;
|
|
|
|
///
|
2023-05-26 13:04:21 +00:00
|
|
|
/// let ip_addr: IpAddr = IpAddr::from(ip);
|
|
|
|
/// assert_eq!(ip_addr, IpAddr::V4(127, 0, 0, 1));
|
2023-05-15 18:51:26 +00:00
|
|
|
/// ```
|
|
|
|
fn from(ip: u32) -> Self {
|
|
|
|
IpAddr::from_arr(&(ip.to_be_bytes() as [u8; 4])).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 16:15:28 +00:00
|
|
|
impl ToString for IpAddr {
|
|
|
|
/// Function that returns an IP address in string form
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
IpAddr::V4(oct1, oct2, oct3, oct4) => format!("{}.{}.{}.{}", oct1, oct2, oct3, oct4),
|
|
|
|
IpAddr::V6(addr) => format!("{}", addr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for IpAddr {
|
2023-04-17 16:24:35 +00:00
|
|
|
type Err = NetworkingErr;
|
2023-02-11 16:15:28 +00:00
|
|
|
/// Function that generates a IpAddr / Err from a string
|
2023-02-11 16:35:30 +00:00
|
|
|
///
|
|
|
|
/// # Limitation
|
|
|
|
/// Currently only works for IPs of type IPv4
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::IpAddr;
|
|
|
|
/// use std::str::FromStr;
|
|
|
|
///
|
2023-02-11 16:35:30 +00:00
|
|
|
/// let ip: &str = "127.0.0.1";
|
|
|
|
///
|
2023-05-26 13:04:21 +00:00
|
|
|
/// let parsed_ip: IpAddr = IpAddr::from_str(ip).unwrap();
|
|
|
|
/// assert_eq!(parsed_ip, IpAddr::V4(127,0,0,1));
|
2023-02-11 16:35:30 +00:00
|
|
|
/// ```
|
2023-02-11 16:15:28 +00:00
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
let split_ip = s.split('.');
|
|
|
|
if split_ip.clone().count() != 4 {
|
|
|
|
//Invalid IP address entered
|
2023-04-17 16:24:35 +00:00
|
|
|
return Err(super::NetworkingErr::InvalidIPErr)
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|
|
|
|
let mut ip: [u8; 4] = Default::default();
|
|
|
|
//Go through each octet and ensure it can be parsed;
|
|
|
|
for (i, oct) in split_ip.into_iter().enumerate() {
|
2023-02-11 16:49:23 +00:00
|
|
|
if i > ip.len() {
|
|
|
|
//Ip string is out of the range of the 4 octets in an IPv4 Address
|
2023-04-17 16:24:35 +00:00
|
|
|
return Err(NetworkingErr::InvalidIPErr)
|
2023-02-11 16:49:23 +00:00
|
|
|
}
|
2023-02-11 16:15:28 +00:00
|
|
|
match oct.parse::<u8>() {
|
|
|
|
Ok(parsed_oct) => ip[i] = parsed_oct,
|
2023-04-17 16:24:35 +00:00
|
|
|
Err(_) => return Err(NetworkingErr::InvalidIPErr)
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 20:19:35 +00:00
|
|
|
Ok(IpAddr::from_arr(&ip)?)
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Function that takes in an IP address and then prints a formatted string to the CLI
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
2023-05-26 13:04:21 +00:00
|
|
|
/// use subnet_calculator::ip::{IpAddr, print_ip};
|
|
|
|
///
|
2023-02-11 16:15:28 +00:00
|
|
|
/// let ip = IpAddr::V4(127, 0, 0, 1);
|
|
|
|
///
|
|
|
|
/// print_ip(ip);
|
|
|
|
/// //Output: IP Address: 127.0.0.1
|
|
|
|
/// ```
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn print_ip(ip_address: IpAddr) {
|
|
|
|
println!("IP Address: {}", ip_address.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
/// Tests if an IP is converted to a string
|
|
|
|
/// correctly using the ToString trait
|
|
|
|
fn ip_to_string() {
|
|
|
|
use super::*;
|
|
|
|
let ip = IpAddr::V4(192, 168, 0, 1);
|
|
|
|
assert_eq!(ip.to_string(), "192.168.0.1")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Tests if an IP is converted from a string
|
|
|
|
/// correctly using the FromString trait
|
|
|
|
fn string_to_ip() {
|
|
|
|
use super::*;
|
|
|
|
let ip = "127.0.0.1";
|
|
|
|
assert_eq!(IpAddr::from_str(ip).unwrap(), IpAddr::V4(127, 0, 0, 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
2023-02-11 16:35:30 +00:00
|
|
|
///Tests if an invalid Ip will cause a panic when
|
|
|
|
/// converting from a string to an IpAddr
|
2023-02-11 16:15:28 +00:00
|
|
|
fn invalid_string_to_ip() {
|
|
|
|
use super::*;
|
2023-02-11 16:49:23 +00:00
|
|
|
let ip = "127.0.0.0.1";
|
2023-02-11 16:15:28 +00:00
|
|
|
IpAddr::from_str(ip).unwrap();
|
2023-04-17 16:24:35 +00:00
|
|
|
assert_eq!(IpAddr::from_str(ip), Err(NetworkingErr::InvalidIPErr))
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|
2023-04-20 20:42:13 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Tests the conversion of an IPv4 Address to
|
|
|
|
/// an array
|
|
|
|
fn ipaddr_to_arr() {
|
|
|
|
use super::*;
|
|
|
|
let mut ip_addr = IpAddr::V4(127, 0, 0, 1);
|
|
|
|
assert_eq!(ip_addr.to_arr().unwrap(), [127, 0, 0, 1]);
|
|
|
|
ip_addr = IpAddr::V6("::".to_string());
|
|
|
|
assert_eq!(ip_addr.to_arr().unwrap_err(), NetworkingErr::InvalidIPErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Tests the conversion of an array to
|
|
|
|
// an IPv4 Address
|
|
|
|
fn arr_to_ipaddr() {
|
|
|
|
use super::*;
|
2023-04-20 20:57:50 +00:00
|
|
|
let ip_addr: [u8; 4] = [127, 0, 0, 1];
|
2023-04-20 20:42:13 +00:00
|
|
|
assert_eq!(IpAddr::from_arr(&ip_addr).unwrap(), IpAddr::V4(127, 0, 0, 1));
|
|
|
|
}
|
2023-05-15 18:22:02 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Tests the conversion of an IPv4 Address to
|
|
|
|
/// an unsigned 32-bit integer
|
|
|
|
fn ipaddr_to_u32() {
|
|
|
|
use super::*;
|
|
|
|
let ip = IpAddr::V4(0, 0, 0, 0);
|
2023-05-15 18:35:07 +00:00
|
|
|
assert_eq!(0, u32::from(ip));
|
2023-05-15 18:22:02 +00:00
|
|
|
|
|
|
|
let ip = IpAddr::V4(127, 0, 0, 1);
|
2023-05-15 18:35:07 +00:00
|
|
|
assert_eq!(2_130_706_433, u32::from(ip));
|
2023-05-15 18:22:02 +00:00
|
|
|
|
|
|
|
let ip = IpAddr::V4(10, 10, 10, 0);
|
2023-05-15 18:35:07 +00:00
|
|
|
assert_eq!(168_430_080, u32::from(ip));
|
2023-05-15 18:22:02 +00:00
|
|
|
|
|
|
|
let ip = IpAddr::V4(255, 255, 255, 255);
|
2023-05-15 18:35:07 +00:00
|
|
|
assert_eq!(u32::MAX, u32::from(ip));
|
2023-05-15 18:22:02 +00:00
|
|
|
}
|
2023-05-15 18:51:26 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
/// Tests the conversion of an u32 IPv4 Address to
|
|
|
|
/// an IpAddr
|
|
|
|
fn u32_to_ipaddr()
|
|
|
|
{
|
|
|
|
use super::*;
|
|
|
|
let ip = u32::from(IpAddr::V4(0, 0, 0, 0));
|
|
|
|
assert_eq!(IpAddr::V4(0, 0, 0, 0), IpAddr::from(ip));
|
|
|
|
|
|
|
|
let ip = u32::from(IpAddr::V4(127, 0, 0, 1));
|
|
|
|
assert_eq!(IpAddr::V4(127, 0, 0, 1), IpAddr::from(ip));
|
|
|
|
|
|
|
|
let ip = u32::from(IpAddr::V4(10, 10, 10, 0));
|
|
|
|
assert_eq!(IpAddr::V4(10, 10, 10, 0), IpAddr::from(ip));
|
|
|
|
|
|
|
|
let ip = u32::from(IpAddr::V4(255, 255, 255, 255));
|
|
|
|
assert_eq!(IpAddr::V4(255, 255, 255, 255), IpAddr::from(ip));
|
|
|
|
}
|
2023-02-11 16:15:28 +00:00
|
|
|
}
|