From 7ebdcb2940ef0f4c616205bd7edc6283745f8334 Mon Sep 17 00:00:00 2001 From: Ross Allan Date: Sun, 7 May 2017 11:26:00 +0100 Subject: [PATCH] Add option to limit run time --- src/main.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7e865a4..1f47d8e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,9 @@ fn main() { let mut length = 0usize; let mut mbper_second = 1f32; let mut port_range = 50000; + let mut run_seconds = -1; - println!("usage: ./puma6_fail ", target, length, mbper_second, port_range); + println!("usage: ./puma6_fail ", target, length, mbper_second, port_range, run_seconds); if args.len() <= 1 { println!("A target IP must be given"); @@ -25,23 +26,25 @@ fn main() { } target = args[1].as_str(); - - let source = UdpSocket::bind("0.0.0.0:10000").expect("couldn't bind to address");; if args.len() > 2 { length = args[2].as_str().parse::().expect("invalid packet size") } - let data = vec![0; length as usize]; - if args.len() > 3 { mbper_second = args[3].as_str().parse::().expect("invalid speed") } - let per_second = (1024f32 * 1024f32 * mbper_second) / 8f32; if args.len() > 4 { port_range = args[4].as_str().parse::().expect("invalid port count") } + if args.len() > 5 { + run_seconds = args[5].as_str().parse::().expect("invalid run seconds"); + } + + let source = UdpSocket::bind("0.0.0.0:10000").expect("couldn't bind to address");; + let data = vec![0; length as usize]; + let per_second = (1024f32 * 1024f32 * mbper_second) / 8f32; let udp_length = length + 28; let ethernet_length = udp_length + 26; let packets_per_millisecond = (per_second / (udp_length as f32))/1000f32; @@ -64,5 +67,9 @@ fn main() { thread::sleep(Duration::from_millis(2)); } } + if run_seconds > 0 && start.to(time::PreciseTime::now()).num_seconds() as i32 > run_seconds { + println!("run seconds exceeded, stopping"); + return; + } } }