Skip to content

Commit 907d001

Browse files
committed
new enum MessageType to unify ARP, ICMP, and IGMP message types
1 parent 12829e8 commit 907d001

7 files changed

Lines changed: 65 additions & 91 deletions

File tree

src/gui/pages/connection_details_page.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::utils::formatted_strings::{
4040
get_formatted_timestamp, get_socket_address, mac_from_dec_to_hex, pretty_print_message_types,
4141
};
4242
use crate::utils::types::icon::Icon;
43-
use crate::{Language, Protocol, Sniffer, StyleType};
43+
use crate::{Language, Sniffer, StyleType};
4444
use iced::alignment::Vertical;
4545
use iced::widget::scrollable::Direction;
4646
use iced::widget::tooltip::Position;
@@ -250,12 +250,7 @@ fn col_info<'a>(
250250
));
251251
}
252252

253-
let messages = match key.protocol {
254-
Protocol::Icmpv4 | Protocol::Icmpv6 => pretty_print_message_types(&val.icmp_types),
255-
Protocol::Arp => pretty_print_message_types(&val.arp_types),
256-
Protocol::Igmp => pretty_print_message_types(&val.igmp_types),
257-
_ => String::new(),
258-
};
253+
let messages = pretty_print_message_types(&val.message_types);
259254

