|
@@ -1,14 +1,19 @@
|
|
|
+use std::cell::RefCell;
|
|
|
use std::str::{self, FromStr};
|
|
|
-use std::env;
|
|
|
+use std::rc::Rc;
|
|
|
+use std::io;
|
|
|
+use std::fs::File;
|
|
|
use std::time::{Instant, Duration, SystemTime, UNIX_EPOCH};
|
|
|
+use std::env;
|
|
|
use std::process;
|
|
|
use log::{LogLevel, LogLevelFilter, LogRecord};
|
|
|
use env_logger::LogBuilder;
|
|
|
-use getopts;
|
|
|
+use getopts::{Options, Matches};
|
|
|
|
|
|
-use smoltcp::phy::{EthernetTracer, FaultInjector, TapInterface};
|
|
|
+use smoltcp::phy::{Device, EthernetTracer, FaultInjector, TapInterface};
|
|
|
+use smoltcp::phy::{PcapWriter, PcapSink, PcapMode, PcapLinkType};
|
|
|
|
|
|
-pub fn setup_logging_with_clock<F>(since_startup: F)
|
|
|
+pub fn setup_logging_with_clock<F>(filter: &str, since_startup: F)
|
|
|
where F: Fn() -> u64 + Send + Sync + 'static {
|
|
|
LogBuilder::new()
|
|
|
.format(move |record: &LogRecord| {
|
|
@@ -28,22 +33,67 @@ pub fn setup_logging_with_clock<F>(since_startup: F)
|
|
|
}
|
|
|
})
|
|
|
.filter(None, LogLevelFilter::Trace)
|
|
|
+ .parse(filter)
|
|
|
+ .parse(&env::var("RUST_LOG").unwrap_or("".to_owned()))
|
|
|
.init()
|
|
|
.unwrap();
|
|
|
}
|
|
|
|
|
|
-pub fn setup_logging() {
|
|
|
+pub fn setup_logging(filter: &str) {
|
|
|
let startup_at = Instant::now();
|
|
|
- setup_logging_with_clock(move || {
|
|
|
+ setup_logging_with_clock(filter, move || {
|
|
|
let elapsed = Instant::now().duration_since(startup_at);
|
|
|
elapsed.as_secs() * 1000 + (elapsed.subsec_nanos() / 1000000) as u64
|
|
|
})
|
|
|
}
|
|
|
|
|
|
-pub fn setup_device(more_args: &[&str])
|
|
|
- -> (FaultInjector<EthernetTracer<TapInterface>>,
|
|
|
- Vec<String>) {
|
|
|
- let mut opts = getopts::Options::new();
|
|
|
+struct Dispose;
|
|
|
+
|
|
|
+impl io::Write for Dispose {
|
|
|
+ fn write(&mut self, data: &[u8]) -> io::Result<usize> {
|
|
|
+ Ok(data.len())
|
|
|
+ }
|
|
|
+
|
|
|
+ fn flush(&mut self) -> io::Result<()> {
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn create_options() -> (Options, Vec<&'static str>) {
|
|
|
+ let mut opts = Options::new();
|
|
|
+ opts.optflag("h", "help", "print this help menu");
|
|
|
+ (opts, Vec::new())
|
|
|
+}
|
|
|
+
|
|
|
+pub fn parse_options(options: &Options, free: Vec<&str>) -> Matches {
|
|
|
+ match options.parse(env::args().skip(1)) {
|
|
|
+ Err(err) => {
|
|
|
+ println!("{}", err);
|
|
|
+ process::exit(1)
|
|
|
+ }
|
|
|
+ Ok(matches) => {
|
|
|
+ if matches.opt_present("h") || matches.free.len() != free.len() {
|
|
|
+ let brief = format!("Usage: {} [OPTION]... {}",
|
|
|
+ env::args().nth(0).unwrap(), free.join(" "));
|
|
|
+ print!("{}", options.usage(&brief));
|
|
|
+ process::exit(if matches.free.len() != free.len() { 1 } else { 0 })
|
|
|
+ }
|
|
|
+ matches
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub fn add_tap_options(_opts: &mut Options, free: &mut Vec<&str>) {
|
|
|
+ free.push("INTERFACE");
|
|
|
+}
|
|
|
+
|
|
|
+pub fn parse_tap_options(matches: &mut Matches) -> TapInterface {
|
|
|
+ let interface = matches.free.remove(0);
|
|
|
+ TapInterface::new(&interface).unwrap()
|
|
|
+}
|
|
|
+
|
|
|
+pub fn add_middleware_options(opts: &mut Options, _free: &mut Vec<&str>) {
|
|
|
+ opts.optopt("", "pcap", "Write a packet capture file", "FILE");
|
|
|
opts.optopt("", "drop-chance", "Chance of dropping a packet (%)", "CHANCE");
|
|
|
opts.optopt("", "corrupt-chance", "Chance of corrupting a packet (%)", "CHANCE");
|
|
|
opts.optopt("", "size-limit", "Drop packets larger than given size (octets)", "SIZE");
|
|
@@ -52,16 +102,10 @@ pub fn setup_device(more_args: &[&str])
|
|
|
opts.optopt("", "rx-rate-limit", "Drop packets after transmit rate exceeds given limit \
|
|
|
(packets per interval)", "RATE");
|
|
|
opts.optopt("", "shaping-interval", "Sets the interval for rate limiting (ms)", "RATE");
|
|
|
- opts.optflag("h", "help", "print this help menu");
|
|
|
+}
|
|
|
|
|
|
- let matches = opts.parse(env::args().skip(1)).unwrap();
|
|
|
- if matches.opt_present("h") || matches.free.len() != more_args.len() + 1 {
|
|
|
- let brief = format!("Usage: {} INTERFACE {} [options]",
|
|
|
- env::args().nth(0).unwrap(),
|
|
|
- more_args.join(" "));
|
|
|
- print!("{}", opts.usage(&brief));
|
|
|
- process::exit(if matches.free.len() != more_args.len() + 1 { 1 } else { 0 });
|
|
|
- }
|
|
|
+pub fn parse_middleware_options<D: Device>(matches: &mut Matches, device: D, loopback: bool)
|
|
|
+ -> FaultInjector<EthernetTracer<PcapWriter<D, Rc<PcapSink>>>> {
|
|
|
let drop_chance = u8::from_str(&matches.opt_str("drop-chance")
|
|
|
.unwrap_or("0".to_string())).unwrap();
|
|
|
let corrupt_chance = u8::from_str(&matches.opt_str("corrupt-chance")
|
|
@@ -75,9 +119,18 @@ pub fn setup_device(more_args: &[&str])
|
|
|
let shaping_interval = u32::from_str(&matches.opt_str("shaping-interval")
|
|
|
.unwrap_or("0".to_string())).unwrap();
|
|
|
|
|
|
+ let pcap_writer: Box<io::Write>;
|
|
|
+ if let Some(pcap_filename) = matches.opt_str("pcap") {
|
|
|
+ pcap_writer = Box::new(File::create(pcap_filename).expect("cannot open file"))
|
|
|
+ } else {
|
|
|
+ pcap_writer = Box::new(Dispose)
|
|
|
+ }
|
|
|
+
|
|
|
let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().subsec_nanos();
|
|
|
|
|
|
- let device = TapInterface::new(&matches.free[0]).unwrap();
|
|
|
+ let device = PcapWriter::new(device, Rc::new(RefCell::new(pcap_writer)) as Rc<PcapSink>,
|
|
|
+ if loopback { PcapMode::TxOnly } else { PcapMode::Both },
|
|
|
+ PcapLinkType::Ethernet);
|
|
|
let device = EthernetTracer::new(device, |_timestamp, printer| trace!("{}", printer));
|
|
|
let mut device = FaultInjector::new(device, seed);
|
|
|
device.set_drop_chance(drop_chance);
|
|
@@ -86,6 +139,5 @@ pub fn setup_device(more_args: &[&str])
|
|
|
device.set_max_tx_rate(tx_rate_limit);
|
|
|
device.set_max_rx_rate(rx_rate_limit);
|
|
|
device.set_bucket_interval(Duration::from_millis(shaping_interval as u64));
|
|
|
-
|
|
|
- (device, matches.free[1..].to_owned())
|
|
|
+ device
|
|
|
}
|