Skip to content

Commit

Permalink
created MyLinkType enum and fixing appearance (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 31, 2023
1 parent 975ccc5 commit 228c509
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 38 deletions.
36 changes: 17 additions & 19 deletions src/gui/pages/overview_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use iced::widget::{
use iced::widget::{horizontal_space, Rule};
use iced::Length::{Fill, FillPortion, Fixed};
use iced::{Alignment, Font, Length, Renderer};
use pcap::Linktype;

use crate::countries::country_utils::get_flag_tooltip;
use crate::countries::flags_pictures::FLAGS_WIDTH_BIG;
Expand All @@ -29,6 +28,7 @@ use crate::networking::types::data_info::DataInfo;
use crate::networking::types::filters::Filters;
use crate::networking::types::host::Host;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::my_link_type::MyLinkType;
use crate::networking::types::search_parameters::SearchParameters;
use crate::report::get_report_entries::{get_app_entries, get_host_entries};
use crate::translations::translations::{
Expand All @@ -43,8 +43,7 @@ use crate::translations::translations_2::{
only_top_30_hosts_translation,
};
use crate::utils::formatted_strings::{
get_active_filters_string, get_adapter_link_type_str, get_formatted_bytes_string_with_b,
get_percentage_string,
get_active_filters_string, get_formatted_bytes_string_with_b, get_percentage_string,
};
use crate::utils::types::icon::Icon;
use crate::{AppProtocol, ChartType, ConfigSettings, Language, RunningPage, StyleType};
Expand Down Expand Up @@ -143,10 +142,10 @@ fn body_no_packets(
font: Font,
language: Language,
waiting: &str,
link_type: Linktype,
link_type: MyLinkType,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut adapter_name = device.name.clone();
adapter_name.push_str(&format!(" ({})", link_type.0,));
adapter_name.push_str(&format!("\n{}", link_type.full_print_on_one_line(language)));
let (icon_text, nothing_to_see_text) = if device.addresses.lock().unwrap().is_empty() {
(
Icon::Warning.to_text().size(60),
Expand Down Expand Up @@ -430,8 +429,7 @@ fn lazy_col_info(
let all_bytes = sniffer.runtime_data.all_bytes;
let link_type = sniffer.runtime_data.link_type;

let col_device_filters =
col_device_filters(language, font, &sniffer.filters, &sniffer.device, link_type);
let col_device = col_device(language, font, &sniffer.device, link_type);

let col_data_representation =
col_data_representation(language, font, sniffer.traffic_chart.chart_type);
Expand All @@ -444,6 +442,7 @@ fn lazy_col_info(
all_bytes,
filtered_bytes,
font,
&sniffer.filters,
);

let content = Column::new()
Expand All @@ -453,14 +452,14 @@ fn lazy_col_info(
Row::new()
.height(Length::Fixed(120.0))
.push(
Scrollable::new(col_device_filters)
.width(Length::Fill)
.direction(Direction::Vertical(ScrollbarType::properties())),
Scrollable::new(col_device)
.width(Length::FillPortion(1))
.direction(Direction::Horizontal(ScrollbarType::properties())),
)
.push(Rule::vertical(25))
.push(col_data_representation),
)
.push(Rule::horizontal(25))
.push(Rule::horizontal(15))
.push(
Scrollable::new(col_bytes_packets)
.width(Length::Fill)
Expand Down Expand Up @@ -515,12 +514,11 @@ fn container_chart(sniffer: &Sniffer, font: Font) -> Container<Message, Renderer
.style(ContainerType::BorderedRound)
}

fn col_device_filters(
fn col_device(
language: Language,
font: Font,
filters: &Filters,
device: &MyDevice,
link_type: Linktype,
link_type: MyLinkType,
) -> Column<'static, Message, Renderer<StyleType>> {
#[cfg(not(target_os = "windows"))]
let adapter_info = &device.name;
Expand All @@ -529,17 +527,15 @@ fn col_device_filters(
#[cfg(target_os = "windows")]
let adapter_info = device.desc.as_ref().unwrap_or(adapter_name);

let adapter_link_type = get_adapter_link_type_str(adapter_info, link_type);

Column::new()
.spacing(15)
.width(Length::FillPortion(1))
.push(TextType::highlighted_subtitle_with_desc(
network_adapter_translation(language),
&adapter_link_type,
&adapter_info,
font,
))
.push(vertical_space(15))
.push(get_active_filters_col(filters, language, font, false))
.push(link_type.link_type_col(language, font))
}

fn col_data_representation(
Expand Down Expand Up @@ -584,6 +580,7 @@ fn col_bytes_packets(
all_bytes: u128,
filtered_bytes: u128,
font: Font,
filters: &Filters,
) -> Column<'static, Message, Renderer<StyleType>> {
let dropped_val = if dropped > 0 {
format!(
Expand All @@ -606,6 +603,7 @@ fn col_bytes_packets(

Column::new()
.spacing(15)
.push(get_active_filters_col(filters, language, font, false))
.push(TextType::highlighted_subtitle_with_desc(
filtered_bytes_translation(language),
&bytes_value,
Expand Down
8 changes: 4 additions & 4 deletions src/gui/types/runtime_data.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Module defining the `RunTimeData` struct, useful to to generate chart and to display statistics about network traffic
//!
use pcap::Linktype;
use crate::networking::types::my_link_type::MyLinkType;
use std::collections::VecDeque;

use crate::notifications::types::logged_notification::LoggedNotification;

/// Struct containing useful data to display statistics about network traffic and the relative notifications
pub struct RunTimeData {
/// Link type of the current capture (e.g., ethernet)
pub link_type: Linktype,
pub link_type: MyLinkType,
/// Total number of bytes (filtered and not filtered)
pub all_bytes: u128,
/// Total number of packets (filtered and not filtered)
Expand Down Expand Up @@ -41,7 +41,7 @@ impl RunTimeData {
/// Constructs a new `ChartsData` element.
pub fn new() -> Self {
RunTimeData {
link_type: Linktype::ETHERNET,
link_type: MyLinkType::NotYetAssigned,
all_bytes: 0,
all_packets: 0,
tot_sent_bytes: 0,
Expand Down
6 changes: 3 additions & 3 deletions src/networking/types/info_traffic.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//! Module defining the `ReportInfo` struct, useful to format the output report file and
//! to keep track of statistics about the sniffed traffic.
use pcap::Linktype;
use std::collections::{HashMap, HashSet};

use crate::networking::types::address_port_pair::AddressPortPair;
use crate::networking::types::data_info::DataInfo;
use crate::networking::types::data_info_host::DataInfoHost;
use crate::networking::types::host::Host;
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_link_type::MyLinkType;
use crate::networking::types::traffic_direction::TrafficDirection;
use crate::AppProtocol;

/// Struct to be shared between the threads in charge of parsing packets and update reports.
pub struct InfoTraffic {
/// Link type of the current capture (e.g., ethernet)
pub link_type: Linktype,
pub link_type: MyLinkType,
/// Total amount of filtered bytes received.
pub tot_received_bytes: u128,
/// Total amount of filtered bytes sent.
Expand Down Expand Up @@ -50,7 +50,7 @@ impl InfoTraffic {
/// Constructs a new `InfoTraffic` element.
pub fn new() -> Self {
InfoTraffic {
link_type: Linktype::ETHERNET,
link_type: MyLinkType::NotYetAssigned,
tot_received_bytes: 0,
tot_sent_bytes: 0,
tot_received_packets: 0,
Expand Down
1 change: 1 addition & 0 deletions src/networking/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod info_traffic;
pub mod ip_collection;
pub mod ip_version;
pub mod my_device;
pub mod my_link_type;
pub mod packet_filters_fields;
pub mod port_collection;
pub mod protocol;
Expand Down
89 changes: 89 additions & 0 deletions src/networking/types/my_link_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use crate::gui::styles::text::TextType;
use crate::gui::types::message::Message;
use crate::translations::translations_3::link_type_translation;
use crate::{Language, StyleType};
use iced::widget::Column;
use iced::{Font, Renderer};
use pcap::Linktype;

#[derive(Copy, Clone)]
pub enum MyLinkType {
Supported(Linktype),
Unsupported(Linktype),
NotYetAssigned,
}

impl MyLinkType {
pub fn is_supported(&self) -> bool {
match self {
MyLinkType::Supported(_) => true,
MyLinkType::Unsupported(_) | MyLinkType::NotYetAssigned => false,
}
}

pub fn from_pcap_link_type(link_type: Linktype) -> Self {
match link_type {
Linktype::NULL
| Linktype::ETHERNET
| Linktype(12)
| Linktype::LOOP
| Linktype::IPV4
| Linktype::IPV6 => Self::Supported(link_type),
_ => Self::Unsupported(link_type),
}
}

pub fn full_print_on_one_line(&self, language: Language) -> String {
match self {
MyLinkType::Supported(l) => {
format!(
"{}: {} ({})",
link_type_translation(language),
l.get_name().unwrap_or(l.0.to_string()),
l.get_description().unwrap_or(String::new())
)
}
MyLinkType::Unsupported(l) => {
format!(
"{}: {} (NOT SUPPORTED)",
link_type_translation(language),
l.get_name().unwrap_or(l.0.to_string()),
)
}
MyLinkType::NotYetAssigned => String::new(),
}
}

pub fn link_type_col(
&self,
language: Language,
font: Font,
) -> Column<'static, Message, Renderer<StyleType>> {
match self {
MyLinkType::Supported(l) => {
let link_info = format!(
"{} ({})",
l.get_name().unwrap_or(l.0.to_string()),
l.get_description().unwrap_or(String::new())
);
TextType::highlighted_subtitle_with_desc(
link_type_translation(language),
&link_info,
font,
)
}
MyLinkType::Unsupported(l) => {
let link_info = format!(
"{} (NOT SUPPORTED)",
l.get_name().unwrap_or(l.0.to_string()),
);
TextType::highlighted_subtitle_with_desc(
link_type_translation(language),
&link_info,
font,
)
}
MyLinkType::NotYetAssigned => Column::new().height(0),
}
}
}
3 changes: 2 additions & 1 deletion src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::networking::types::filters::Filters;
use crate::networking::types::icmp_type::IcmpType;
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::my_link_type::MyLinkType;
use crate::networking::types::packet_filters_fields::PacketFiltersFields;
use crate::InfoTraffic;

Expand All @@ -35,7 +36,7 @@ pub fn parse_packets(
let capture_id = *current_capture_id.lock().unwrap();

let link_type = cap.get_datalink();
info_traffic_mutex.lock().unwrap().link_type = link_type;
info_traffic_mutex.lock().unwrap().link_type = MyLinkType::from_pcap_link_type(link_type);

loop {
match cap.next_packet() {
Expand Down
8 changes: 8 additions & 0 deletions src/translations/translations_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ pub fn messages_translation(language: Language) -> &'static str {
_ => "Messages",
}
}

pub fn link_type_translation(language: Language) -> &'static str {
match language {
Language::EN => "Link type",
Language::IT => "Tipo di collegamento",
_ => "Link type",
}
}
11 changes: 0 additions & 11 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use pcap::Linktype;
use std::net::IpAddr;

use crate::networking::types::filters::Filters;
Expand Down Expand Up @@ -77,16 +76,6 @@ pub fn get_active_filters_string(filters: &Filters, language: Language) -> Strin
filters_string
}

pub fn get_adapter_link_type_str(adapter: &str, link_type: Linktype) -> String {
let link_type_str = match link_type {
Linktype::ETHERNET => "(Ethernet)",
Linktype::NULL | Linktype::LOOP => "(null/loopback)",
Linktype::IPV4 | Linktype::IPV6 => "(raw IP)",
_ => "",
};
format!("{adapter} {link_type_str}")
}

/// Returns a String representing a quantity of bytes with its proper multiple (K, M, G, T)
pub fn get_formatted_bytes_string(bytes: u128) -> String {
let mut multiple_transmitted = String::new();
Expand Down

0 comments on commit 228c509

Please sign in to comment.