From 1573f8a4b1fbc2e10f7ce352eb8504ff04233fdf Mon Sep 17 00:00:00 2001 From: Giuliano Bellini s294739 Date: Sat, 3 Sep 2022 00:30:19 +0200 Subject: [PATCH] added experimental feature to draw graphical reports --- .gitignore | 3 ++ Cargo.toml | 8 +++- src/thread_write_report_functions.rs | 70 +++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index f9ae26be..33571ae1 100644 --- a/.gitignore +++ b/.gitignore @@ -223,4 +223,7 @@ $RECYCLE.BIN/ #folder with gif and images /img +#graphs +*.svg + .idea \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 55baadb7..a8bab208 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ chrono = "0.4.20" crossterm = "0.13.3" colored = "2.0.0" thousands = "0.2.0" +#charts = "0.3.0" #used for debug purposes to discover ports not yet assigned to a known application layer protocols @@ -29,4 +30,9 @@ thousands = "0.2.0" #used for debug purposes to measure time needed to write output file report #[features] #default = ["elapsed_time"] -#elapsed_time = [] \ No newline at end of file +#elapsed_time = [] + +#experimental: plot received packets in a line series chart +#[features] +#default = ["draw_graph"] +#draw_graph = [] \ No newline at end of file diff --git a/src/thread_write_report_functions.rs b/src/thread_write_report_functions.rs index 2fdc19bc..5d9819ea 100644 --- a/src/thread_write_report_functions.rs +++ b/src/thread_write_report_functions.rs @@ -23,6 +23,9 @@ #[cfg(feature = "elapsed_time")] use std::time::{Instant}; +#[cfg(feature = "draw_graph")] +use charts::{Chart, ScaleLinear, MarkerType, LineSeriesView}; + /// The calling thread enters in a loop in which it waits for ```interval``` seconds and then re-write /// from scratch the output report file, with updated values. @@ -77,6 +80,11 @@ pub fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval #[cfg(feature = "elapsed_time")] let mut last_10_write_times = vec![]; + #[cfg(feature = "draw_graph")] + let mut tot_packets_graph = vec![(0.0,0.0)]; + #[cfg(feature = "draw_graph")] + let mut filtered_packets_graph = vec![(0.0,0.0)]; + loop { thread::sleep(Duration::from_secs(interval)); @@ -88,6 +96,15 @@ pub fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval let map_sniffed_filtered_app = mutex_map.lock().expect("Error acquiring mutex\n\r"); + let tot_packets = map_sniffed_filtered_app.1; + let filtered_packets = map_sniffed_filtered_app.2; + + #[cfg(feature = "draw_graph")] + { + tot_packets_graph.push((interval as f32 *times_report_updated as f32,tot_packets as f32)); + filtered_packets_graph.push((interval as f32 *times_report_updated as f32,filtered_packets as f32)); + } + #[cfg(feature = "elapsed_time")] let start = Instant::now(); @@ -95,8 +112,8 @@ pub fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval device_name.clone(), first_timestamp.clone(), times_report_updated, lowest_port, highest_port, min_packets, network_layer.clone(), transport_layer.clone(), app_layer.clone(), - map_sniffed_filtered_app.0.len(), map_sniffed_filtered_app.1, - map_sniffed_filtered_app.2, map_sniffed_filtered_app.3.clone()); + map_sniffed_filtered_app.0.len(), tot_packets, + filtered_packets, map_sniffed_filtered_app.3.clone()); #[cfg(feature = "elapsed_time")] let time_header = start.elapsed().as_millis(); @@ -153,6 +170,55 @@ pub fn sleep_and_write_report_loop(lowest_port: u16, highest_port: u16, interval output.flush().expect("Error writing output file\n\r"); + + //experimental: plot received packets in a line series chart + #[cfg(feature = "draw_graph")] + { + let width = 1120; + let height = 700; + let (top, right, bottom, left) = (90, 40, 50, 60); + + let x = ScaleLinear::new() + .set_domain(vec![0_f32, interval as f32 * times_report_updated as f32]) + .set_range(vec![0, width - left - right]); + + let y = ScaleLinear::new() + .set_domain(vec![0_f32, 1.5*tot_packets as f32]) + .set_range(vec![height - top - bottom, 0]); + + let tot_packets_view = LineSeriesView::new() + .set_x_scale(&x) + .set_y_scale(&y) + .set_marker_type(MarkerType::Square) + .set_label_visibility(false) + .load_data(&tot_packets_graph).unwrap(); + + let filtered_packets_view = LineSeriesView::new() + .set_x_scale(&x) + .set_y_scale(&y) + .set_marker_type(MarkerType::Circle) + .set_label_visibility(false) + .load_data(&filtered_packets_graph).unwrap(); + + Chart::new() + .set_width(width) + .set_height(height) + .set_margins(top, right, bottom, left) + .add_title(String::from("Total number of sniffed packets")) + .add_view(&tot_packets_view) + .add_view(&filtered_packets_view) + .add_axis_bottom(&x) + .add_axis_left(&y) + .add_bottom_axis_label("Time (s)") + .save("sniffnet_graph.svg") + .unwrap(); + + // if tot_packets > filtered_packets { + // chart.add_view(&filtered_packets_view); + // } + + } + let mut status = status_pair.0.lock().expect("Error acquiring mutex\n\r"); if *status == Status::Running { if times_report_updated - last_report_updated_console != 1 {