Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for more link types: raw IP and null/loopback #421

Merged
merged 16 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/chart/manage_chart_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ 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};
Expand Down Expand Up @@ -167,6 +168,7 @@ mod tests {
style: StyleType::default(),
};
let mut runtime_data = RunTimeData {
link_type: Linktype::ETHERNET,
all_bytes: 0,
all_packets: 0,
tot_sent_bytes: tot_sent + 1111,
Expand Down
7 changes: 5 additions & 2 deletions src/gui/pages/connection_details_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ fn get_src_or_dest_col(
caption: Row<'static, Message, Renderer<StyleType>>,
ip: &String,
port: Option<u16>,
mac: &str,
mac: &Option<String>,
font: Font,
language: Language,
timing_events: &TimingEvents,
Expand All @@ -348,6 +348,9 @@ fn get_src_or_dest_col(
} else {
address_translation(language)
};

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

Column::new()
.spacing(4)
.push(
Expand All @@ -369,7 +372,7 @@ fn get_src_or_dest_col(
)
.push(TextType::highlighted_subtitle_with_desc(
mac_address_translation(language),
mac,
mac_str,
font,
))
}
Expand Down
13 changes: 10 additions & 3 deletions src/gui/pages/overview_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 Down Expand Up @@ -42,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 @@ -422,8 +424,10 @@ fn lazy_col_info(
let filtered_bytes =
sniffer.runtime_data.tot_sent_bytes + sniffer.runtime_data.tot_received_bytes;
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);
let col_device_filters =
col_device_filters(language, font, &sniffer.filters, &sniffer.device, link_type);

let col_data_representation =
col_data_representation(language, font, sniffer.traffic_chart.chart_type);
Expand Down Expand Up @@ -512,6 +516,7 @@ fn col_device_filters(
font: Font,
filters: &Filters,
device: &MyDevice,
link_type: Linktype,
) -> Column<'static, Message, Renderer<StyleType>> {
#[cfg(not(target_os = "windows"))]
let adapter_info = &device.name;
Expand All @@ -520,11 +525,13 @@ 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()
.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
4 changes: 4 additions & 0 deletions src/gui/types/runtime_data.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Module defining the `RunTimeData` struct, useful to to generate chart and to display statistics about network traffic
//!
use pcap::Linktype;
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,
/// Total number of bytes (filtered and not filtered)
pub all_bytes: u128,
/// Total number of packets (filtered and not filtered)
Expand Down Expand Up @@ -38,6 +41,7 @@ impl RunTimeData {
/// Constructs a new `ChartsData` element.
pub fn new() -> Self {
RunTimeData {
link_type: Linktype::ETHERNET,
all_bytes: 0,
all_packets: 0,
tot_sent_bytes: 0,
Expand Down
1 change: 1 addition & 0 deletions src/gui/types/sniffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl Sniffer {
self.runtime_data.tot_received_bytes = info_traffic_lock.tot_received_bytes;
self.runtime_data.tot_sent_bytes = info_traffic_lock.tot_sent_bytes;
self.runtime_data.dropped_packets = info_traffic_lock.dropped_packets;
self.runtime_data.link_type = info_traffic_lock.link_type;
drop(info_traffic_lock);
let emitted_notifications = notify_and_log(
&mut self.runtime_data,
Expand Down
27 changes: 12 additions & 15 deletions src/networking/manage_packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@ use crate::{AppProtocol, InfoTraffic, IpVersion, Protocol};
/// Returns the relevant collected information.
pub fn analyze_headers(
headers: PacketHeaders,
mac_addresses: &mut (String, String),
mac_addresses: &mut (Option<String>, Option<String>),
exchanged_bytes: &mut u128,
icmp_type: &mut IcmpType,
packet_filters_fields: &mut PacketFiltersFields,
) -> Option<AddressPortPair> {
if !analyze_link_header(headers.link, &mut mac_addresses.0, &mut mac_addresses.1) {
return None;
}
analyze_link_header(headers.link, &mut mac_addresses.0, &mut mac_addresses.1);

if !analyze_network_header(
headers.ip,
Expand Down Expand Up @@ -71,16 +69,15 @@ pub fn analyze_headers(
/// Returns false if packet has to be skipped.
fn analyze_link_header(
link_header: Option<Ethernet2Header>,
mac_address1: &mut String,
mac_address2: &mut String,
) -> bool {
match link_header {
Some(header) => {
*mac_address1 = mac_from_dec_to_hex(header.source);
*mac_address2 = mac_from_dec_to_hex(header.destination);
true
}
_ => false,
mac_address1: &mut Option<String>,
mac_address2: &mut Option<String>,
) {
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 Expand Up @@ -167,7 +164,7 @@ pub fn modify_or_insert_in_map(
info_traffic_mutex: &Arc<Mutex<InfoTraffic>>,
key: &AddressPortPair,
my_device: &MyDevice,
mac_addresses: (String, String),
mac_addresses: (Option<String>, Option<String>),
icmp_type: IcmpType,
exchanged_bytes: u128,
application_protocol: AppProtocol,
Expand Down
4 changes: 2 additions & 2 deletions src/networking/types/info_address_port_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use crate::AppProtocol;
#[derive(Clone, Default)]
pub struct InfoAddressPortPair {
/// Source MAC address
pub mac_address1: String,
pub mac_address1: Option<String>,
/// Destination MAC address
pub mac_address2: String,
pub mac_address2: Option<String>,
/// Amount of bytes transmitted between the pair.
pub transmitted_bytes: u128,
/// Amount of packets transmitted between the pair.
Expand Down
4 changes: 4 additions & 0 deletions src/networking/types/info_traffic.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! 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;
Expand All @@ -13,6 +14,8 @@ 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,
/// Total amount of filtered bytes received.
pub tot_received_bytes: u128,
/// Total amount of filtered bytes sent.
Expand Down Expand Up @@ -47,6 +50,7 @@ impl InfoTraffic {
/// Constructs a new `InfoTraffic` element.
pub fn new() -> Self {
InfoTraffic {
link_type: Linktype::ETHERNET,
tot_received_bytes: 0,
tot_sent_bytes: 0,
tot_received_packets: 0,
Expand Down
Loading
Loading