1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
// Take a look at the license at the top of the repository in the LICENSE file.
use std::collections::{hash_map, HashMap};
use std::mem::MaybeUninit;
use super::utils;
use crate::{NetworkExt, NetworksExt, NetworksIter};
macro_rules! old_and_new {
($ty_:expr, $name:ident, $old:ident, $data:expr) => {{
$ty_.$old = $ty_.$name;
$ty_.$name = $data.$name;
}};
}
#[doc = include_str!("../../md_doc/networks.md")]
pub struct Networks {
interfaces: HashMap<String, NetworkData>,
}
impl Networks {
pub(crate) fn new() -> Networks {
Networks {
interfaces: HashMap::new(),
}
}
}
impl NetworksExt for Networks {
fn iter(&self) -> NetworksIter {
NetworksIter::new(self.interfaces.iter())
}
fn refresh_networks_list(&mut self) {
unsafe {
self.refresh_interfaces(true);
}
// Remove interfaces which are gone.
self.interfaces.retain(|_, n| n.updated);
}
fn refresh(&mut self) {
unsafe {
self.refresh_interfaces(false);
}
}
}
impl Networks {
unsafe fn refresh_interfaces(&mut self, refresh_all: bool) {
let mut nb_interfaces: libc::c_int = 0;
if !utils::get_sys_value(
&[
libc::CTL_NET,
libc::PF_LINK,
libc::NETLINK_GENERIC,
libc::IFMIB_SYSTEM,
libc::IFMIB_IFCOUNT,
],
&mut nb_interfaces,
) {
return;
}
if refresh_all {
// We don't need to update this value if we're not updating all interfaces.
for interface in self.interfaces.values_mut() {
interface.updated = false;
}
}
let mut data: libc::ifmibdata = MaybeUninit::zeroed().assume_init();
for row in 1..nb_interfaces {
let mib = [
libc::CTL_NET,
libc::PF_LINK,
libc::NETLINK_GENERIC,
libc::IFMIB_IFDATA,
row,
libc::IFDATA_GENERAL,
];
if !utils::get_sys_value(&mib, &mut data) {
continue;
}
if let Some(name) = utils::c_buf_to_string(&data.ifmd_name) {
let data = &data.ifmd_data;
match self.interfaces.entry(name) {
hash_map::Entry::Occupied(mut e) => {
let mut interface = e.get_mut();
old_and_new!(interface, ifi_ibytes, old_ifi_ibytes, data);
old_and_new!(interface, ifi_obytes, old_ifi_obytes, data);
old_and_new!(interface, ifi_ipackets, old_ifi_ipackets, data);
old_and_new!(interface, ifi_opackets, old_ifi_opackets, data);
old_and_new!(interface, ifi_ierrors, old_ifi_ierrors, data);
old_and_new!(interface, ifi_oerrors, old_ifi_oerrors, data);
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
if !refresh_all {
// This is simply a refresh, we don't want to add new interfaces!
continue;
}
e.insert(NetworkData {
ifi_ibytes: data.ifi_ibytes,
old_ifi_ibytes: 0,
ifi_obytes: data.ifi_obytes,
old_ifi_obytes: 0,
ifi_ipackets: data.ifi_ipackets,
old_ifi_ipackets: 0,
ifi_opackets: data.ifi_opackets,
old_ifi_opackets: 0,
ifi_ierrors: data.ifi_ierrors,
old_ifi_ierrors: 0,
ifi_oerrors: data.ifi_oerrors,
old_ifi_oerrors: 0,
updated: true,
});
}
}
}
}
}
}
#[doc = include_str!("../../md_doc/network_data.md")]
pub struct NetworkData {
/// Total number of bytes received over interface.
ifi_ibytes: u64,
old_ifi_ibytes: u64,
/// Total number of bytes transmitted over interface.
ifi_obytes: u64,
old_ifi_obytes: u64,
/// Total number of packets received.
ifi_ipackets: u64,
old_ifi_ipackets: u64,
/// Total number of packets transmitted.
ifi_opackets: u64,
old_ifi_opackets: u64,
/// Shows the total number of packets received with error. This includes
/// too-long-frames errors, ring-buffer overflow errors, CRC errors,
/// frame alignment errors, fifo overruns, and missed packets.
ifi_ierrors: u64,
old_ifi_ierrors: u64,
/// similar to `ifi_ierrors`
ifi_oerrors: u64,
old_ifi_oerrors: u64,
/// Whether or not the above data has been updated during refresh
updated: bool,
}
impl NetworkExt for NetworkData {
fn received(&self) -> u64 {
self.ifi_ibytes.saturating_sub(self.old_ifi_ibytes)
}
fn total_received(&self) -> u64 {
self.ifi_ibytes
}
fn transmitted(&self) -> u64 {
self.ifi_obytes.saturating_sub(self.old_ifi_obytes)
}
fn total_transmitted(&self) -> u64 {
self.ifi_obytes
}
fn packets_received(&self) -> u64 {
self.ifi_ipackets.saturating_sub(self.old_ifi_ipackets)
}
fn total_packets_received(&self) -> u64 {
self.ifi_ipackets
}
fn packets_transmitted(&self) -> u64 {
self.ifi_opackets.saturating_sub(self.old_ifi_opackets)
}
fn total_packets_transmitted(&self) -> u64 {
self.ifi_opackets
}
fn errors_on_received(&self) -> u64 {
self.ifi_ierrors.saturating_sub(self.old_ifi_ierrors)
}
fn total_errors_on_received(&self) -> u64 {
self.ifi_ierrors
}
fn errors_on_transmitted(&self) -> u64 {
self.ifi_oerrors.saturating_sub(self.old_ifi_oerrors)
}
fn total_errors_on_transmitted(&self) -> u64 {
self.ifi_oerrors
}
}
|