From 24612d7371ae40685e4a676cd80210acd33822c7 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sat, 13 Aug 2022 16:52:18 +0200 Subject: [PATCH] IPv4 struct refactor --- src/ipv4.rs | 26 ++------------------------ src/main.rs | 4 +++- src/scanner.rs | 41 +++++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index c1652c0..1032e1e 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -7,8 +7,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr}; #[derive(Debug, Copy, Clone)] pub struct IPv4 { pub id: u64, - pub ip: [u8; 4], - pub ignore: bool, + pub ip: [u8; 4] } impl IPv4 { @@ -35,8 +34,7 @@ impl IPv4 { Self { id, - ip, - ignore: false, + ip } } @@ -93,23 +91,3 @@ impl Iterator for IPv4Range { } } } - -pub fn _get_all(ignorelist: Option>) -> Result> { - // Ignore those that we know - let ignorelist = ignorelist.unwrap_or(Vec::new()); - - // Get all of the "ids" - let mut ips: Vec = Vec::new(); - - for id in 0..u32::max_value() { - // Make IP - let mut ip = IPv4::new(id as u64); - - // Make the IP "ignored" if it is in the ignorelist - ip.ignore = ignorelist.contains(&ip.id); - - ips.push(ip); - } - - Ok(ips) -} diff --git a/src/main.rs b/src/main.rs index b08ff68..b8b04a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,5 +12,7 @@ fn main() { let results = start_scan(args.from, args.to, args.port, args.threads as u32, None); - for result in results { println!("{:?}", result); } + for result in results { + println!("{:?}", result); + } } diff --git a/src/scanner.rs b/src/scanner.rs index 4923476..be4237b 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,6 +1,6 @@ use crate::ipv4::{IPv4, IPv4Range}; -use log::info; use core::time::Duration; +use log::info; use std::net::TcpStream; use std::thread::JoinHandle; use std::{panic, thread}; @@ -93,32 +93,49 @@ fn get_scan_workers( threads } +#[derive(Debug)] +pub struct ScanResult { + pub id: u32, + pub ipv4: IPv4, + pub result: bool, +} + +impl ScanResult { + fn from_tuple(result_tuple: (u32, bool)) -> Self { + let (id, result) = result_tuple; + let ipv4 = IPv4::new(id as u64); + Self { id, ipv4, result } + } +} + pub fn start_scan( from: u32, to: u32, target_port: u16, num_threads: u32, ignorelist: Option>, -) -> Vec<(u32, bool)> { - +) -> Vec { // Get the workers - println!("Getting workers.."); + let scan_workers = get_scan_workers(from, to, target_port, num_threads, ignorelist); - let mut results: Vec<(u32, bool)> = Vec::new(); - - // Run all the workers - println!("Running workers:"); + let mut results: Vec = Vec::new(); + + // Run all the workers for worker in scan_workers { print!("\t* worker={:?}", worker); - let mut result = match worker.join() { + let result_tuples = match worker.join() { Ok(r) => r, - Err(e) => panic!("{:?}", e) + Err(e) => panic!("{:?}", e), }; - println!(" result={:?}", result); + let mut worker_results = result_tuples + .iter() + .map(|res| ScanResult::from_tuple(*res)) + .collect(); - results.append(&mut result); + //let result = ScanResult::from_tuple(result_tuples); + results.append(&mut worker_results); } println!("End of scan!");