From 87702bbdd09e5d9c8e3c5d97af5026648b3d4ae0 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Thu, 11 Aug 2022 20:58:59 +0200 Subject: [PATCH] Refactor: scanner::create_scan_thread --- src/ipv4.rs | 6 +++++ src/scanner.rs | 61 ++++++++++++++++++++++++-------------------------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index be011aa..6decc6c 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -62,6 +62,12 @@ pub struct IPv4_Range { impl IPv4_Range { pub fn new(from: u32, to: u32, id_ignore: Option>) -> Self { + to = to.clamp(0, u32::max_value()); + + if from >= to { + panic!("Range size must be >= 1! from: {} >= to: {}", from, to); + } + Self { id_start: from, id_end: to, diff --git a/src/scanner.rs b/src/scanner.rs index c499030..d384868 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -1,11 +1,11 @@ -use crate::ipv4; +use crate::ipv4::{IPv4, IPv4_Range}; use anyhow::Result; use log::info; use std::net::TcpStream; use std::thread; use std::thread::JoinHandle; -fn tcp_scan(mut target: ipv4::IPv4, target_port: u16) -> bool { +fn tcp_scan(mut target: IPv4, target_port: u16) -> bool { let dest = target.to_socketaddr(target_port).unwrap(); false @@ -17,27 +17,21 @@ fn tcp_scan(mut target: ipv4::IPv4, target_port: u16) -> bool { // false } -// fn create_scan_thread( -// ip_list: Vec, -// thread_id: u32, -// ips_per_thread: u32, -// target_port: u16, -// ) -> JoinHandle> { -// thread::spawn(move || { -// info!("Starting thread worker #{}", thread_id); -// let mut results: Vec = Vec::new(); -// -// // do the scan thing -// for i in 0..ips_per_thread { -// let id = (thread_id * ips_per_thread) + i; -// let ref target = ip_list[id as usize]; -// let result = tcp_scan(*target, target_port); -// results.push(result); -// } -// -// results -// }) -// } +fn create_scan_thread(thread_id: u32, ip_range: IPv4_Range, target_port: u16) -> JoinHandle> { + thread::spawn(move || { + info!("Starting thread worker #{}", thread_id); + let mut results: Vec = Vec::new(); + + // do the scan thing + ip_range.into_iter().for_each(|id| { + let target = IPv4::new(id as u64); + let result = tcp_scan(target, target_port); + results.insert(id as usize, result); + }); + + results + }) +} pub fn start_scan( from: u32, @@ -47,24 +41,27 @@ pub fn start_scan( ignorelist: Option>, ) -> Result<()> { println!("Starting wwmap..."); - //let ip_list = ipv4::get_all(ignorelist)?; + //let ip_list = get_all(ignorelist)?; let ips_per_thread = (((to - from) as f32) / num_threads as f32) as u32; let ips_left = num_threads * ips_per_thread; // how many ips we have left after the first threads - let ip_ranges: Vec = Vec::new(); + // container for all of our threads + let mut threads: Vec>> = Vec::new(); for thread_id in 0..num_threads { - let (f, t) = ((thread_id * ips_per_thread), ((thread_id + 1) * ips_per_thread)); - let range = ipv4::IPv4_Range::new(f, t, ignorelist); + // Calculate ranges & stuff + let (f, t) = ( + (thread_id * ips_per_thread), + ((thread_id + 1) * ips_per_thread), + ); + let range = IPv4_Range::new(f, t, ignorelist); - ip_ranges.push(range); + // Create a worker + let worker = create_scan_thread(thread_id, range, target_port); + threads.push(worker); } - // container for all of our threads - let mut threads: Vec>> = Vec::new(); - - // container for all the results let mut result_list: Vec = Vec::new();