IPv4 struct refactor

main
E. Almqvist 2 years ago
parent 1cf0ea59f3
commit 24612d7371
  1. 26
      src/ipv4.rs
  2. 4
      src/main.rs
  3. 41
      src/scanner.rs

@ -7,8 +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]
pub ignore: bool,
} }
impl IPv4 { impl IPv4 {
@ -35,8 +34,7 @@ impl IPv4 {
Self { Self {
id, id,
ip, ip
ignore: false,
} }
} }
@ -93,23 +91,3 @@ impl Iterator for IPv4Range {
} }
} }
} }
pub fn _get_all(ignorelist: Option<Vec<u64>>) -> Result<Vec<IPv4>> {
// Ignore those that we know
let ignorelist = ignorelist.unwrap_or(Vec::new());
// Get all of the "ids"
let mut ips: Vec<IPv4> = Vec::new();
for id in 0..u32::max_value() {
// Make IP
let mut ip = IPv4::new(id as u64);
// Make the IP "ignored" if it is in the ignorelist
ip.ignore = ignorelist.contains(&ip.id);
ips.push(ip);
}
Ok(ips)
}

@ -12,5 +12,7 @@ fn main() {
let results = start_scan(args.from, args.to, args.port, args.threads as u32, None); let results = start_scan(args.from, args.to, args.port, args.threads as u32, None);
for result in results { println!("{:?}", result); } for result in results {
println!("{:?}", result);
}
} }

@ -1,6 +1,6 @@
use crate::ipv4::{IPv4, IPv4Range}; use crate::ipv4::{IPv4, IPv4Range};
use log::info;
use core::time::Duration; use core::time::Duration;
use log::info;
use std::net::TcpStream; use std::net::TcpStream;
use std::thread::JoinHandle; use std::thread::JoinHandle;
use std::{panic, thread}; use std::{panic, thread};
@ -93,32 +93,49 @@ fn get_scan_workers(
threads threads
} }
#[derive(Debug)]
pub struct ScanResult {
pub id: u32,
pub ipv4: IPv4,
pub result: bool,
}
impl ScanResult {
fn from_tuple(result_tuple: (u32, bool)) -> Self {
let (id, result) = result_tuple;
let ipv4 = IPv4::new(id as u64);
Self { id, ipv4, result }
}
}
pub fn start_scan( pub fn start_scan(
from: u32, from: u32,
to: u32, to: u32,
target_port: u16, target_port: u16,
num_threads: u32, num_threads: u32,
ignorelist: Option<Vec<u32>>, ignorelist: Option<Vec<u32>>,
) -> Vec<(u32, bool)> { ) -> Vec<ScanResult> {
// Get the workers // Get the workers
println!("Getting workers..");
let scan_workers = get_scan_workers(from, to, target_port, num_threads, ignorelist); let scan_workers = get_scan_workers(from, to, target_port, num_threads, ignorelist);
let mut results: Vec<(u32, bool)> = Vec::new(); let mut results: Vec<ScanResult> = Vec::new();
// Run all the workers // Run all the workers
println!("Running workers:");
for worker in scan_workers { for worker in scan_workers {
print!("\t* worker={:?}", worker); print!("\t* worker={:?}", worker);
let mut result = match worker.join() { let result_tuples = match worker.join() {
Ok(r) => r, Ok(r) => r,
Err(e) => panic!("{:?}", e) Err(e) => panic!("{:?}", e),
}; };
println!(" result={:?}", result); let mut worker_results = result_tuples
.iter()
.map(|res| ScanResult::from_tuple(*res))
.collect();
results.append(&mut result); //let result = ScanResult::from_tuple(result_tuples);
results.append(&mut worker_results);
} }
println!("End of scan!"); println!("End of scan!");

Loading…
Cancel
Save