Refactor: scanner::create_scan_thread

main
E. Almqvist 2 years ago
parent 031e9ed491
commit 87702bbdd0
  1. 6
      src/ipv4.rs
  2. 61
      src/scanner.rs

@ -62,6 +62,12 @@ pub struct IPv4_Range {
impl IPv4_Range { impl IPv4_Range {
pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self { pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self {
to = to.clamp(0, u32::max_value());
if from >= to {
panic!("Range size must be >= 1! from: {} >= to: {}", from, to);
}
Self { Self {
id_start: from, id_start: from,
id_end: to, id_end: to,

@ -1,11 +1,11 @@
use crate::ipv4; use crate::ipv4::{IPv4, IPv4_Range};
use anyhow::Result; use anyhow::Result;
use log::info; use log::info;
use std::net::TcpStream; use std::net::TcpStream;
use std::thread; use std::thread;
use std::thread::JoinHandle; 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(); let dest = target.to_socketaddr(target_port).unwrap();
false false
@ -17,27 +17,21 @@ fn tcp_scan(mut target: ipv4::IPv4, target_port: u16) -> bool {
// false // false
} }
// fn create_scan_thread( fn create_scan_thread(thread_id: u32, ip_range: IPv4_Range, target_port: u16) -> JoinHandle<Vec<bool>> {
// ip_list: Vec<ipv4::IPv4>, thread::spawn(move || {
// thread_id: u32, info!("Starting thread worker #{}", thread_id);
// ips_per_thread: u32, let mut results: Vec<bool> = Vec::new();
// target_port: u16,
// ) -> JoinHandle<Vec<bool>> { // do the scan thing
// thread::spawn(move || { ip_range.into_iter().for_each(|id| {
// info!("Starting thread worker #{}", thread_id); let target = IPv4::new(id as u64);
// let mut results: Vec<bool> = Vec::new(); let result = tcp_scan(target, target_port);
// results.insert(id as usize, result);
// // do the scan thing });
// for i in 0..ips_per_thread {
// let id = (thread_id * ips_per_thread) + i; results
// let ref target = ip_list[id as usize]; })
// let result = tcp_scan(*target, target_port); }
// results.push(result);
// }
//
// results
// })
// }
pub fn start_scan( pub fn start_scan(
from: u32, from: u32,
@ -47,24 +41,27 @@ pub fn start_scan(
ignorelist: Option<Vec<u32>>, ignorelist: Option<Vec<u32>>,
) -> Result<()> { ) -> Result<()> {
println!("Starting wwmap..."); 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_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 ips_left = num_threads * ips_per_thread; // how many ips we have left after the first threads
let ip_ranges: Vec<ipv4::IPv4_Range> = Vec::new(); // container for all of our threads
let mut threads: Vec<JoinHandle<Vec<bool>>> = Vec::new();
for thread_id in 0..num_threads { for thread_id in 0..num_threads {
let (f, t) = ((thread_id * ips_per_thread), ((thread_id + 1) * ips_per_thread)); // Calculate ranges & stuff
let range = ipv4::IPv4_Range::new(f, t, ignorelist); 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<JoinHandle<Vec<bool>>> = Vec::new();
// container for all the results // container for all the results
let mut result_list: Vec<bool> = Vec::new(); let mut result_list: Vec<bool> = Vec::new();

Loading…
Cancel
Save