From e8173e6184d39704c1742b413f11dcc950a51274 Mon Sep 17 00:00:00 2001 From: Jakub Date: Tue, 22 Jul 2025 17:30:19 +0200 Subject: [PATCH 1/5] Change batch_size and ulimit type from u16 and u64 to usize --- src/input.rs | 8 ++++---- src/main.rs | 15 ++++++++------- src/scanner/mod.rs | 4 ++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/input.rs b/src/input.rs index 26605317..deb26ff6 100644 --- a/src/input.rs +++ b/src/input.rs @@ -113,7 +113,7 @@ pub struct Opts { /// it will do every port at the same time. Although, your OS may not /// support this. #[arg(short, long, default_value = "4500")] - pub batch_size: u16, + pub batch_size: usize, /// The timeout in milliseconds before a port is assumed to be closed. #[arg(short, long, default_value = "1500")] @@ -126,7 +126,7 @@ pub struct Opts { /// Automatically ups the ULIMIT with the value you provided. #[arg(short, long)] - pub ulimit: Option, + pub ulimit: Option, /// The order of scanning to be performed. The "serial" option will /// scan ports in ascending order while the "random" option will scan @@ -262,10 +262,10 @@ pub struct Config { range: Option, greppable: Option, accessible: Option, - batch_size: Option, + batch_size: Option, timeout: Option, tries: Option, - ulimit: Option, + ulimit: Option, resolver: Option, scan_order: Option, command: Option>, diff --git a/src/main.rs b/src/main.rs index f802987d..a5e55e1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,9 +23,9 @@ extern crate dirs; // Average value for Ubuntu #[cfg(unix)] -const DEFAULT_FILE_DESCRIPTORS_LIMIT: u64 = 8000; +const DEFAULT_FILE_DESCRIPTORS_LIMIT: usize = 8000; // Safest batch size based on experimentation -const AVERAGE_BATCH_SIZE: u16 = 3000; +const AVERAGE_BATCH_SIZE: usize = 3000; #[macro_use] extern crate log; @@ -78,7 +78,7 @@ fn main() { } #[cfg(unix)] - let batch_size: u16 = infer_batch_size(&opts, adjust_ulimit_size(&opts)); + let batch_size: usize = infer_batch_size(&opts, adjust_ulimit_size(&opts)); #[cfg(not(unix))] let batch_size: u16 = AVERAGE_BATCH_SIZE; @@ -222,10 +222,11 @@ The Modern Day Port Scanner."#; } #[cfg(unix)] -fn adjust_ulimit_size(opts: &Opts) -> u64 { +fn adjust_ulimit_size(opts: &Opts) -> usize { use rlimit::Resource; if let Some(limit) = opts.ulimit { + let limit = limit as u64; if Resource::NOFILE.set(limit, limit).is_ok() { detail!( format!("Automatically increasing ulimit value to {limit}."), @@ -242,14 +243,14 @@ fn adjust_ulimit_size(opts: &Opts) -> u64 { } let (soft, _) = Resource::NOFILE.get().unwrap(); - soft + soft as usize } #[cfg(unix)] -fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 { +fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize { use std::convert::TryInto; - let mut batch_size: u64 = opts.batch_size.into(); + let mut batch_size: usize = opts.batch_size; // Adjust the batch size when the ulimit value is lower than the desired batch size if ulimit < batch_size { diff --git a/src/scanner/mod.rs b/src/scanner/mod.rs index 92038eac..76078347 100644 --- a/src/scanner/mod.rs +++ b/src/scanner/mod.rs @@ -29,7 +29,7 @@ use std::{ #[derive(Debug)] pub struct Scanner { ips: Vec, - batch_size: u16, + batch_size: usize, timeout: Duration, tries: NonZeroU8, greppable: bool, @@ -44,7 +44,7 @@ pub struct Scanner { impl Scanner { pub fn new( ips: &[IpAddr], - batch_size: u16, + batch_size: usize, timeout: Duration, tries: u8, greppable: bool, From 10d9608d9083ade2bb39febde2f35cd74cc2be38 Mon Sep 17 00:00:00 2001 From: Jakub Date: Tue, 22 Jul 2025 17:57:15 +0200 Subject: [PATCH 2/5] add error handling for overflow --- src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index a5e55e1d..118e5ed9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,6 +224,7 @@ The Modern Day Port Scanner."#; #[cfg(unix)] fn adjust_ulimit_size(opts: &Opts) -> usize { use rlimit::Resource; + use std::convert::TryInto; if let Some(limit) = opts.ulimit { let limit = limit as u64; @@ -243,7 +244,7 @@ fn adjust_ulimit_size(opts: &Opts) -> usize { } let (soft, _) = Resource::NOFILE.get().unwrap(); - soft as usize + soft.try_into().unwrap_or_else(|_| usize::MAX) } #[cfg(unix)] From 52aba2131736cebea966b87a8a750010d84255e8 Mon Sep 17 00:00:00 2001 From: Jakub Date: Tue, 22 Jul 2025 18:04:51 +0200 Subject: [PATCH 3/5] remove unnecessary conversions `clippy` --- src/main.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 118e5ed9..54dae059 100644 --- a/src/main.rs +++ b/src/main.rs @@ -244,14 +244,12 @@ fn adjust_ulimit_size(opts: &Opts) -> usize { } let (soft, _) = Resource::NOFILE.get().unwrap(); - soft.try_into().unwrap_or_else(|_| usize::MAX) + soft.try_into().unwrap_or(usize::MAX) } #[cfg(unix)] fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize { - use std::convert::TryInto; - - let mut batch_size: usize = opts.batch_size; + let mut batch_size = opts.batch_size; // Adjust the batch size when the ulimit value is lower than the desired batch size if ulimit < batch_size { @@ -262,7 +260,7 @@ fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize { // When the OS supports high file limits like 8000, but the user // selected a batch size higher than this we should reduce it to // a lower number. - if ulimit < AVERAGE_BATCH_SIZE.into() { + if ulimit < AVERAGE_BATCH_SIZE { // ulimit is smaller than aveage batch size // user must have very small ulimit // decrease batch size to half of ulimit @@ -271,7 +269,7 @@ fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize { batch_size = ulimit / 2; } else if ulimit > DEFAULT_FILE_DESCRIPTORS_LIMIT { info!("Batch size is now average batch size"); - batch_size = AVERAGE_BATCH_SIZE.into(); + batch_size = AVERAGE_BATCH_SIZE; } else { batch_size = ulimit - 100; } @@ -284,8 +282,6 @@ fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize { } batch_size - .try_into() - .expect("Couldn't fit the batch size into a u16.") } #[cfg(test)] From db3be88348c306120bf75e72399177b66e8fae67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ko=C5=82odziej?= Date: Tue, 22 Jul 2025 20:07:03 +0200 Subject: [PATCH 4/5] fix missed --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 54dae059..c6b1fec9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,7 +81,7 @@ fn main() { let batch_size: usize = infer_batch_size(&opts, adjust_ulimit_size(&opts)); #[cfg(not(unix))] - let batch_size: u16 = AVERAGE_BATCH_SIZE; + let batch_size: usize = AVERAGE_BATCH_SIZE; let scanner = Scanner::new( &ips, From e7e3f9bfdff7f8fea11fd5ddc0024c5c29fc7381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ko=C5=82odziej?= Date: Wed, 23 Jul 2025 12:41:58 +0200 Subject: [PATCH 5/5] chore: fix CI (unrelated) --- benches/benchmark_portscan.rs | 2 +- tests/timelimits.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benches/benchmark_portscan.rs b/benches/benchmark_portscan.rs index 42fcd6cc..d7c989b8 100644 --- a/benches/benchmark_portscan.rs +++ b/benches/benchmark_portscan.rs @@ -97,7 +97,7 @@ fn criterion_benchmark(c: &mut Criterion) { let mut address_group = c.benchmark_group("address parsing"); address_group.measurement_time(Duration::from_secs(10)); address_group.bench_function("parse addresses with exclusions", |b| { - b.iter(|| bench_address_parsing()) + b.iter(bench_address_parsing) }); address_group.finish(); } diff --git a/tests/timelimits.rs b/tests/timelimits.rs index 37ce67b1..6b32ba45 100644 --- a/tests/timelimits.rs +++ b/tests/timelimits.rs @@ -44,7 +44,7 @@ fn run_rustscan_with_timeout(args: &[&str], timeout: Duration) { let end = Instant::now(); let duration = end.saturating_duration_since(start).as_secs_f32(); - println!("time: {:1.1}s", duration); + println!("time: {duration:1.1}s"); } mod timelimits {