Minor fixes

main
E. Almqvist 2 years ago
parent 3856c3ce3b
commit 8e513023bc
  1. 11
      src/ipv4.rs
  2. 50
      src/scanner.rs

@ -7,7 +7,7 @@ use std::net::{IpAddr, Ipv4Addr, SocketAddr};
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct IPv4 { pub struct IPv4 {
pub id: u64, pub id: u64,
pub ip: [u8; 4] pub ip: [u8; 4],
} }
impl IPv4 { impl IPv4 {
@ -25,17 +25,14 @@ impl IPv4 {
} }
// Reverse it so that we start from the top // Reverse it so that we start from the top
ip = ip.into_iter().rev().collect(); // ip = ip.into_iter().rev().collect();
// convert to array // convert to array
let ip: [u8; 4] = ip let ip: [u8; 4] = ip
.try_into() .try_into()
.unwrap_or_else(|_: Vec<u8>| panic!("Unable to convert Vec to [u8; 4] for IPv4!")); .unwrap_or_else(|_: Vec<u8>| panic!("Unable to convert Vec to [u8; 4] for IPv4!"));
Self { Self { id, ip }
id,
ip
}
} }
pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> { pub fn to_ipaddr(self: &mut Self) -> Result<IpAddr> {
@ -63,7 +60,7 @@ impl IPv4Range {
let to = to.clamp(0, u32::max_value()); let to = to.clamp(0, u32::max_value());
if from >= to { if from >= to {
panic!("Range size must be >= 1! from: {} >= to: {}", from, to); panic!("Range size must be >= 1! from={} >= to={}", from, to);
} }
Self { Self {

@ -1,6 +1,6 @@
use crate::ipv4::{IPv4, IPv4Range}; use crate::ipv4::{IPv4, IPv4Range};
use core::time::Duration; use core::time::Duration;
use log::{info, warn, debug}; use log::{debug, info, warn};
use std::net::TcpStream; use std::net::TcpStream;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::{panic, thread}; use std::{panic, thread};
@ -60,37 +60,51 @@ fn get_scan_workers(
num_threads: u64, num_threads: u64,
ignorelist: Option<Vec<u32>>, ignorelist: Option<Vec<u32>>,
) -> Vec<JoinHandle<Vec<(u32, bool)>>> { ) -> Vec<JoinHandle<Vec<(u32, bool)>>> {
let ips_per_thread: u64 = (((to - from) as f32) / num_threads as f32) as u64;
let ip_amount = to - from;
let ips_per_thread: u64 = ((ip_amount as f32) / num_threads as f32) as u64;
println!("{} : {}", num_threads, ips_per_thread); println!("{} : {}", num_threads, ips_per_thread);
let ips_left: u64 = (to - from) as u64 - (num_threads * ips_per_thread) as u64; // how many ips we have left after the first threads println!(
println!("{} | {}", to - from, 2); "{} - {} = {} | {}",
to,
from,
to - from,
num_threads * ips_per_thread
);
// container for all of our threads // container for all of our threads
let mut threads: Vec<JoinHandle<Vec<(u32, bool)>>> = Vec::new(); let mut threads: Vec<JoinHandle<Vec<(u32, bool)>>> = Vec::new();
// TODO: make last thread do the "ips_left" work // how many ips we have left after the first threads
for thread_id in 0..num_threads { if ((to - from) as u64 % num_threads) != 0 {
let id_ignorelist = ignorelist.clone().unwrap_or_default(); println!(";(");
let ips_left: u64 = ip_amount as u64 - (num_threads * ips_per_thread) as u64;
// Create a worker
let worker = create_scan_worker(thread_id, ips_per_thread, target_port, id_ignorelist);
threads.push(worker);
}
// Clean up the rest // Clean up the rest
if ips_left > 0 {
warn!("Number of IPv4 addresses is not divisible by the amount of threads! Creating extra thread..."); warn!("Number of IPv4 addresses is not divisible by the amount of threads! Creating extra thread...");
let id_ignorelist = ignorelist.clone().unwrap_or_default(); let id_ignorelist = ignorelist.clone().unwrap_or_default();
let worker = create_scan_worker( let worker = create_scan_worker(
threads.len() as u64 + 1, threads.len() as u64 + 1,
ips_per_thread, ips_left,
target_port, target_port,
id_ignorelist, id_ignorelist,
); );
threads.push(worker);
};
// TODO: make last thread do the "ips_left" work
for thread_id in 0..num_threads {
let id_ignorelist = ignorelist.clone().unwrap_or_default();
// Create a worker
let worker = create_scan_worker(thread_id, ips_per_thread, target_port, id_ignorelist);
threads.push(worker); threads.push(worker);
} }
threads threads
} }
@ -126,7 +140,7 @@ pub fn start_scan(
// Run all the workers // Run all the workers
for worker in scan_workers { for worker in scan_workers {
println!("* Running worker: {:?}", worker); println!("* Running worker:");
let result_tuples = match worker.join() { let result_tuples = match worker.join() {
Ok(r) => r, Ok(r) => r,
Err(e) => panic!("{:?}", e), Err(e) => panic!("{:?}", e),
@ -137,7 +151,7 @@ pub fn start_scan(
.map(|res| ScanResult::from_tuple(*res)) .map(|res| ScanResult::from_tuple(*res))
.collect(); .collect();
println!("\t* Worker got results: {:?}", result_tuples); println!("\t* Worker result: {:?}", result_tuples);
results.append(&mut worker_results); results.append(&mut worker_results);
} }

Loading…
Cancel
Save