Skip to content

Commit

Permalink
changed MyLinkType logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 31, 2023
1 parent 228c509 commit d6e8872
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/chart/manage_chart_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ fn get_max(deque: &VecDeque<(u32, i64)>) -> i64 {

#[cfg(test)]
mod tests {
use pcap::Linktype;
use std::collections::VecDeque;

use crate::chart::manage_chart_data::{get_max, get_min, update_charts_data};
use crate::networking::types::my_link_type::MyLinkType;
use crate::{ChartType, Language, RunTimeData, StyleType, TrafficChart};

#[test]
Expand Down Expand Up @@ -168,7 +168,7 @@ mod tests {
style: StyleType::default(),
};
let mut runtime_data = RunTimeData {
link_type: Linktype::ETHERNET,
link_type: MyLinkType::NotYetAssigned,
all_bytes: 0,
all_packets: 0,
tot_sent_bytes: tot_sent + 1111,
Expand Down
15 changes: 6 additions & 9 deletions src/gui/pages/overview_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ fn lazy_col_info(
.direction(Direction::Horizontal(ScrollbarType::properties())),
)
.push(Rule::vertical(25))
.push(col_data_representation),
.push(col_data_representation.width(Length::FillPortion(1))),
)
.push(Rule::horizontal(15))
.push(
Expand Down Expand Up @@ -523,16 +523,13 @@ fn col_device(
#[cfg(not(target_os = "windows"))]
let adapter_info = &device.name;
#[cfg(target_os = "windows")]
let adapter_name = &device.name;
#[cfg(target_os = "windows")]
let adapter_info = device.desc.as_ref().unwrap_or(adapter_name);
let adapter_info = device.desc.as_ref().unwrap_or(&device.name);

Column::new()
.spacing(15)
.width(Length::FillPortion(1))
.spacing(10)
.push(TextType::highlighted_subtitle_with_desc(
network_adapter_translation(language),
&adapter_info,
adapter_info,
font,
))
.push(link_type.link_type_col(language, font))
Expand All @@ -543,7 +540,7 @@ fn col_data_representation(
font: Font,
chart_type: ChartType,
) -> Column<'static, Message, Renderer<StyleType>> {
let mut ret_val = Column::new().spacing(5).width(Length::FillPortion(1)).push(
let mut ret_val = Column::new().spacing(5).push(
Text::new(format!("{}:", data_representation_translation(language)))
.style(TextType::Subtitle)
.font(font),
Expand Down Expand Up @@ -602,7 +599,7 @@ fn col_bytes_packets(
};

Column::new()
.spacing(15)
.spacing(10)
.push(get_active_filters_col(filters, language, font, false))
.push(TextType::highlighted_subtitle_with_desc(
filtered_bytes_translation(language),
Expand Down
8 changes: 5 additions & 3 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::networking::types::filters::Filters;
use crate::networking::types::host::Host;
use crate::networking::types::ip_collection::AddressCollection;
use crate::networking::types::my_device::MyDevice;
use crate::networking::types::my_link_type::MyLinkType;
use crate::networking::types::port_collection::PortCollection;
use crate::networking::types::search_parameters::SearchParameters;
use crate::notifications::notify_and_log::notify_and_log;
Expand Down Expand Up @@ -293,7 +294,6 @@ impl Sniffer {
fn refresh_data(&mut self) -> Command<Message> {
let info_traffic_lock = self.info_traffic.lock().unwrap();
self.runtime_data.all_packets = info_traffic_lock.all_packets;
self.runtime_data.link_type = info_traffic_lock.link_type;
if info_traffic_lock.tot_received_packets + info_traffic_lock.tot_sent_packets == 0 {
drop(info_traffic_lock);
return self.update(Message::Waiting);
Expand Down Expand Up @@ -352,7 +352,7 @@ impl Sniffer {
let current_device_name = &*self.device.name.clone();
self.set_adapter(current_device_name);
let device = self.device.clone();
let (pcap_error, cap) = get_capture_result(&device);
let (pcap_error, cap_result) = get_capture_result(&device);
self.pcap_error = pcap_error.clone();
let info_traffic_mutex = self.info_traffic.clone();
*info_traffic_mutex.lock().unwrap() = InfoTraffic::new();
Expand All @@ -365,17 +365,19 @@ impl Sniffer {

if pcap_error.is_none() {
// no pcap error
let cap = cap_result.unwrap();
let current_capture_id = self.current_capture_id.clone();
let filters = self.filters.clone();
let country_mmdb_reader = self.country_mmdb_reader.clone();
let asn_mmdb_reader = self.asn_mmdb_reader.clone();
self.runtime_data.link_type = MyLinkType::from_pcap_link_type(cap.get_datalink());
thread::Builder::new()
.name("thread_parse_packets".to_string())
.spawn(move || {
parse_packets(
&current_capture_id,
&device,
cap.unwrap(),
cap,
&filters,
&info_traffic_mutex,
&country_mmdb_reader,
Expand Down
4 changes: 0 additions & 4 deletions src/networking/types/info_traffic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ 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: MyLinkType,
/// Total amount of filtered bytes received.
pub tot_received_bytes: u128,
/// Total amount of filtered bytes sent.
Expand Down Expand Up @@ -50,7 +47,6 @@ impl InfoTraffic {
/// Constructs a new `InfoTraffic` element.
pub fn new() -> Self {
InfoTraffic {
link_type: MyLinkType::NotYetAssigned,
tot_received_bytes: 0,
tot_sent_bytes: 0,
tot_received_packets: 0,
Expand Down
48 changes: 32 additions & 16 deletions src/networking/types/my_link_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,47 @@ use iced::widget::Column;
use iced::{Font, Renderer};
use pcap::Linktype;

/// Currently supported link types
#[derive(Copy, Clone)]
pub enum MyLinkType {
Supported(Linktype),
Null(Linktype),
Ethernet(Linktype),
RawIp(Linktype),
Loop(Linktype),
IPv4(Linktype),
IPv6(Linktype),
Unsupported(Linktype),
NotYetAssigned,
}

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

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),
Linktype::NULL => Self::Null(link_type),
Linktype::ETHERNET => Self::Ethernet(link_type),
Linktype(12) => Self::RawIp(link_type),
Linktype::LOOP => Self::Loop(link_type),
Linktype::IPV4 => Self::IPv4(link_type),
Linktype::IPV6 => Self::IPv6(link_type),
_ => Self::Unsupported(link_type),
}
}

pub fn full_print_on_one_line(&self, language: Language) -> String {
pub fn full_print_on_one_line(self, language: Language) -> String {
match self {
MyLinkType::Supported(l) => {
Self::Null(l)
| Self::Ethernet(l)
| Self::RawIp(l)
| Self::Loop(l)
| Self::IPv4(l)
| Self::IPv6(l) => {
format!(
"{}: {} ({})",
link_type_translation(language),
Expand All @@ -55,12 +66,17 @@ impl MyLinkType {
}

pub fn link_type_col(
&self,
self,
language: Language,
font: Font,
) -> Column<'static, Message, Renderer<StyleType>> {
match self {
MyLinkType::Supported(l) => {
Self::Null(l)
| Self::Ethernet(l)
| Self::RawIp(l)
| Self::Loop(l)
| Self::IPv4(l)
| Self::IPv6(l) => {
let link_info = format!(
"{} ({})",
l.get_name().unwrap_or(l.0.to_string()),
Expand Down
17 changes: 9 additions & 8 deletions src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex};
use std::thread;

use etherparse::{PacketHeaders, ReadError};
use pcap::{Active, Capture, Linktype, Packet};
use pcap::{Active, Capture, Packet};

use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::manage_packets::{
Expand Down Expand Up @@ -35,8 +35,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 = MyLinkType::from_pcap_link_type(link_type);
let my_link_type = MyLinkType::from_pcap_link_type(cap.get_datalink());

loop {
match cap.next_packet() {
Expand All @@ -50,7 +49,7 @@ pub fn parse_packets(
if *current_capture_id.lock().unwrap() != capture_id {
return;
}
if let Ok(headers) = get_sniffable_headers(&packet, link_type) {
if let Ok(headers) = get_sniffable_headers(&packet, my_link_type) {
let mut exchanged_bytes = 0;
let mut mac_addresses = (None, None);
let mut icmp_type = IcmpType::default();
Expand Down Expand Up @@ -195,11 +194,13 @@ pub fn parse_packets(

fn get_sniffable_headers<'a>(
packet: &'a Packet,
link_type: Linktype,
my_link_type: MyLinkType,
) -> Result<PacketHeaders<'a>, ReadError> {
match link_type {
Linktype(12) | Linktype::IPV4 | Linktype::IPV6 => PacketHeaders::from_ip_slice(packet),
Linktype::NULL | Linktype::LOOP => from_null_slice(packet),
match my_link_type {
MyLinkType::RawIp(_) | MyLinkType::IPv4(_) | MyLinkType::IPv6(_) => {
PacketHeaders::from_ip_slice(packet)
}
MyLinkType::Null(_) | MyLinkType::Loop(_) => from_null_slice(packet),
_ => PacketHeaders::from_ethernet_slice(packet),
}
}
Expand Down

0 comments on commit d6e8872

Please sign in to comment.