From c075329920b0d56dd18d075f769515fe7b8b88fc Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 8 Aug 2022 17:48:04 +0200 Subject: [PATCH] IP generation --- src/ipv4.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index 1f5763a..1247e02 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -13,10 +13,11 @@ let i = 0 .. u32:max_value() # This is waaaay better than a stupid loop */ -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub struct IPv4 { pub id: u64, - pub ip: Vec + pub ip: Vec, + pub ignore: bool } impl IPv4 { @@ -36,7 +37,7 @@ impl IPv4 { // Reverse it so that we start from the top ip = ip.into_iter().rev().collect(); - Self { id, ip } + Self { id, ip, ignore: false } } pub fn to_ipaddr(self: &mut Self) -> Result { @@ -53,14 +54,22 @@ impl IPv4 { } } +pub fn get_all(ignorelist: Option>) -> Result> { + // Ignore those that we know + let ignorelist = ignorelist.unwrap_or(Vec::new()); -pub fn get_all(blacklist: Option>) -> Result> { - let blacklist = blacklist.unwrap_or(Vec::new()); - let ips: Vec = (0..u32::max_value()).collect(); // 32 bit max value is the last IP + // Get all of the "ids" + let ids: Vec = (0..u32::max_value()).collect(); - //if combos.len() <= 0 { - Err(anyhow!("Unable to generate IPv4 permutations")) -// } else { -// Ok(combos) -// } + let ips: Vec = ids.iter().map(|&ip| { + // Make IP + let mut ip = IPv4::new(ip as u64); + + // Make the IP "ignored" if it is in the ignorelist + if ignorelist.len() > 0 && ignorelist.contains(&ip.id) { ip.ignore = true; } + + ip + }).collect(); + + Ok(ips) }