Skip to content

Commit

Permalink
print link_type_str and fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
GyulyVGC committed Dec 30, 2023
1 parent 4696a5e commit f0c1d77
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 60 deletions.
6 changes: 1 addition & 5 deletions src/gui/pages/connection_details_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,7 @@ fn get_src_or_dest_col(
address_translation(language)
};

let mac_str = if let Some(val) = mac {
&val
} else {
"-"
};
let mac_str = if let Some(val) = mac { val } else { "-" };

Column::new()
.spacing(4)
Expand Down
11 changes: 6 additions & 5 deletions src/gui/pages/overview_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use crate::translations::translations_2::{
only_top_30_hosts_translation,
};
use crate::utils::formatted_strings::{
get_active_filters_string, get_formatted_bytes_string_with_b, get_percentage_string,
get_active_filters_string, get_adapter_link_type_str, 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 @@ -522,19 +523,19 @@ fn col_device_filters(
link_type: Linktype,
) -> Column<'static, Message, Renderer<StyleType>> {
#[cfg(not(target_os = "windows"))]
let mut adapter_info = device.name.to_owned();
let adapter_info = &device.name;
#[cfg(target_os = "windows")]
let adapter_name = &device.name;
#[cfg(target_os = "windows")]
let mut adapter_info = device.desc.as_ref().unwrap_or(adapter_name);
let adapter_info = device.desc.as_ref().unwrap_or(adapter_name);

adapter_info.push_str(&format!(" ({})", link_type.0,));
let adapter_link_type = get_adapter_link_type_str(adapter_info, link_type);

Column::new()
.width(Length::FillPortion(1))
.push(TextType::highlighted_subtitle_with_desc(
network_adapter_translation(language),
&adapter_info,
&adapter_link_type,
font,
))
.push(vertical_space(15))
Expand Down
17 changes: 7 additions & 10 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,13 @@ fn analyze_link_header(
link_header: Option<Ethernet2Header>,
mac_address1: &mut Option<String>,
mac_address2: &mut Option<String>,
) {
match link_header {
Some(header) => {
*mac_address1 = Some(mac_from_dec_to_hex(header.source));
*mac_address2 = Some(mac_from_dec_to_hex(header.destination));
}
_ => {
*mac_address1 = None;
*mac_address2 = None;
},
) {
if let Some(header) = link_header {
*mac_address1 = Some(mac_from_dec_to_hex(header.source));
*mac_address2 = Some(mac_from_dec_to_hex(header.destination));
} else {
*mac_address1 = None;
*mac_address2 = None;
}
}

Expand Down
90 changes: 50 additions & 40 deletions src/secondary_threads/parse_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ pub fn parse_packets(
asn_mmdb_reader: &Arc<MmdbReader>,
) {
let capture_id = *current_capture_id.lock().unwrap();
let mut is_first_packet = true;

// pcap seems to assign ethernet to all interfaces (at least on macOS)...
let mut link_type = cap.get_datalink();
// ...it'll be confirmed after having parsed the first packet!
let mut is_link_type_confirmed = false;

loop {
match cap.next_packet() {
Expand All @@ -47,19 +50,12 @@ pub fn parse_packets(
if *current_capture_id.lock().unwrap() != capture_id {
return;
}
if let Ok(headers) = match is_first_packet {
true => {
is_first_packet = false;
get_sniffable_headers(&packet, &mut link_type, info_traffic_mutex)
}
false => match link_type {
Linktype::IPV4 | Linktype::IPV6 => PacketHeaders::from_ip_slice(&packet),
Linktype::NULL | Linktype::LOOP => {
PacketHeaders::from_ip_slice(&packet[4..])
}
_ => PacketHeaders::from_ethernet_slice(&packet),
},
} {
if let Ok(headers) = get_sniffable_headers(
&packet,
&mut link_type,
info_traffic_mutex,
&mut is_link_type_confirmed,
) {
let mut exchanged_bytes = 0;
let mut mac_addresses = (None, None);
let mut icmp_type = IcmpType::default();
Expand Down Expand Up @@ -206,39 +202,53 @@ fn get_sniffable_headers<'a>(
packet: &'a Packet,
link_type: &mut Linktype,
info_traffic_mutex: &Arc<Mutex<InfoTraffic>>,
is_link_type_confirmed: &mut bool,
) -> Result<PacketHeaders<'a>, ReadError> {
let ethernet_result = PacketHeaders::from_ethernet_slice(&packet);
let ip_result = PacketHeaders::from_ip_slice(&packet);
let null_result = PacketHeaders::from_ip_slice(&packet[4..]);

let is_ethernet_sniffable = are_headers_sniffable(&ethernet_result);
let is_ip_sniffable = are_headers_sniffable(&ip_result);
let is_null_sniffable = are_headers_sniffable(&null_result);

match (is_ethernet_sniffable, is_ip_sniffable, is_null_sniffable) {
(true, _, _) => {
*link_type = Linktype::ETHERNET;
info_traffic_mutex.lock().unwrap().link_type = Linktype::ETHERNET;
ethernet_result
}
(_, true, _) => {
*link_type = Linktype::IPV4;
info_traffic_mutex.lock().unwrap().link_type = Linktype::IPV4;
ip_result
}
(_, _, true) => {
*link_type = Linktype::NULL;
info_traffic_mutex.lock().unwrap().link_type = Linktype::NULL;
null_result
match is_link_type_confirmed {
false => {
*is_link_type_confirmed = true;

let ethernet_result = PacketHeaders::from_ethernet_slice(packet);
let ip_result = PacketHeaders::from_ip_slice(packet);
let null_result = PacketHeaders::from_ip_slice(&packet[4..]);

let is_ethernet_sniffable = are_headers_sniffable(&ethernet_result);
let is_ip_sniffable = are_headers_sniffable(&ip_result);
let is_null_sniffable = are_headers_sniffable(&null_result);

match (is_ethernet_sniffable, is_ip_sniffable, is_null_sniffable) {
(true, _, _) => {
*link_type = Linktype::ETHERNET;
info_traffic_mutex.lock().unwrap().link_type = Linktype::ETHERNET;
ethernet_result
}
(_, true, _) => {
// it could be IPV4 as well as IPV6 but it should be the same
*link_type = Linktype::IPV4;
info_traffic_mutex.lock().unwrap().link_type = Linktype::IPV4;
ip_result
}
(_, _, true) => {
// it could be NULL as well as LOOP but it should be the same
*link_type = Linktype::NULL;
info_traffic_mutex.lock().unwrap().link_type = Linktype::NULL;
null_result
}
(false, false, false) => ethernet_result,
}
}
(false, false, false) => ethernet_result,
true => match *link_type {
Linktype::IPV4 | Linktype::IPV6 => PacketHeaders::from_ip_slice(packet),
Linktype::NULL | Linktype::LOOP => PacketHeaders::from_ip_slice(&packet[4..]),
_ => PacketHeaders::from_ethernet_slice(packet),
},
}
}

fn are_headers_sniffable(headers_result: &Result<PacketHeaders, ReadError>) -> bool {
return if let Ok(headers) = headers_result {
if let Ok(headers) = headers_result {
headers.ip.is_some() && headers.transport.is_some()
} else {
false
};
}
}
11 changes: 11 additions & 0 deletions src/utils/formatted_strings.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use pcap::Linktype;
use std::net::IpAddr;

use crate::networking::types::filters::Filters;
Expand Down Expand Up @@ -76,6 +77,16 @@ 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 f0c1d77

Please sign in to comment.