diff --git a/src/ipv4.rs b/src/ipv4.rs index cb98a98..5c1a031 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -1,3 +1,5 @@ +use std::net::{IpAddr, Ipv4Addr}; + use anyhow::{Result, anyhow}; use convert_base::Convert; use crate::util; @@ -14,7 +16,7 @@ let i = 0 .. u32:max_value() #[derive(Debug)] pub struct IPv4 { pub id: u64, - pub ip: Vec + pub ip: Vec } impl IPv4 { @@ -22,7 +24,7 @@ impl IPv4 { let mut base = Convert::new(10, 256); let id_vec = util::number_to_vec(id); // push all digits into a vec - let mut ip = base.convert::(&id_vec); + let mut ip = base.convert::(&id_vec); // In case we are missing some digits if ip.len() < 4 { @@ -36,6 +38,14 @@ impl IPv4 { Self { id, ip } } + + pub fn to_ipaddr(self: &mut Self) -> Result { + if let [a, b, c, d] = self.ip[0..3] { + Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d))) + } else { + Err(anyhow!("Unable to unpack IPv4 address")) + } + } } diff --git a/src/main.rs b/src/main.rs index 245c5e0..a490ae4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() { // scanner::start_scan(100); // permutations::ipv4(None); - let ip = IPv4::new((u32::max_value() - 256 - 255) as u64); + let ip = IPv4::new((u32::max_value()) as u64); println!("{:?}", ip); let ip = IPv4::new(256); println!("{:?}", ip);