Skip to content

Commit 00d1003

Browse files
authored
Change batch_size and ulimit type (#811)
* Change batch_size and ulimit type from u16 and u64 to usize * add error handling for overflow * remove unnecessary conversions `clippy` * fix missed * chore: fix CI (unrelated)
1 parent 4c056a9 commit 00d1003

File tree

5 files changed

+20
-22
lines changed

5 files changed

+20
-22
lines changed

benches/benchmark_portscan.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ fn criterion_benchmark(c: &mut Criterion) {
9797
let mut address_group = c.benchmark_group("address parsing");
9898
address_group.measurement_time(Duration::from_secs(10));
9999
address_group.bench_function("parse addresses with exclusions", |b| {
100-
b.iter(|| bench_address_parsing())
100+
b.iter(bench_address_parsing)
101101
});
102102
address_group.finish();
103103
}

src/input.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub struct Opts {
113113
/// it will do every port at the same time. Although, your OS may not
114114
/// support this.
115115
#[arg(short, long, default_value = "4500")]
116-
pub batch_size: u16,
116+
pub batch_size: usize,
117117

118118
/// The timeout in milliseconds before a port is assumed to be closed.
119119
#[arg(short, long, default_value = "1500")]
@@ -126,7 +126,7 @@ pub struct Opts {
126126

127127
/// Automatically ups the ULIMIT with the value you provided.
128128
#[arg(short, long)]
129-
pub ulimit: Option<u64>,
129+
pub ulimit: Option<usize>,
130130

131131
/// The order of scanning to be performed. The "serial" option will
132132
/// scan ports in ascending order while the "random" option will scan
@@ -262,10 +262,10 @@ pub struct Config {
262262
range: Option<PortRange>,
263263
greppable: Option<bool>,
264264
accessible: Option<bool>,
265-
batch_size: Option<u16>,
265+
batch_size: Option<usize>,
266266
timeout: Option<u32>,
267267
tries: Option<u8>,
268-
ulimit: Option<u64>,
268+
ulimit: Option<usize>,
269269
resolver: Option<String>,
270270
scan_order: Option<ScanOrder>,
271271
command: Option<Vec<String>>,

src/main.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ extern crate dirs;
2323

2424
// Average value for Ubuntu
2525
#[cfg(unix)]
26-
const DEFAULT_FILE_DESCRIPTORS_LIMIT: u64 = 8000;
26+
const DEFAULT_FILE_DESCRIPTORS_LIMIT: usize = 8000;
2727
// Safest batch size based on experimentation
28-
const AVERAGE_BATCH_SIZE: u16 = 3000;
28+
const AVERAGE_BATCH_SIZE: usize = 3000;
2929

3030
#[macro_use]
3131
extern crate log;
@@ -78,10 +78,10 @@ fn main() {
7878
}
7979

8080
#[cfg(unix)]
81-
let batch_size: u16 = infer_batch_size(&opts, adjust_ulimit_size(&opts));
81+
let batch_size: usize = infer_batch_size(&opts, adjust_ulimit_size(&opts));
8282

8383
#[cfg(not(unix))]
84-
let batch_size: u16 = AVERAGE_BATCH_SIZE;
84+
let batch_size: usize = AVERAGE_BATCH_SIZE;
8585

8686
let scanner = Scanner::new(
8787
&ips,
@@ -233,10 +233,12 @@ The Modern Day Port Scanner."#;
233233
}
234234

235235
#[cfg(unix)]
236-
fn adjust_ulimit_size(opts: &Opts) -> u64 {
236+
fn adjust_ulimit_size(opts: &Opts) -> usize {
237237
use rlimit::Resource;
238+
use std::convert::TryInto;
238239

239240
if let Some(limit) = opts.ulimit {
241+
let limit = limit as u64;
240242
if Resource::NOFILE.set(limit, limit).is_ok() {
241243
detail!(
242244
format!("Automatically increasing ulimit value to {limit}."),
@@ -253,14 +255,12 @@ fn adjust_ulimit_size(opts: &Opts) -> u64 {
253255
}
254256

255257
let (soft, _) = Resource::NOFILE.get().unwrap();
256-
soft
258+
soft.try_into().unwrap_or(usize::MAX)
257259
}
258260

259261
#[cfg(unix)]
260-
fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 {
261-
use std::convert::TryInto;
262-
263-
let mut batch_size: u64 = opts.batch_size.into();
262+
fn infer_batch_size(opts: &Opts, ulimit: usize) -> usize {
263+
let mut batch_size = opts.batch_size;
264264

265265
// Adjust the batch size when the ulimit value is lower than the desired batch size
266266
if ulimit < batch_size {
@@ -271,7 +271,7 @@ fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 {
271271
// When the OS supports high file limits like 8000, but the user
272272
// selected a batch size higher than this we should reduce it to
273273
// a lower number.
274-
if ulimit < AVERAGE_BATCH_SIZE.into() {
274+
if ulimit < AVERAGE_BATCH_SIZE {
275275
// ulimit is smaller than aveage batch size
276276
// user must have very small ulimit
277277
// decrease batch size to half of ulimit
@@ -280,7 +280,7 @@ fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 {
280280
batch_size = ulimit / 2;
281281
} else if ulimit > DEFAULT_FILE_DESCRIPTORS_LIMIT {
282282
info!("Batch size is now average batch size");
283-
batch_size = AVERAGE_BATCH_SIZE.into();
283+
batch_size = AVERAGE_BATCH_SIZE;
284284
} else {
285285
batch_size = ulimit - 100;
286286
}
@@ -293,8 +293,6 @@ fn infer_batch_size(opts: &Opts, ulimit: u64) -> u16 {
293293
}
294294

295295
batch_size
296-
.try_into()
297-
.expect("Couldn't fit the batch size into a u16.")
298296
}
299297

300298
#[cfg(test)]

src/scanner/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use std::{
2929
#[derive(Debug)]
3030
pub struct Scanner {
3131
ips: Vec<IpAddr>,
32-
batch_size: u16,
32+
batch_size: usize,
3333
timeout: Duration,
3434
tries: NonZeroU8,
3535
greppable: bool,
@@ -44,7 +44,7 @@ pub struct Scanner {
4444
impl Scanner {
4545
pub fn new(
4646
ips: &[IpAddr],
47-
batch_size: u16,
47+
batch_size: usize,
4848
timeout: Duration,
4949
tries: u8,
5050
greppable: bool,

tests/timelimits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn run_rustscan_with_timeout(args: &[&str], timeout: Duration) {
4444
let end = Instant::now();
4545
let duration = end.saturating_duration_since(start).as_secs_f32();
4646

47-
println!("time: {:1.1}s", duration);
47+
println!("time: {duration:1.1}s");
4848
}
4949

5050
mod timelimits {

0 commit comments

Comments
 (0)