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 {
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 {
id_start: from,
id_end: to,

@ -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<ipv4::IPv4>,
// thread_id: u32,
// ips_per_thread: u32,
// target_port: u16,
// ) -> JoinHandle<Vec<bool>> {
// thread::spawn(move || {
// info!("Starting thread worker #{}", thread_id);
// let mut results: Vec<bool> = 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<Vec<bool>> {
thread::spawn(move || {
info!("Starting thread worker #{}", thread_id);
let mut results: Vec<bool> = 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<Vec<u32>>,
) -> 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<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 {
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<JoinHandle<Vec<bool>>> = Vec::new();
// container for all the results
let mut result_list: Vec<bool> = Vec::new();

Loading…
Cancel
Save