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 9 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
21 changes: 16 additions & 5 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 All @@ -65,11 +67,13 @@ pub fn overview_page(sniffer: &Sniffer) -> Container<Message, Renderer<StyleType
sniffer.runtime_data.tot_sent_packets + sniffer.runtime_data.tot_received_packets;
let dropped = sniffer.runtime_data.dropped_packets;
let total = observed + u128::from(dropped);
let link_type = sniffer.runtime_data.link_type;

match (observed, filtered) {
(0, 0) => {
//no packets observed at all
body = body_no_packets(&sniffer.device, font, language, &sniffer.waiting);
body =
body_no_packets(&sniffer.device, font, language, &sniffer.waiting, link_type);
}
(observed, 0) => {
//no packets have been filtered but some have been observed
Expand Down Expand Up @@ -139,8 +143,10 @@ fn body_no_packets(
font: Font,
language: Language,
waiting: &str,
link_type: Linktype,
) -> Column<'static, Message, Renderer<StyleType>> {
let adapter_name = device.name.clone();
let mut adapter_name = device.name.clone();
adapter_name.push_str(&format!(" ({})", link_type.0,));
let (icon_text, nothing_to_see_text) = if device.addresses.lock().unwrap().is_empty() {
(
Icon::Warning.to_text().size(60),
Expand Down Expand Up @@ -422,8 +428,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 +520,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 +529,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 @@ -293,6 +293,7 @@ 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
29 changes: 13 additions & 16 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 Expand Up @@ -493,7 +490,7 @@ pub fn is_my_address(address_to_lookup: &String, my_interface_addresses: &Vec<Ad

/// Determines if the capture opening resolves into an Error
pub fn get_capture_result(device: &MyDevice) -> (Option<String>, Option<Capture<Active>>) {
let cap_result = Capture::from_device(&*device.name)
let cap_result = Capture::from_device(device.to_pcap_device())
.expect("Capture initialization error\n\r")
.promisc(true)
.snaplen(256) //limit stored packets slice dimension (to keep more in the buffer)
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
18 changes: 17 additions & 1 deletion src/networking/types/my_device.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex};

use pcap::Address;
use pcap::{Address, Device, DeviceFlags};

/// Represents the current inspected device.
/// Used to keep in sync the device addresses in case of changes
Expand All @@ -11,3 +11,19 @@ pub struct MyDevice {
pub desc: Option<String>,
pub addresses: Arc<Mutex<Vec<Address>>>,
}

impl MyDevice {
pub fn to_pcap_device(&self) -> Device {
for device in Device::list().unwrap_or_default() {
if device.name.eq(&self.name) {
return device;
}
}
Device::lookup().unwrap_or(None).unwrap_or(Device {
name: String::new(),
desc: None,
addresses: vec![],
flags: DeviceFlags::empty(),
})
}
}
Loading
Loading