Minor refactor & IpAddr conversion

cli
E. Almqvist 2 years ago
parent 99865e2efe
commit 5a03b191e2
  1. 14
      src/ipv4.rs
  2. 2
      src/main.rs

@ -1,3 +1,5 @@
use std::net::{IpAddr, Ipv4Addr};
use anyhow::{Result, anyhow}; use anyhow::{Result, anyhow};
use convert_base::Convert; use convert_base::Convert;
use crate::util; use crate::util;
@ -14,7 +16,7 @@ let i = 0 .. u32:max_value()
#[derive(Debug)] #[derive(Debug)]
pub struct IPv4 { pub struct IPv4 {
pub id: u64, pub id: u64,
pub ip: Vec<u16> pub ip: Vec<u8>
} }
impl IPv4 { impl IPv4 {
@ -22,7 +24,7 @@ impl IPv4 {
let mut base = Convert::new(10, 256); let mut base = Convert::new(10, 256);
let id_vec = util::number_to_vec(id); // push all digits into a vec let id_vec = util::number_to_vec(id); // push all digits into a vec
let mut ip = base.convert::<u8, u16>(&id_vec); let mut ip = base.convert::<u8, u8>(&id_vec);
// In case we are missing some digits // In case we are missing some digits
if ip.len() < 4 { if ip.len() < 4 {
@ -36,6 +38,14 @@ impl IPv4 {
Self { id, ip } Self { id, ip }
} }
pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> {
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"))
}
}
} }

@ -14,7 +14,7 @@ fn main() {
// scanner::start_scan(100); // scanner::start_scan(100);
// permutations::ipv4(None); // 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); println!("{:?}", ip);
let ip = IPv4::new(256); let ip = IPv4::new(256);
println!("{:?}", ip); println!("{:?}", ip);

Loading…
Cancel
Save