260255
if !messages.is_empty() {
261256
ret_val = ret_val.push(

src/networking/ipfix/collect.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ fn ingest_flow_record(
236236
&[],
237237
mac_addresses,
238238
None,
239-
None,
240-
None,
241239
packets,
242240
bytes,
243241
ip_blacklist,

src/networking/manage_packets.rs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ use crate::networking::types::data_info_host::DataInfoHost;
1212
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
1313
use crate::networking::types::info_traffic::InfoTraffic;
1414
use crate::networking::types::ip_blacklist::IpBlacklist;
15+
use crate::networking::types::message_type::MessageType;
1516
use crate::networking::types::program::Program;
1617
use crate::networking::types::service::Service;
1718
use crate::networking::types::service_query::ServiceQuery;
1819
use crate::networking::types::traffic_direction::TrafficDirection;
1920
use crate::networking::types::traffic_type::TrafficType;
2021
use crate::utils::types::timestamp::Timestamp;
21-
use sniffnet_packet_parser::IcmpType;
22-
use sniffnet_packet_parser::{ArpType, IgmpType};
2322
use std::time::Instant;
2423

2524
include!(concat!(env!("OUT_DIR"), "/services.rs"));
@@ -76,15 +75,13 @@ pub fn get_service(
7675
}
7776

7877
/// Function to insert the source and destination of a packet into the map containing the analyzed traffic
79-
#[allow(clippy::too_many_arguments, clippy::similar_names)]
78+
#[allow(clippy::too_many_arguments)]
8079
pub fn modify_or_insert_in_map(
8180
info_traffic_msg: &mut InfoTraffic,
8281
key: &AddressPortPair,
8382
my_interface_addresses: &[Address],
8483
mac_addresses: (Option<[u8; 6]>, Option<[u8; 6]>),
85-
icmp_type: Option<IcmpType>,
86-
arp_type: Option<ArpType>,
87-
igmp_type: Option<IgmpType>,
84+
message_type: Option<MessageType>,
8885
packets: u128,
8986
bytes: u128,
9087
ip_blacklist: &IpBlacklist,
@@ -132,27 +129,9 @@ pub fn modify_or_insert_in_map(
132129
info.final_timestamp = final_ts;
133130
}
134131
info.final_instant = Instant::now();
135-
if key.protocol.is_icmp()
136-
&& let Some(icmp_type) = icmp_type
137-
{
138-
info.icmp_types
139-
.entry(icmp_type)
140-
.and_modify(|n| *n += 1)
141-
.or_insert(1);
142-
}
143-
if key.protocol.eq(&Protocol::Arp)
144-
&& let Some(arp_type) = arp_type
145-
{
146-
info.arp_types
147-
.entry(arp_type)
148-
.and_modify(|n| *n += 1)
149-
.or_insert(1);
150-
}
151-
if key.protocol.eq(&Protocol::Igmp)
152-
&& let Some(igmp_type) = igmp_type
153-
{
154-
info.igmp_types
155-
.entry(igmp_type)
132+
if let Some(message_type) = message_type {
133+
info.message_types
134+
.entry(message_type)
156135
.and_modify(|n| *n += 1)
157136
.or_insert(1);
158137
}
@@ -167,24 +146,8 @@ pub fn modify_or_insert_in_map(
167146
final_instant: Instant::now(),
168147
service,
169148
traffic_direction,
170-
icmp_types: if key.protocol.is_icmp()
171-
&& let Some(icmp_type) = icmp_type
172-
{
173-
HashMap::from([(icmp_type, 1)])
174-
} else {
175-
HashMap::new()
176-
},
177-
arp_types: if key.protocol.eq(&Protocol::Arp)
178-
&& let Some(arp_type) = arp_type
179-
{
180-
HashMap::from([(arp_type, 1)])
181-
} else {
182-
HashMap::new()
183-
},
184-
igmp_types: if key.protocol.eq(&Protocol::Igmp)
185-
&& let Some(igmp_type) = igmp_type
186-
{
187-
HashMap::from([(igmp_type, 1)])
149+
message_types: if let Some(message_type) = message_type {
150+
HashMap::from([(message_type, 1)])
188151
} else {
189152
HashMap::new()
190153
},

src/networking/parse_packets.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::networking::types::address_port_pair::AddressPortPair;
1111
use crate::networking::types::capture_context::{CaptureContext, CaptureSource, CaptureType};
1212
use crate::networking::types::info_traffic::InfoTraffic;
1313
use crate::networking::types::ip_blacklist::IpBlacklist;
14+
use crate::networking::types::message_type::MessageType;
1415
use crate::utils::error_logger::{ErrorLogger, Location};
1516
use crate::utils::types::timestamp::Timestamp;
1617
use async_channel::Sender;
@@ -21,11 +22,7 @@ use std::time::{Duration, Instant};
2122
use tokio::sync::broadcast::Receiver;
2223

2324
/// The calling thread enters a loop in which it waits for network packets
24-
#[allow(
25-
clippy::too_many_lines,
26-
clippy::too_many_arguments,
27-
clippy::similar_names
28-
)]
25+
#[allow(clippy::too_many_lines, clippy::too_many_arguments)]
2926
pub fn parse_packets(
3027
cap_id: usize,
3128
mut cs: CaptureSource,
@@ -138,9 +135,7 @@ pub fn parse_packets(
138135

139136
let bytes = parsed.bytes_count() as u128;
140137
let mac_addresses = (parsed.link_info.src_mac, parsed.link_info.dst_mac);
141-
let icmp_type = parsed.transport_info.icmp_type;
142-
let arp_type = parsed.net_info.arp_type;
143-
let igmp_type = parsed.transport_info.igmp_type;
138+
let message_type = MessageType::from_parsed_packet(&parsed);
144139

145140
let key = AddressPortPair::from_parsed_packet(&parsed);
146141

@@ -158,9 +153,7 @@ pub fn parse_packets(
158153
&key,
159154
cs.get_addresses(),
160155
mac_addresses,
161-
icmp_type,
162-
arp_type,
163-
igmp_type,
156+
message_type,
164157
1,
165158
bytes,
166159
ip_blacklist,

src/networking/types/info_address_port_pair.rs

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
use crate::Service;
55
use crate::networking::types::data_info::DataInfo;
66
use crate::networking::types::data_representation::DataRepr;
7+
use crate::networking::types::message_type::MessageType;
78
use crate::networking::types::program::Program;
89
use crate::networking::types::traffic_direction::TrafficDirection;
910
use crate::report::types::sort_type::SortType;
1011
use crate::utils::types::timestamp::Timestamp;
11-
use sniffnet_packet_parser::IcmpType;
12-
use sniffnet_packet_parser::{ArpType, IgmpType};
1312
use std::cmp::Ordering;
1413
use std::collections::HashMap;
1514
use std::time::Instant;
@@ -37,12 +36,8 @@ pub struct InfoAddressPortPair {
3736
pub service: Service,
3837
/// Determines if the connection is incoming or outgoing
3938
pub traffic_direction: TrafficDirection,
40-
/// Types of the ICMP messages exchanged, with the relative count (this is empty if not ICMP)
41-
pub icmp_types: HashMap<IcmpType, usize>,
42-
/// Types of the ARP operations, with the relative count (this is empty if not ARP)
43-
pub arp_types: HashMap<ArpType, usize>,
44-
/// Types of the IGMP messages exchanged, with the relative count (this is empty if not IGMP)
45-
pub igmp_types: HashMap<IgmpType, usize>,
39+
/// Types of the messages exchanged, with the relative count (this is empty for protocols without message types)
40+
pub message_types: HashMap<MessageType, usize>,
4641
/// Whether the remote address is blacklisted
4742
pub is_blacklisted: bool,
4843
/// The program associated to this pair
@@ -66,21 +61,9 @@ impl InfoAddressPortPair {
6661
self.service = other.service;
6762
self.is_blacklisted = other.is_blacklisted;
6863
self.traffic_direction = other.traffic_direction;
69-
for (icmp_type, count) in &other.icmp_types {
70-
self.icmp_types
71-
.entry(*icmp_type)
72-
.and_modify(|v| *v += count)
73-
.or_insert(*count);
74-
}
75-
for (arp_type, count) in &other.arp_types {
76-
self.arp_types
77-
.entry(*arp_type)
78-
.and_modify(|v| *v += count)
79-
.or_insert(*count);
80-
}
81-
for (igmp_type, count) in &other.igmp_types {
82-
self.igmp_types
83-
.entry(*igmp_type)
64+
for (message_type, count) in &other.message_types {
65+
self.message_types
66+
.entry(*message_type)
8467
.and_modify(|v| *v += count)
8568
.or_insert(*count);
8669
}
@@ -130,9 +113,7 @@ impl Default for InfoAddressPortPair {
130113
final_instant: Instant::now(),
131114
service: Service::default(),
132115
traffic_direction: TrafficDirection::default(),
133-
icmp_types: HashMap::new(),
134-
arp_types: HashMap::new(),
135-
igmp_types: HashMap::new(),
116+
message_types: HashMap::new(),
136117
is_blacklisted: false,
137118
program: Program::default(),
138119
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Module defining the `MessageType` enum,
2+
//! which unifies the message types of the protocols that carry them (ARP, ICMP, and IGMP).
3+
4+
use std::fmt::{Display, Formatter};
5+
6+
use sniffnet_packet_parser::{ArpType, IcmpType, IgmpType, ParsedPacket};
7+
8+
/// The message type carried by a packet, for the protocols that define one.
9+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
10+
pub enum MessageType {
11+
/// The ARP message type.
12+
Arp(ArpType),
13+
/// The ICMP message type.
14+
Icmp(IcmpType),
15+
/// The IGMP message type.
16+
Igmp(IgmpType),
17+
}
18+
19+
impl MessageType {
20+
/// Extracts the message type carried by a parsed packet, if any.
21+
pub fn from_parsed_packet(parsed: &ParsedPacket) -> Option<Self> {
22+
match parsed.transport_info.protocol {
23+
sniffnet_packet_parser::Protocol::Icmpv4 | sniffnet_packet_parser::Protocol::Icmpv6 => {
24+
parsed.transport_info.icmp_type.map(Self::Icmp)
25+
}
26+
sniffnet_packet_parser::Protocol::Igmp => {
27+
parsed.transport_info.igmp_type.map(Self::Igmp)
28+
}
29+
sniffnet_packet_parser::Protocol::Arp => parsed.net_info.arp_type.map(Self::Arp),
30+
_ => None,
31+
}
32+
}
33+
}
34+
35+
impl Display for MessageType {
36+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
37+
match self {
38+
MessageType::Arp(arp_type) => write!(f, "{arp_type}"),
39+
MessageType::Icmp(icmp_type) => write!(f, "{icmp_type}"),
40+
MessageType::Igmp(igmp_type) => write!(f, "{igmp_type}"),
41+
}
42+
}
43+
}

src/networking/types/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod info_traffic;
1313
pub mod ip_blacklist;
1414
pub mod ipfix_exporter;
1515
pub mod latency;
16+
pub mod message_type;
1617
pub mod my_device;
1718
pub mod program;
1819
pub mod program_lookup;

0 commit comments

Comments
 (0)