sysinfo/common/network.rs
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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
// Take a look at the license at the top of the repository in the LICENSE file.
use std::collections::HashMap;
use std::fmt;
use std::net::IpAddr;
use crate::{NetworkDataInner, NetworksInner};
/// Interacting with network interfaces.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("[{interface_name}]: {network:?}");
/// }
/// ```
pub struct Networks {
pub(crate) inner: NetworksInner,
}
impl<'a> IntoIterator for &'a Networks {
type Item = (&'a String, &'a NetworkData);
type IntoIter = std::collections::hash_map::Iter<'a, String, NetworkData>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl Default for Networks {
fn default() -> Self {
Networks::new()
}
}
impl Networks {
/// Creates a new empty [`Networks`][crate::Networks] type.
///
/// If you want it to be filled directly, take a look at [`Networks::new_with_refreshed_list`].
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// for (interface_name, network) in &networks {
/// println!("[{interface_name}]: {network:?}");
/// }
/// ```
pub fn new() -> Self {
Self {
inner: NetworksInner::new(),
}
}
/// Creates a new [`Networks`][crate::Networks] type with the network interfaces
/// list loaded. It is a combination of [`Networks::new`] and
/// [`Networks::refresh_list`].
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for network in &networks {
/// println!("{network:?}");
/// }
/// ```
pub fn new_with_refreshed_list() -> Self {
let mut networks = Self::new();
networks.refresh_list();
networks
}
/// Returns the network interfaces map.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for network in networks.list() {
/// println!("{network:?}");
/// }
/// ```
pub fn list(&self) -> &HashMap<String, NetworkData> {
self.inner.list()
}
/// Refreshes the network interfaces list.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// ```
pub fn refresh_list(&mut self) {
self.inner.refresh_list()
}
/// Refreshes the network interfaces' content. If you didn't run [`Networks::refresh_list`]
/// before, calling this method won't do anything as no interfaces are present.
///
/// ⚠️ If a network interface is added or removed, this method won't take it into account. Use
/// [`Networks::refresh_list`] instead.
///
/// ⚠️ If you didn't call [`Networks::refresh_list`] beforehand, this method will do nothing
/// as the network list will be empty.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Wait some time...? Then refresh the data of each network.
/// networks.refresh();
/// ```
pub fn refresh(&mut self) {
self.inner.refresh()
}
}
impl std::ops::Deref for Networks {
type Target = HashMap<String, NetworkData>;
fn deref(&self) -> &Self::Target {
self.list()
}
}
/// Getting volume of received and transmitted data.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("[{interface_name}] {network:?}");
/// }
/// ```
pub struct NetworkData {
pub(crate) inner: NetworkDataInner,
}
impl NetworkData {
/// Returns the number of received bytes since the last refresh.
///
/// If you want the total number of bytes received, take a look at the
/// [`total_received`](NetworkData::total_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("in: {} B", network.received());
/// }
/// ```
pub fn received(&self) -> u64 {
self.inner.received()
}
/// Returns the total number of received bytes.
///
/// If you want the amount of received bytes since the last refresh, take a look at the
/// [`received`](NetworkData::received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {} B", network.total_received());
/// }
/// ```
pub fn total_received(&self) -> u64 {
self.inner.total_received()
}
/// Returns the number of transmitted bytes since the last refresh.
///
/// If you want the total number of bytes transmitted, take a look at the
/// [`total_transmitted`](NetworkData::total_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("out: {} B", network.transmitted());
/// }
/// ```
pub fn transmitted(&self) -> u64 {
self.inner.transmitted()
}
/// Returns the total number of transmitted bytes.
///
/// If you want the amount of transmitted bytes since the last refresh, take a look at the
/// [`transmitted`](NetworkData::transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {} B", network.total_transmitted());
/// }
/// ```
pub fn total_transmitted(&self) -> u64 {
self.inner.total_transmitted()
}
/// Returns the number of incoming packets since the last refresh.
///
/// If you want the total number of packets received, take a look at the
/// [`total_packets_received`](NetworkData::total_packets_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.packets_received());
/// }
/// ```
pub fn packets_received(&self) -> u64 {
self.inner.packets_received()
}
/// Returns the total number of incoming packets.
///
/// If you want the amount of received packets since the last refresh, take a look at the
/// [`packets_received`](NetworkData::packets_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.total_packets_received());
/// }
/// ```
pub fn total_packets_received(&self) -> u64 {
self.inner.total_packets_received()
}
/// Returns the number of outcoming packets since the last refresh.
///
/// If you want the total number of packets transmitted, take a look at the
/// [`total_packets_transmitted`](NetworkData::total_packets_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.packets_transmitted());
/// }
/// ```
pub fn packets_transmitted(&self) -> u64 {
self.inner.packets_transmitted()
}
/// Returns the total number of outcoming packets.
///
/// If you want the amount of transmitted packets since the last refresh, take a look at the
/// [`packets_transmitted`](NetworkData::packets_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.total_packets_transmitted());
/// }
/// ```
pub fn total_packets_transmitted(&self) -> u64 {
self.inner.total_packets_transmitted()
}
/// Returns the number of incoming errors since the last refresh.
///
/// If you want the total number of errors on received packets, take a look at the
/// [`total_errors_on_received`](NetworkData::total_errors_on_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.errors_on_received());
/// }
/// ```
pub fn errors_on_received(&self) -> u64 {
self.inner.errors_on_received()
}
/// Returns the total number of incoming errors.
///
/// If you want the amount of errors on received packets since the last refresh, take a look at
/// the [`errors_on_received`](NetworkData::errors_on_received) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("in: {}", network.total_errors_on_received());
/// }
/// ```
pub fn total_errors_on_received(&self) -> u64 {
self.inner.total_errors_on_received()
}
/// Returns the number of outcoming errors since the last refresh.
///
/// If you want the total number of errors on transmitted packets, take a look at the
/// [`total_errors_on_transmitted`](NetworkData::total_errors_on_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
/// use std::{thread, time};
///
/// let mut networks = Networks::new_with_refreshed_list();
/// // Waiting a bit to get data from network...
/// thread::sleep(time::Duration::from_millis(10));
/// // Refreshing again to generate diff.
/// networks.refresh();
///
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.errors_on_transmitted());
/// }
/// ```
pub fn errors_on_transmitted(&self) -> u64 {
self.inner.errors_on_transmitted()
}
/// Returns the total number of outcoming errors.
///
/// If you want the amount of errors on transmitted packets since the last refresh, take a look at
/// the [`errors_on_transmitted`](NetworkData::errors_on_transmitted) method.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("out: {}", network.total_errors_on_transmitted());
/// }
/// ```
pub fn total_errors_on_transmitted(&self) -> u64 {
self.inner.total_errors_on_transmitted()
}
/// Returns the MAC address associated to current interface.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("MAC address: {}", network.mac_address());
/// }
/// ```
pub fn mac_address(&self) -> MacAddr {
self.inner.mac_address()
}
/// Returns the Ip Networks associated to current interface.
///
/// ```no_run
/// use sysinfo::Networks;
///
/// let mut networks = Networks::new_with_refreshed_list();
/// for (interface_name, network) in &networks {
/// println!("Ip Networks: {:?}", network.ip_networks());
/// }
/// ```
pub fn ip_networks(&self) -> &[IpNetwork] {
self.inner.ip_networks()
}
}
/// MAC address for network interface.
///
/// It is returned by [`NetworkData::mac_address`][crate::NetworkData::mac_address].
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct MacAddr(pub [u8; 6]);
impl MacAddr {
/// A `MacAddr` with all bytes set to `0`.
pub const UNSPECIFIED: Self = MacAddr([0; 6]);
/// Checks if this `MacAddr` has all bytes equal to `0`.
pub fn is_unspecified(&self) -> bool {
self == &MacAddr::UNSPECIFIED
}
}
impl fmt::Display for MacAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let data = &self.0;
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
data[0], data[1], data[2], data[3], data[4], data[5],
)
}
}
/// Ip networks address for network interface.
///
/// It is returned by [`NetworkData::ip_networks`][crate::NetworkData::ip_networks].
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct IpNetwork {
/// The ip of the network interface
pub addr: IpAddr,
/// The netmask, prefix of the ipaddress
pub prefix: u8,
}
impl fmt::Display for IpNetwork {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.addr, self.prefix)
}
}
#[cfg(test)]
mod tests {
use crate::*;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
// Ensure that the `Display` and `Debug` traits are implemented on the `MacAddr` struct
#[test]
fn check_display_impl_mac_address() {
println!(
"{} {:?}",
MacAddr([0x1, 0x2, 0x3, 0x4, 0x5, 0x6]),
MacAddr([0xa, 0xb, 0xc, 0xd, 0xe, 0xf])
);
}
#[test]
fn check_mac_address_is_unspecified_true() {
assert!(MacAddr::UNSPECIFIED.is_unspecified());
assert!(MacAddr([0; 6]).is_unspecified());
}
#[test]
fn check_mac_address_is_unspecified_false() {
assert!(!MacAddr([1, 2, 3, 4, 5, 6]).is_unspecified());
}
// Ensure that the `Display` and `Debug` traits are implemented on the `IpNetwork` struct
#[test]
fn check_display_impl_ip_network_ipv4() {
println!(
"{} {:?}",
IpNetwork {
addr: IpAddr::from(Ipv4Addr::new(1, 2, 3, 4)),
prefix: 3
},
IpNetwork {
addr: IpAddr::from(Ipv4Addr::new(255, 255, 255, 0)),
prefix: 21
}
);
}
#[test]
fn check_display_impl_ip_network_ipv6() {
println!(
"{} {:?}",
IpNetwork {
addr: IpAddr::from(Ipv6Addr::new(0xffff, 0xaabb, 00, 0, 0, 0x000c, 11, 21)),
prefix: 127
},
IpNetwork {
addr: IpAddr::from(Ipv6Addr::new(0xffcc, 0, 0, 0xffcc, 0, 0xffff, 0, 0xccaa)),
prefix: 120
}
)
}
#[test]
fn check_ip_networks() {
if !IS_SUPPORTED_SYSTEM {
return;
}
let networks = Networks::new_with_refreshed_list();
if networks.iter().any(|(_, n)| !n.ip_networks().is_empty()) {
return;
}
panic!("Networks should have at least one IP network ");
}
}