Refactor & stuff

main
E. Almqvist 2 years ago
parent 6978f7d9c0
commit 031e9ed491
  1. 46
      src/ipv4.rs
  2. 86
      src/scanner.rs

@ -4,23 +4,11 @@ use convert_base::Convert;
use std::convert::TryInto;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
/*
Algorithm: O(n)
let i = 0 .. u32:max_value()
# Convert each i to base 256 and we get all the ipv4 addresses
# This is waaaay better than a stupid loop
*/
#[derive(Debug, Copy, Clone)]
pub struct IPv4 {
pub id: u64,
pub ip: [u8; 4],
pub ignore: bool,
// pub enum __Generator {
// Start(&'static)
// }
}
impl IPv4 {
@ -66,6 +54,40 @@ impl IPv4 {
}
}
pub struct IPv4_Range {
pub id_start: u32,
pub id_end: u32,
pub id_ignore: Vec<u32>,
}
impl IPv4_Range {
pub fn new(from: u32, to: u32, id_ignore: Option<Vec<u32>>) -> Self {
Self {
id_start: from,
id_end: to,
id_ignore: id_ignore.unwrap_or(Vec::new()),
}
}
}
impl Iterator for IPv4_Range {
type Item = u32;
fn next(&mut self) -> Option<u32> {
if self.id_start == self.id_end {
None
} else {
let res = Some(self.id_start);
self.id_start += 1;
if self.id_ignore.contains(&res.unwrap()) {
return self.next();
}
res
}
}
}
pub fn get_all(ignorelist: Option<Vec<u64>>) -> Result<Vec<IPv4>> {
// Ignore those that we know
let ignorelist = ignorelist.unwrap_or(Vec::new());

@ -17,62 +17,56 @@ 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(
// 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
// })
// }
pub fn start_scan(target_port: u16, num_threads: u32, ignorelist: Option<Vec<u64>>) -> Result<()> {
pub fn start_scan(
from: u32,
to: u32,
target_port: u16,
num_threads: u32,
ignorelist: Option<Vec<u32>>,
) -> Result<()> {
println!("Starting wwmap...");
let ip_list = ipv4::get_all(ignorelist)?;
//let ip_list = ipv4::get_all(ignorelist)?;
println!("Post list gen");
let ips_per_thread = ((ip_list.len() 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
// container for all of our threads
let mut threads: Vec<JoinHandle<Vec<bool>>> = Vec::new();
let result_list: Vec<bool> = Vec::new();
let ip_ranges: Vec<ipv4::IPv4_Range> = Vec::new();
// create all of our threads
for thread_id in 0..num_threads {
threads.push(create_scan_thread(
ip_list.clone(),
thread_id,
ips_per_thread,
target_port,
));
}
let (f, t) = ((thread_id * ips_per_thread), ((thread_id + 1) * ips_per_thread));
let range = ipv4::IPv4_Range::new(f, t, ignorelist);
// create the last thread to do the job
if ips_left > 0 {
threads.push(create_scan_thread(
ip_list,
num_threads,
ips_left,
target_port,
));
ip_ranges.push(range);
}
// Append all of the results into the list
// 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();
Ok(())
}

Loading…
Cancel
Save