Skip to content

Commit

Permalink
use HashMap over IndexMap and remove addresses_last_interval
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 2, 2023
1 parent eef16f2 commit f26e7d7
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 62 deletions.
35 changes: 17 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ strip = true
pcap = "1.1.0"
etherparse = "0.13.0"
chrono = { version = "0.4.31", default_features = false, features = ["clock"] }
indexmap = "2.1.0"
plotters = { version = "0.3.5", default_features = false, features = ["area_series"] }
iced = { version = "0.10.0", features = ["tokio", "svg", "advanced", "lazy"] }
plotters-iced = "0.9.0"
Expand Down
6 changes: 2 additions & 4 deletions src/gui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Application for Sniffer {

let content = Column::new().push(header).push(body).push(footer);

match self.modal {
match self.modal.clone() {
None => {
if let Some(settings_page) = self.settings_page {
let overlay = match settings_page {
Expand Down Expand Up @@ -118,9 +118,7 @@ impl Application for Sniffer {
font_headers,
self.language,
),
MyModal::ConnectionDetails(connection_index) => {
connection_details_page(self, connection_index)
}
MyModal::ConnectionDetails(key) => connection_details_page(self, key),
};

Modal::new(content, overlay)
Expand Down
6 changes: 4 additions & 2 deletions src/gui/components/types/my_modal.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::networking::types::address_port_pair::AddressPortPair;

/// This enum defines the currently displayed modal.
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum MyModal {
/// Quit modal.
Quit,
/// Clear all modal.
ClearAll,
/// Connection details modal.
ConnectionDetails(usize),
ConnectionDetails(AddressPortPair),
}
17 changes: 7 additions & 10 deletions src/gui/pages/connection_details_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ use crate::{Language, Sniffer, StyleType};

pub fn connection_details_page(
sniffer: &Sniffer,
connection_index: usize,
key: AddressPortPair,
) -> Container<Message, Renderer<StyleType>> {
Container::new(lazy(
sniffer.runtime_data.tot_sent_packets + sniffer.runtime_data.tot_received_packets,
move |_| page_content(sniffer, connection_index),
move |_| page_content(sniffer, &key),
))
}

fn page_content(
sniffer: &Sniffer,
connection_index: usize,
key: &AddressPortPair,
) -> Container<'static, Message, Renderer<StyleType>> {
let font = get_font(sniffer.style);
let font_headers = get_font_headers(sniffer.style);
Expand All @@ -53,11 +53,8 @@ fn page_content(
.info_traffic
.lock()
.expect("Error acquiring mutex\n\r");
let key_val: (&AddressPortPair, &InfoAddressPortPair) =
info_traffic_lock.map.get_index(connection_index).unwrap();
let key = key_val.0.clone();
let val = key_val.1.clone();
let address_to_lookup = get_address_to_lookup(&key, val.traffic_direction);
let val = info_traffic_lock.map.get(key).unwrap().clone();
let address_to_lookup = get_address_to_lookup(key, val.traffic_direction);
let host_option = info_traffic_lock
.addresses_resolved
.get(&address_to_lookup)
Expand Down Expand Up @@ -99,7 +96,7 @@ fn page_content(
sniffer.language,
font,
);
let computer = get_local_tooltip(sniffer, &address_to_lookup, &key);
let computer = get_local_tooltip(sniffer, &address_to_lookup, key);
if address_to_lookup.eq(&key.address1) {
source_caption = source_caption.push(flag);
dest_caption = dest_caption.push(computer);
Expand Down Expand Up @@ -132,7 +129,7 @@ fn page_content(
dest_col = dest_col.push(host_info_col);
}

let col_info = col_info(&key, &val, font, sniffer.language);
let col_info = col_info(key, &val, font, sniffer.language);

let content = assemble_widgets(col_info, source_col, dest_col);

Expand Down
2 changes: 1 addition & 1 deletion src/gui/pages/inspect_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ fn lazy_report(sniffer: &Sniffer) -> Container<'static, Message, Renderer<StyleT
button(entry_row)
.padding(2)
.on_press(Message::ShowModal(MyModal::ConnectionDetails(
report_entry.val.index,
report_entry.key,
)))
.style(ButtonType::Neutral),
);
Expand Down
6 changes: 3 additions & 3 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ impl Sniffer {
}

fn switch_page(&mut self, next: bool) {
match (self.running_page, self.settings_page, self.modal) {
(_, Some(current_setting), None) => {
match (self.running_page, self.settings_page, self.modal.is_none()) {
(_, Some(current_setting), true) => {
// Settings opened
if next {
self.settings_page = Some(current_setting.next());
Expand All @@ -476,7 +476,7 @@ impl Sniffer {
(
RunningPage::Inspect | RunningPage::Notifications | RunningPage::Overview,
None,
None,
true,
) => {
// Running with no overlays
if self.runtime_data.tot_sent_packets + self.runtime_data.tot_received_packets > 0 {
Expand Down
13 changes: 1 addition & 12 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,7 @@ pub fn modify_or_insert_in_map(
let destination_ip = &key.address2;
let very_long_address = source_ip.len() > 25 || destination_ip.len() > 25;

let len = info_traffic_mutex.lock().unwrap().map.len();
let index = info_traffic_mutex
.lock()
.unwrap()
.map
.get_index_of(key)
.unwrap_or(len);

if index == len {
if !info_traffic_mutex.lock().unwrap().map.contains_key(key) {
// first occurrence of key

// update device addresses
Expand Down Expand Up @@ -222,12 +214,9 @@ pub fn modify_or_insert_in_map(
app_protocol: application_protocol,
very_long_address,
traffic_direction,
index,
})
.clone();

info_traffic.addresses_last_interval.insert(index);

if let Some(host_info) = info_traffic
.addresses_resolved
.get(&get_address_to_lookup(key, traffic_direction))
Expand Down
2 changes: 1 addition & 1 deletion src/networking/types/address_port_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fmt;
use crate::TransProtocol;

/// Struct representing a network address:port pair.
#[derive(PartialEq, Eq, Hash, Clone)]
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub struct AddressPortPair {
/// Network layer IPv4 or IPv6 source address.
pub address1: String,
Expand Down
3 changes: 0 additions & 3 deletions src/networking/types/info_address_port_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ pub struct InfoAddressPortPair {
pub app_protocol: AppProtocol,
/// Check if source or destination is an IPv6 address longer than 25 bytes (used for layout)
pub very_long_address: bool,
/// Integer corresponding to the index inside the connections map
pub index: usize,
/// Determines if the connection is incoming or outgoing
pub traffic_direction: TrafficDirection,
}
Expand All @@ -48,7 +46,6 @@ impl Default for InfoAddressPortPair {
app_protocol: AppProtocol::Other,
very_long_address: false,
traffic_direction: TrafficDirection::default(),
index: 0,
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/networking/types/info_traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::collections::{HashMap, HashSet};

use indexmap::IndexMap;

use crate::networking::types::address_port_pair::AddressPortPair;
use crate::networking::types::data_info::DataInfo;
use crate::networking::types::data_info_host::DataInfoHost;
Expand All @@ -30,9 +28,7 @@ pub struct InfoTraffic {
/// Number of dropped packets
pub dropped_packets: u32,
/// Map of the filtered traffic
pub map: IndexMap<AddressPortPair, InfoAddressPortPair>,
/// Set with the addresses of the last time interval
pub addresses_last_interval: HashSet<usize>,
pub map: HashMap<AddressPortPair, InfoAddressPortPair>,
/// Collection of the favorite hosts
pub favorite_hosts: HashSet<Host>,
/// Collection of favorite hosts that exchanged data in the last interval
Expand All @@ -58,8 +54,7 @@ impl InfoTraffic {
all_packets: 0,
all_bytes: 0,
dropped_packets: 0,
map: IndexMap::new(),
addresses_last_interval: HashSet::new(),
map: HashMap::new(),
favorite_hosts: HashSet::new(),
favorites_last_interval: HashSet::new(),
app_protocols: HashMap::new(),
Expand Down

0 comments on commit f26e7d7

Please sign in to comment.