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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Take a look at the license at the top of the repository in the LICENSE file.
use libc::{
self, CTL_NET, IFNAMSIZ, NET_RT_IFLIST2, PF_ROUTE, RTM_IFINFO2, c_char, c_int, c_uint,
if_data64, if_msghdr2, sysctl,
};
use std::collections::{HashMap, hash_map};
use std::mem::{MaybeUninit, size_of};
use std::ptr::null_mut;
use crate::network::refresh_networks_addresses;
use crate::{IpNetwork, MacAddr, NetworkData};
// FIXME: To be removed once https://github.com/rust-lang/libc/pull/4022 is merged and released.
#[repr(C)]
struct ifmibdata {
ifmd_name: [c_char; IFNAMSIZ],
ifmd_pcount: c_uint,
ifmd_flags: c_uint,
ifmd_snd_len: c_uint,
ifmd_snd_maxlen: c_uint,
ifmd_snd_drops: c_uint,
ifmd_filler: [c_uint; 4],
ifmd_data: if_data64,
}
// FIXME: To be removed once https://github.com/rust-lang/libc/pull/4022 is merged and released.
pub const IFDATA_GENERAL: c_int = 1;
// FIXME: To be removed once https://github.com/rust-lang/libc/pull/4022 is merged and released.
pub const IFMIB_IFDATA: c_int = 2;
// FIXME: To be removed once https://github.com/rust-lang/libc/pull/4022 is merged and released.
pub const NETLINK_GENERIC: c_int = 0;
#[inline]
fn update_field(old_field: &mut u64, new_field: &mut u64, value: u64) {
*old_field = *new_field;
*new_field = value;
}
fn update_network_data(inner: &mut NetworkDataInner, data: &if_data64) {
update_field(&mut inner.old_out, &mut inner.current_out, data.ifi_obytes);
update_field(&mut inner.old_in, &mut inner.current_in, data.ifi_ibytes);
update_field(
&mut inner.old_packets_out,
&mut inner.packets_out,
data.ifi_opackets,
);
update_field(
&mut inner.old_packets_in,
&mut inner.packets_in,
data.ifi_ipackets,
);
update_field(
&mut inner.old_errors_in,
&mut inner.errors_in,
data.ifi_ierrors,
);
update_field(
&mut inner.old_errors_out,
&mut inner.errors_out,
data.ifi_oerrors,
);
}
pub(crate) struct NetworksInner {
pub(crate) interfaces: HashMap<String, NetworkData>,
}
impl NetworksInner {
pub(crate) fn new() -> Self {
Self {
interfaces: HashMap::new(),
}
}
pub(crate) fn list(&self) -> &HashMap<String, NetworkData> {
&self.interfaces
}
pub(crate) fn refresh(&mut self, remove_not_listed_interfaces: bool) {
self.update_networks();
if remove_not_listed_interfaces {
self.interfaces.retain(|_, i| {
if !i.inner.updated {
return false;
}
i.inner.updated = false;
true
});
}
refresh_networks_addresses(&mut self.interfaces);
}
#[allow(clippy::cast_ptr_alignment)]
#[allow(clippy::uninit_vec)]
fn update_networks(&mut self) {
let mib = &mut [CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0];
let mib2 = &mut [
CTL_NET,
libc::PF_LINK,
NETLINK_GENERIC,
IFMIB_IFDATA,
0,
IFDATA_GENERAL,
];
let mut len = 0;
unsafe {
if sysctl(
mib.as_mut_ptr(),
mib.len() as _,
null_mut(),
&mut len,
null_mut(),
0,
) < 0
{
// TODO: might be nice to put an error in here...
return;
}
let mut buf = Vec::with_capacity(len);
buf.set_len(len);
if sysctl(
mib.as_mut_ptr(),
mib.len() as _,
buf.as_mut_ptr(),
&mut len,
null_mut(),
0,
) < 0
{
// TODO: might be nice to put an error in here...
return;
}
let buf = buf.as_ptr() as *const c_char;
let lim = buf.add(len);
let mut next = buf;
while next < lim {
let ifm = next as *const libc::if_msghdr;
next = next.offset((*ifm).ifm_msglen as isize);
if (*ifm).ifm_type == RTM_IFINFO2 as u8 {
// The interface (line description) name stored at ifname will be returned in
// the default coded character set identifier (CCSID) currently in effect for
// the job. If this is not a single byte CCSID, then storage greater than
// IFNAMSIZ (16) bytes may be needed. 22 bytes is large enough for all CCSIDs.
let mut name = vec![0u8; libc::IFNAMSIZ + 6];
let if2m: *const if_msghdr2 = ifm as *const if_msghdr2;
let pname =
libc::if_indextoname((*if2m).ifm_index as _, name.as_mut_ptr() as _);
if pname.is_null() {
continue;
}
name.set_len(libc::strlen(pname));
let name = String::from_utf8_unchecked(name);
let mtu = (*if2m).ifm_data.ifi_mtu as u64;
// Because data size is capped at 32 bits with the previous sysctl call for some
// reasons, we need to make another sysctl call to get the actual values
// we originally got into `ifm.ifm_data`...
//
// Issue: https://github.com/GuillaumeGomez/sysinfo/issues/1378
let mut mib_data: MaybeUninit<ifmibdata> = MaybeUninit::uninit();
mib2[4] = (*if2m).ifm_index as _;
let ret = sysctl(
mib2.as_mut_ptr(),
mib2.len() as _,
mib_data.as_mut_ptr() as *mut _,
&mut size_of::<ifmibdata>(),
null_mut(),
0,
);
match self.interfaces.entry(name) {
hash_map::Entry::Occupied(mut e) => {
let interface = e.get_mut();
let interface = &mut interface.inner;
if ret < 0 {
sysinfo_debug!(
"Cannot get network interface data usage: sysctl failed: {ret}"
);
} else {
let data = mib_data.assume_init();
update_network_data(interface, &data.ifmd_data);
}
if interface.mtu != mtu {
interface.mtu = mtu
}
interface.updated = true;
}
hash_map::Entry::Vacant(e) => {
let current_in;
let current_out;
let packets_in;
let packets_out;
let errors_in;
let errors_out;
if ret < 0 {
sysinfo_debug!(
"Cannot get network interface data usage: sysctl failed: {ret}"
);
current_in = 0;
current_out = 0;
packets_in = 0;
packets_out = 0;
errors_in = 0;
errors_out = 0;
} else {
let data = mib_data.assume_init();
let data = data.ifmd_data;
current_in = data.ifi_ibytes;
current_out = data.ifi_obytes;
packets_in = data.ifi_ipackets;
packets_out = data.ifi_opackets;
errors_in = data.ifi_ierrors;
errors_out = data.ifi_oerrors;
}
e.insert(NetworkData {
inner: NetworkDataInner {
current_in,
old_in: current_in,
current_out,
old_out: current_out,
packets_in,
old_packets_in: packets_in,
packets_out,
old_packets_out: packets_out,
errors_in,
old_errors_in: errors_in,
errors_out,
old_errors_out: errors_out,
updated: true,
mac_addr: MacAddr::UNSPECIFIED,
ip_networks: vec![],
mtu,
},
});
}
}
}
}
}
}
}
#[derive(PartialEq, Eq)]
pub(crate) struct NetworkDataInner {
current_in: u64,
old_in: u64,
current_out: u64,
old_out: u64,
packets_in: u64,
old_packets_in: u64,
packets_out: u64,
old_packets_out: u64,
errors_in: u64,
old_errors_in: u64,
errors_out: u64,
old_errors_out: u64,
updated: bool,
/// MAC address
pub(crate) mac_addr: MacAddr,
/// IP networks
pub(crate) ip_networks: Vec<IpNetwork>,
/// Interface Maximum Transfer Unit (MTU)
mtu: u64,
}
impl NetworkDataInner {
pub(crate) fn received(&self) -> u64 {
self.current_in.saturating_sub(self.old_in)
}
pub(crate) fn total_received(&self) -> u64 {
self.current_in
}
pub(crate) fn transmitted(&self) -> u64 {
self.current_out.saturating_sub(self.old_out)
}
pub(crate) fn total_transmitted(&self) -> u64 {
self.current_out
}
pub(crate) fn packets_received(&self) -> u64 {
self.packets_in.saturating_sub(self.old_packets_in)
}
pub(crate) fn total_packets_received(&self) -> u64 {
self.packets_in
}
pub(crate) fn packets_transmitted(&self) -> u64 {
self.packets_out.saturating_sub(self.old_packets_out)
}
pub(crate) fn total_packets_transmitted(&self) -> u64 {
self.packets_out
}
pub(crate) fn errors_on_received(&self) -> u64 {
self.errors_in.saturating_sub(self.old_errors_in)
}
pub(crate) fn total_errors_on_received(&self) -> u64 {
self.errors_in
}
pub(crate) fn errors_on_transmitted(&self) -> u64 {
self.errors_out.saturating_sub(self.old_errors_out)
}
pub(crate) fn total_errors_on_transmitted(&self) -> u64 {
self.errors_out
}
pub(crate) fn mac_address(&self) -> MacAddr {
self.mac_addr
}
pub(crate) fn ip_networks(&self) -> &[IpNetwork] {
&self.ip_networks
}
pub(crate) fn mtu(&self) -> u64 {
self.mtu
}
}