1mod monitor;
2mod nat;
3
4pub use monitor::{InterfaceEvent, InterfaceMonitor};
5pub use nat::{get_local_ip, get_public_ip, NatDetectionMethod, NatDetector};
6
7use crate::types::{IpNetwork, MacAddress};
8use std::time::Duration;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum InterfaceKind {
13 Loopback,
14 Ethernet,
15 Wireless,
16 Virtual,
17 Tunnel,
18 Other(String),
19}
20
21#[derive(Debug, Clone, Default)]
23pub struct InterfaceStats {
24 pub rx_bytes: u64,
25 pub tx_bytes: u64,
26 pub rx_packets: u64,
27 pub tx_packets: u64,
28 pub rx_errors: u64,
29 pub tx_errors: u64,
30 pub rx_dropped: u64,
31 pub tx_dropped: u64,
32 pub rx_fifo_errors: u64,
33 pub tx_fifo_errors: u64,
34 pub rx_frame_errors: u64,
35 pub tx_collisions: u64,
36 pub rx_compressed: u64,
37 pub tx_compressed: u64,
38 pub multicast: u64,
39 pub timestamp: Duration,
40}
41
42#[derive(Debug, Clone)]
44pub struct Interface {
45 pub name: String,
46 pub index: u32,
47 pub mac_address: Option<MacAddress>,
48 pub mtu: u32,
49 pub is_up: bool,
50 pub is_running: bool,
51 pub kind: InterfaceKind,
52 pub ipv4: Vec<IpNetwork>,
53 pub ipv6: Vec<IpNetwork>,
54 pub stats: InterfaceStats,
55}
56
57impl Interface {
58 pub fn list() -> crate::Result<Vec<Self>> {
63 #[cfg(unix)]
64 {
65 crate::platform::list_interfaces()
66 }
67 #[cfg(windows)]
68 {
69 crate::platform::list_interfaces()
70 }
71 }
72
73 #[must_use]
75 pub const fn interface_type(&self) -> &InterfaceKind {
76 &self.kind
77 }
78
79 #[must_use]
81 pub const fn is_up(&self) -> bool {
82 self.is_up
83 }
84
85 #[must_use]
87 pub fn ip_addresses(&self) -> Vec<String> {
88 let mut ips = Vec::new();
89 for ip in &self.ipv4 {
90 ips.push(ip.to_string());
91 }
92 for ip in &self.ipv6 {
93 ips.push(ip.to_string());
94 }
95 ips
96 }
97
98 #[must_use]
100 pub fn is_loopback(&self) -> bool {
101 self.kind == InterfaceKind::Loopback
102 }
103}