Skip to content

Commit bc6c507

Browse files
committed
simplify InfoTraffic struct: use DataInfo
1 parent ad5a7d6 commit bc6c507

7 files changed

Lines changed: 81 additions & 92 deletions

File tree

src/chart/manage_chart_data.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ impl TrafficChart {
1616
self.ticks += 1;
1717

1818
#[allow(clippy::cast_precision_loss)]
19-
let out_bytes_entry =
20-
-1.0 * (info_traffic.tot_out_bytes - info_traffic.tot_out_bytes_prev) as f32;
19+
let out_bytes_entry = -1.0
20+
* (info_traffic.tot_data_info.outgoing_bytes()
21+
- info_traffic.tot_data_info_prev.outgoing_bytes()) as f32;
2122
#[allow(clippy::cast_precision_loss)]
22-
let in_bytes_entry = (info_traffic.tot_in_bytes - info_traffic.tot_in_bytes_prev) as f32;
23+
let in_bytes_entry = (info_traffic.tot_data_info.incoming_bytes()
24+
- info_traffic.tot_data_info_prev.incoming_bytes()) as f32;
2325
#[allow(clippy::cast_precision_loss)]
24-
let out_packets_entry =
25-
-1.0 * (info_traffic.tot_out_packets - info_traffic.tot_out_packets_prev) as f32;
26+
let out_packets_entry = -1.0
27+
* (info_traffic.tot_data_info.outgoing_packets()
28+
- info_traffic.tot_data_info_prev.outgoing_packets()) as f32;
2629
#[allow(clippy::cast_precision_loss)]
27-
let in_packets_entry =
28-
(info_traffic.tot_in_packets - info_traffic.tot_in_packets_prev) as f32;
30+
let in_packets_entry = (info_traffic.tot_data_info.incoming_packets()
31+
- info_traffic.tot_data_info_prev.incoming_packets())
32+
as f32;
2933

3034
let out_bytes_point = (tot_seconds, out_bytes_entry);
3135
let in_bytes_point = (tot_seconds, in_bytes_entry);
@@ -163,6 +167,8 @@ mod tests {
163167
use splines::{Interpolation, Key, Spline};
164168

165169
use crate::chart::manage_chart_data::{ChartSeries, get_max, get_min};
170+
use crate::networking::types::data_info::DataInfo;
171+
use crate::networking::types::traffic_direction::TrafficDirection;
166172
use crate::utils::types::timestamp::Timestamp;
167173
use crate::{ChartType, InfoTraffic, Language, StyleType, TrafficChart};
168174

@@ -250,6 +256,14 @@ mod tests {
250256
};
251257
let tot_sent = 1000 * 28 + 500;
252258
let tot_received = 21000 * 28 + 1000;
259+
let tot_data_info_prev =
260+
DataInfo::new_for_tests(tot_received, tot_sent, tot_received, tot_sent);
261+
let tot_data_info = DataInfo::new_for_tests(
262+
tot_received + 4444,
263+
tot_sent + 3333,
264+
tot_received + 2222,
265+
tot_sent + 1111,
266+
);
253267
let mut traffic_chart = TrafficChart {
254268
ticks: 29,
255269
out_bytes: sent.clone(),
@@ -271,15 +285,9 @@ mod tests {
271285
let mut info_traffic = InfoTraffic {
272286
all_bytes: 0,
273287
all_packets: 0,
274-
tot_out_bytes: tot_sent + 1111,
275-
tot_in_bytes: tot_received + 2222,
276-
tot_out_packets: tot_sent + 3333,
277-
tot_in_packets: tot_received + 4444,
288+
tot_data_info,
278289
dropped_packets: 0,
279-
tot_out_bytes_prev: tot_sent,
280-
tot_in_bytes_prev: tot_received,
281-
tot_out_packets_prev: tot_sent,
282-
tot_in_packets_prev: tot_received,
290+
tot_data_info_prev,
283291
..Default::default()
284292
};
285293

@@ -292,10 +300,7 @@ mod tests {
292300
assert_eq!(get_max(&traffic_chart.in_bytes), 21000.0);
293301

294302
// prev values aren't updated here anymore: manually set them
295-
info_traffic.tot_out_bytes_prev = info_traffic.tot_out_bytes;
296-
info_traffic.tot_in_bytes_prev = info_traffic.tot_in_bytes;
297-
info_traffic.tot_out_packets_prev = info_traffic.tot_out_packets;
298-
info_traffic.tot_in_packets_prev = info_traffic.tot_in_packets;
303+
info_traffic.tot_data_info_prev = info_traffic.tot_data_info;
299304

300305
let mut sent_bytes = sent.clone();
301306
sent_bytes
@@ -337,17 +342,20 @@ mod tests {
337342
received_bytes.spline.keys()
338343
);
339344

340-
info_traffic.tot_out_bytes += 99;
341-
info_traffic.tot_in_packets += 990;
342-
info_traffic.tot_in_bytes += 2;
345+
info_traffic
346+
.tot_data_info
347+
.add_packets(990, 2, TrafficDirection::Incoming);
348+
info_traffic
349+
.tot_data_info
350+
.add_packet(99, TrafficDirection::Outgoing);
343351
traffic_chart.update_charts_data(&info_traffic, false);
344-
info_traffic.tot_out_bytes_prev = info_traffic.tot_out_bytes;
345-
info_traffic.tot_in_bytes_prev = info_traffic.tot_in_bytes;
346-
info_traffic.tot_out_packets_prev = info_traffic.tot_out_packets;
347-
info_traffic.tot_in_packets_prev = info_traffic.tot_in_packets;
348-
info_traffic.tot_out_bytes += 77;
349-
info_traffic.tot_in_packets += 1;
350-
info_traffic.tot_out_packets += 220;
352+
info_traffic.tot_data_info_prev = info_traffic.tot_data_info;
353+
info_traffic
354+
.tot_data_info
355+
.add_packet(0, TrafficDirection::Incoming);
356+
info_traffic
357+
.tot_data_info
358+
.add_packets(220, 77, TrafficDirection::Outgoing);
351359
traffic_chart.update_charts_data(&info_traffic, false);
352360

353361
sent_bytes.spline.remove(0);
@@ -370,7 +378,7 @@ mod tests {
370378
sent_packets.spline.remove(0);
371379
sent_packets
372380
.spline
373-
.add(Key::new(30.0, 0.0, Interpolation::Cosine));
381+
.add(Key::new(30.0, -1.0, Interpolation::Cosine));
374382
sent_packets
375383
.spline
376384
.add(Key::new(31.0, -220.0, Interpolation::Cosine));

src/gui/pages/overview_page.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn overview_page(sniffer: &Sniffer) -> Container<Message, StyleType> {
6969
} else {
7070
// NO pcap error detected
7171
let observed = sniffer.info_traffic.all_packets;
72-
let filtered = sniffer.info_traffic.tot_out_packets + sniffer.info_traffic.tot_in_packets;
72+
let filtered = sniffer.info_traffic.tot_data_info.tot_packets();
7373

7474
match (observed, filtered) {
7575
(0, 0) => {

src/gui/pages/thumbnail_page.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub fn thumbnail_page(sniffer: &Sniffer) -> Container<Message, StyleType> {
2727
let ConfigSettings { style, .. } = sniffer.configs.settings;
2828
let font = style.get_extension().font;
2929

30-
let filtered = sniffer.info_traffic.tot_out_packets + sniffer.info_traffic.tot_in_packets;
30+
let filtered = sniffer.info_traffic.tot_data_info.tot_packets();
3131

3232
if filtered == 0 {
3333
return Container::new(

src/gui/sniffer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ impl Sniffer {
690690
self.info_traffic.refresh(msg, &self.favorite_hosts);
691691
self.update_thresholds();
692692
let info_traffic = &self.info_traffic;
693-
if info_traffic.tot_in_packets + info_traffic.tot_out_packets == 0 {
693+
if info_traffic.tot_data_info.tot_packets() == 0 {
694694
return;
695695
}
696696
let emitted_notifications = notify_and_log(
@@ -988,7 +988,7 @@ impl Sniffer {
988988
true,
989989
) => {
990990
// Running with no overlays
991-
if self.info_traffic.tot_out_packets + self.info_traffic.tot_in_packets > 0 {
991+
if self.info_traffic.tot_data_info.tot_packets() > 0 {
992992
// Running with no overlays and some packets filtered
993993
self.running_page = if next {
994994
self.running_page.next()
@@ -1160,6 +1160,7 @@ mod tests {
11601160
use crate::gui::types::timing_events::TimingEvents;
11611161
use crate::networking::types::host::Host;
11621162
use crate::networking::types::info_traffic::InfoTrafficMessage;
1163+
use crate::networking::types::traffic_direction::TrafficDirection;
11631164
use crate::notifications::types::logged_notification::{
11641165
LoggedNotification, PacketsThresholdExceeded,
11651166
};
@@ -1696,7 +1697,10 @@ mod tests {
16961697
));
16971698
// Thresholds adjustments won't be updated if `info_traffic.tot_in_packets`
16981699
// and `info_traffic.tot_out_packets` are both `0`.
1699-
sniffer.info_traffic.tot_in_packets = 1;
1700+
sniffer
1701+
.info_traffic
1702+
.tot_data_info
1703+
.add_packet(0, TrafficDirection::Outgoing);
17001704

17011705
// Simulate a tick to apply the settings
17021706
sniffer.update(Message::TickRun(
@@ -1974,7 +1978,10 @@ mod tests {
19741978
assert_eq!(sniffer.running_page, RunningPage::Overview);
19751979
assert_eq!(sniffer.settings_page, None);
19761980
// switch with closed setting and some packets received => change running page
1977-
sniffer.info_traffic.tot_in_packets += 1;
1981+
sniffer
1982+
.info_traffic
1983+
.tot_data_info
1984+
.add_packet(0, TrafficDirection::Outgoing);
19781985
sniffer.update(Message::SwitchPage(true));
19791986
assert_eq!(sniffer.running_page, RunningPage::Inspect);
19801987
assert_eq!(sniffer.settings_page, None);

src/networking/parse_packets.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ pub fn parse_packets(
154154
exchanged_bytes,
155155
);
156156

157-
info_traffic_msg.add_packet(exchanged_bytes, traffic_direction);
157+
info_traffic_msg
158+
.tot_data_info
159+
.add_packet(exchanged_bytes, traffic_direction);
158160

159161
// check the rDNS status of this address and act accordingly
160162
let address_to_lookup = get_address_to_lookup(&key, traffic_direction);

src/networking/types/info_traffic.rs

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@ use crate::networking::types::data_info::DataInfo;
55
use crate::networking::types::data_info_host::DataInfoHost;
66
use crate::networking::types::host::Host;
77
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
8-
use crate::networking::types::traffic_direction::TrafficDirection;
98
use crate::utils::types::timestamp::Timestamp;
109
use std::collections::{HashMap, HashSet};
1110

1211
/// Struct containing overall traffic statistics and data.
1312
#[derive(Debug, Default)]
1413
pub struct InfoTraffic {
15-
/// Total amount of filtered bytes received.
16-
pub tot_in_bytes: u128,
17-
/// Total amount of filtered bytes sent.
18-
pub tot_out_bytes: u128,
19-
/// Total amount of filtered packets received.
20-
pub tot_in_packets: u128,
21-
/// Total amount of filtered packets sent.
22-
pub tot_out_packets: u128,
14+
/// Total amount of exchanged data
15+
pub tot_data_info: DataInfo,
2316
/// Total packets including those not filtered
2417
pub all_packets: u128,
2518
/// Total bytes including those not filtered
@@ -28,35 +21,24 @@ pub struct InfoTraffic {
2821
pub dropped_packets: u32,
2922
/// Timestamp of the latest parsed packet
3023
pub last_packet_timestamp: Timestamp,
31-
/// Total sent bytes filtered before the current time interval
32-
pub tot_out_bytes_prev: u128,
33-
/// Total received bytes filtered before the current time interval
34-
pub tot_in_bytes_prev: u128,
35-
/// Total sent packets filtered before the current time interval
36-
pub tot_out_packets_prev: u128,
37-
/// Total received packets filtered before the current time interval
38-
pub tot_in_packets_prev: u128,
3924
/// Map of the filtered traffic
4025
pub map: HashMap<AddressPortPair, InfoAddressPortPair>,
4126
/// Map of the upper layer services with their data info
4227
pub services: HashMap<Service, DataInfo>,
4328
/// Map of the hosts with their data info
4429
pub hosts: HashMap<Host, DataInfoHost>,
30+
/// Total amount of exchanged data before the current time interval
31+
pub tot_data_info_prev: DataInfo,
4532
/// Collection of favorite hosts that exchanged data in the last interval
4633
pub favorites_last_interval: HashSet<(Host, DataInfoHost)>,
4734
}
4835

4936
impl InfoTraffic {
5037
pub fn refresh(&mut self, msg: InfoTrafficMessage, favorites: &HashSet<Host>) {
51-
self.tot_out_bytes_prev = self.tot_out_bytes;
52-
self.tot_in_bytes_prev = self.tot_in_bytes;
53-
self.tot_out_packets_prev = self.tot_out_packets;
54-
self.tot_in_packets_prev = self.tot_in_packets;
38+
self.tot_data_info_prev = self.tot_data_info;
39+
40+
self.tot_data_info.refresh(msg.tot_data_info);
5541

56-
self.tot_in_bytes += msg.tot_in_bytes;
57-
self.tot_out_bytes += msg.tot_out_bytes;
58-
self.tot_in_packets += msg.tot_in_packets;
59-
self.tot_out_packets += msg.tot_out_packets;
6042
self.all_packets += msg.all_packets;
6143
self.all_bytes += msg.all_bytes;
6244
self.dropped_packets = msg.dropped_packets;
@@ -100,17 +82,21 @@ impl InfoTraffic {
10082
pub fn get_thumbnail_data(&self, chart_type: ChartType) -> (u128, u128, u128, u128) {
10183
if chart_type.eq(&ChartType::Bytes) {
10284
(
103-
self.tot_in_bytes,
104-
self.tot_out_bytes,
105-
self.all_bytes - self.tot_out_bytes - self.tot_in_bytes,
85+
self.tot_data_info.incoming_bytes(),
86+
self.tot_data_info.outgoing_bytes(),
87+
self.all_bytes
88+
- self.tot_data_info.outgoing_bytes()
89+
- self.tot_data_info.incoming_bytes(),
10690
// assume that the dropped packets have the same size as the average packet
10791
u128::from(self.dropped_packets) * self.all_bytes / self.all_packets,
10892
)
10993
} else {
11094
(
111-
self.tot_in_packets,
112-
self.tot_out_packets,
113-
self.all_packets - self.tot_out_packets - self.tot_in_packets,
95+
self.tot_data_info.incoming_packets(),
96+
self.tot_data_info.outgoing_packets(),
97+
self.all_packets
98+
- self.tot_data_info.outgoing_packets()
99+
- self.tot_data_info.incoming_packets(),
114100
u128::from(self.dropped_packets),
115101
)
116102
}
@@ -120,14 +106,8 @@ impl InfoTraffic {
120106
/// Struct containing traffic statistics and data related to the last time interval.
121107
#[derive(Debug, Clone, Default)]
122108
pub struct InfoTrafficMessage {
123-
/// Total amount of filtered bytes received.
124-
pub tot_in_bytes: u128,
125-
/// Total amount of filtered bytes sent.
126-
pub tot_out_bytes: u128,
127-
/// Total amount of filtered packets received.
128-
pub tot_in_packets: u128,
129-
/// Total amount of filtered packets sent.
130-
pub tot_out_packets: u128,
109+
/// Total amount of exchanged data
110+
pub tot_data_info: DataInfo,
131111
/// Total packets including those not filtered
132112
pub all_packets: u128,
133113
/// Total bytes including those not filtered
@@ -145,18 +125,6 @@ pub struct InfoTrafficMessage {
145125
}
146126

147127
impl InfoTrafficMessage {
148-
pub fn add_packet(&mut self, bytes: u128, traffic_direction: TrafficDirection) {
149-
if traffic_direction == TrafficDirection::Outgoing {
150-
//increment number of sent packets and bytes
151-
self.tot_out_packets += 1;
152-
self.tot_out_bytes += bytes;
153-
} else {
154-
//increment number of received packets and bytes
155-
self.tot_in_packets += 1;
156-
self.tot_in_bytes += bytes;
157-
}
158-
}
159-
160128
pub fn take_but_leave_something(&mut self) -> Self {
161129
let info_traffic = Self {
162130
last_packet_timestamp: self.last_packet_timestamp,

src/notifications/notify_and_log.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ pub fn notify_and_log(
2222
let timestamp = info_traffic.last_packet_timestamp;
2323
// packets threshold
2424
if let Some(threshold) = notifications.packets_notification.threshold {
25-
let sent_packets_entry = info_traffic.tot_out_packets - info_traffic.tot_out_packets_prev;
26-
let received_packets_entry = info_traffic.tot_in_packets - info_traffic.tot_in_packets_prev;
25+
let sent_packets_entry = info_traffic.tot_data_info.outgoing_packets()
26+
- info_traffic.tot_data_info_prev.outgoing_packets();
27+
let received_packets_entry = info_traffic.tot_data_info.incoming_packets()
28+
- info_traffic.tot_data_info_prev.incoming_packets();
2729
if received_packets_entry + sent_packets_entry > u128::from(threshold) {
2830
// log this notification
2931
emitted_notifications += 1;
@@ -45,8 +47,10 @@ pub fn notify_and_log(
4547
}
4648
// bytes threshold
4749
if let Some(threshold) = notifications.bytes_notification.threshold {
48-
let sent_bytes_entry = info_traffic.tot_out_bytes - info_traffic.tot_out_bytes_prev;
49-
let received_bytes_entry = info_traffic.tot_in_bytes - info_traffic.tot_in_bytes_prev;
50+
let sent_bytes_entry = info_traffic.tot_data_info.outgoing_bytes()
51+
- info_traffic.tot_data_info_prev.outgoing_bytes();
52+
let received_bytes_entry = info_traffic.tot_data_info.incoming_bytes()
53+
- info_traffic.tot_data_info_prev.incoming_bytes();
5054
if received_bytes_entry + sent_bytes_entry > u128::from(threshold) {
5155
//log this notification
5256
emitted_notifications += 1;

0 commit comments

Comments
 (0)