From 066b74cb377fa8716074c1c2700a5bf4672f7cba Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Mon, 15 Aug 2022 14:48:37 +0200 Subject: [PATCH] Formatting & refactor & stats --- src/ipv4.rs | 11 +++++------ src/scanner.rs | 22 +++++++++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/ipv4.rs b/src/ipv4.rs index 3372623..a13cf9d 100644 --- a/src/ipv4.rs +++ b/src/ipv4.rs @@ -43,8 +43,7 @@ impl IPv4 { Self { id, ip } } - pub fn to_ipaddr(self: &mut Self) -> Result { - // TODO: remove unneeded Result returns + fn to_ipaddr(self: &mut Self) -> Result { if let [a, b, c, d] = self.ip[0..4] { Ok(IpAddr::V4(Ipv4Addr::new(a, b, c, d))) } else { @@ -52,9 +51,9 @@ impl IPv4 { } } - pub fn to_socketaddr(self: &mut Self, port: u16) -> Result { - let ip_addr = self.to_ipaddr()?; - Ok(SocketAddr::new(ip_addr, port)) + pub fn to_socketaddr(self: &mut Self, port: u16) -> SocketAddr { + let ip_addr = self.to_ipaddr().unwrap_or_else(|err| panic!("{}", err)); + SocketAddr::new(ip_addr, port) } } @@ -82,7 +81,7 @@ impl IPv4Range { pub fn from_cidr(cidr_string: String, id_ignore: Option>) -> Self { let cidr = Ipv4Cidr::from_str(cidr_string).unwrap(); - let (from, to) = (cidr.first(), cidr.last()); + let (from, to) = (cidr.first(), cidr.last()); Self::new(from, to, id_ignore) } diff --git a/src/scanner.rs b/src/scanner.rs index 55a4ab8..3e67446 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -5,14 +5,18 @@ use std::net::TcpStream; use std::thread::JoinHandle; use std::{panic, thread}; +static mut TCP_SCANS_ISSUED: u64 = 0; + fn tcp_scan(mut target: IPv4, target_port: u16) -> bool { debug!("Starting scan on {:?}", target); - let dest = target - .to_socketaddr(target_port) - .unwrap_or_else(|e| panic!("{}", e)); + let dest = target.to_socketaddr(target_port); let timeout = Duration::new(1, 0); + unsafe { + TCP_SCANS_ISSUED += 1; + } + if let Ok(_res) = TcpStream::connect_timeout(&dest, timeout) { println!("{:?}", dest); true @@ -31,7 +35,9 @@ fn create_scan_thread(ip_range: IPv4Range, target_port: u16) -> JoinHandle Vec