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
#[cfg(feature = "gateway")]
mod shared;
#[cfg(feature = "gateway")]
pub use self::shared::*;
mod types;
pub use self::types::*;
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "ios",
target_os = "android"
))]
mod unix;
#[cfg(any(
target_os = "linux",
target_os = "macos",
target_os = "openbsd",
target_os = "freebsd",
target_os = "netbsd",
target_os = "ios",
target_os = "android"
))]
use self::unix::*;
#[cfg(target_os = "windows")]
mod windows;
#[cfg(target_os = "windows")]
use self::windows::*;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[cfg(any(target_os = "linux", target_os = "android"))]
mod linux;
#[cfg(target_os = "android")]
mod android;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod macos;
#[cfg(feature = "gateway")]
use crate::device::NetworkDevice;
use crate::ipnet::{Ipv4Net, Ipv6Net};
use crate::mac::MacAddr;
use crate::sys;
#[cfg(feature = "gateway")]
use std::net::IpAddr;
/// Structure of Network Interface information
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Interface {
/// Index of network interface. This is an integer which uniquely identifies the interface
/// on this machine.
pub index: u32,
/// Machine-readable name of the network interface. On unix-like OSs, this is the interface
/// name, like 'eth0' or 'eno1'. On Windows, this is the interface's GUID as a string.
pub name: String,
/// Friendly name of network interface. On Windows, this is the network adapter configured
/// name, e.g. "Ethernet 5" or "Wi-Fi". On Mac, this is the interface display name,
/// such as "Ethernet" or "FireWire". If no friendly name is available, this is left as None.
pub friendly_name: Option<String>,
/// Description of the network interface. On Windows, this is the network adapter model, such
/// as "Realtek USB GbE Family Controller #4" or "Software Loopback Interface 1". Currently
/// this is not available on platforms other than Windows.
pub description: Option<String>,
/// Interface Type
pub if_type: InterfaceType,
/// MAC address of network interface
pub mac_addr: Option<MacAddr>,
/// List of Ipv4Nets (IPv4 address + netmask) for the network interface
pub ipv4: Vec<Ipv4Net>,
/// List of Ipv6Nets (IPv6 address + netmask) for the network interface
pub ipv6: Vec<Ipv6Net>,
/// List of IPv6 Scope IDs for each of the corresponding elements in the ipv6 address vector.
/// The Scope ID is an integer which uniquely identifies this interface address on the system,
/// and must be provided when using link-local addressing to specify which interface
/// you wish to use. The scope ID can be the same as the interface index, but is not
/// required to be by the standard.
/// The scope ID can also be referred to as the zone index.
pub ipv6_scope_ids: Vec<u32>,
/// Flags for the network interface (OS Specific)
pub flags: u32,
/// Speed in bits per second of the transmit for the network interface, if known.
/// Currently only supported on Linux, Android, and Windows.
pub transmit_speed: Option<u64>,
/// Speed in bits per second of the receive for the network interface.
/// Currently only supported on Linux, Android, and Windows.
pub receive_speed: Option<u64>,
/// Default gateway for the network interface. This is the address of the router to which
/// IP packets are forwarded when they need to be sent to a device outside
/// of the local network.
#[cfg(feature = "gateway")]
pub gateway: Option<NetworkDevice>,
/// DNS server addresses for the network interface
#[cfg(feature = "gateway")]
pub dns_servers: Vec<IpAddr>,
/// Maximum Transmission Unit (MTU) for the network interface
pub mtu: Option<u32>,
/// Whether this is the default interface for accessing the Internet.
#[cfg(feature = "gateway")]
pub default: bool,
}
impl Interface {
/// Construct a new default Interface instance
#[cfg(feature = "gateway")]
pub fn default() -> Result<Interface, String> {
let interfaces: Vec<Interface> = interfaces();
for iface in &interfaces {
if iface.default {
return Ok(iface.clone());
}
}
let local_ip: IpAddr = match get_local_ipaddr() {
Some(local_ip) => local_ip,
None => return Err(String::from("Local IP address not found")),
};
for iface in interfaces {
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
return Ok(iface);
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
return Ok(iface);
}
}
}
}
Err(String::from("Default Interface not found"))
}
// Construct a dummy Interface instance
pub fn dummy() -> Interface {
Interface {
index: 0,
name: String::new(),
friendly_name: None,
description: None,
if_type: InterfaceType::Unknown,
mac_addr: None,
ipv4: Vec::new(),
ipv6: Vec::new(),
ipv6_scope_ids: Vec::new(),
flags: 0,
transmit_speed: None,
receive_speed: None,
#[cfg(feature = "gateway")]
gateway: None,
#[cfg(feature = "gateway")]
dns_servers: Vec::new(),
mtu: None,
#[cfg(feature = "gateway")]
default: false,
}
}
/// Check if the network interface is up
pub fn is_up(&self) -> bool {
self.flags & (sys::IFF_UP as u32) != 0
}
/// Check if the network interface is a Loopback interface
pub fn is_loopback(&self) -> bool {
self.flags & (sys::IFF_LOOPBACK as u32) != 0
}
/// Check if the network interface is a Point-to-Point interface
pub fn is_point_to_point(&self) -> bool {
self.flags & (sys::IFF_POINTOPOINT as u32) != 0
}
/// Check if the network interface is a Multicast interface
pub fn is_multicast(&self) -> bool {
self.flags & (sys::IFF_MULTICAST as u32) != 0
}
/// Check if the network interface is a Broadcast interface
pub fn is_broadcast(&self) -> bool {
self.flags & (sys::IFF_BROADCAST as u32) != 0
}
/// Check if the network interface is a TUN interface
pub fn is_tun(&self) -> bool {
self.is_up() && self.is_point_to_point() && !self.is_broadcast() && !self.is_loopback()
}
/// Check if the network interface is running and ready to send/receive packets
pub fn is_running(&self) -> bool {
is_running(&self)
}
/// Check if the network interface is a physical interface
pub fn is_physical(&self) -> bool {
is_physical_interface(&self)
&& !crate::db::oui::is_virtual_mac(&self.mac_addr.unwrap_or(MacAddr::zero()))
&& !crate::db::oui::is_known_loopback_mac(&self.mac_addr.unwrap_or(MacAddr::zero()))
}
}
/// Get default Network Interface
#[cfg(feature = "gateway")]
pub fn get_default_interface() -> Result<Interface, String> {
let interfaces: Vec<Interface> = interfaces();
for iface in &interfaces {
if iface.default {
return Ok(iface.clone());
}
}
let local_ip: IpAddr = match get_local_ipaddr() {
Some(local_ip) => local_ip,
None => return Err(String::from("Local IP address not found")),
};
for iface in interfaces {
match local_ip {
IpAddr::V4(local_ipv4) => {
if iface.ipv4.iter().any(|x| x.addr() == local_ipv4) {
return Ok(iface);
}
}
IpAddr::V6(local_ipv6) => {
if iface.ipv6.iter().any(|x| x.addr() == local_ipv6) {
return Ok(iface);
}
}
}
}
Err(String::from("Default Interface not found"))
}
/// Get a list of available Network Interfaces
pub fn get_interfaces() -> Vec<Interface> {
interfaces()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_interfaces() {
let interfaces = get_interfaces();
for interface in interfaces {
println!("{:#?}", interface);
}
}
#[test]
#[cfg(feature = "gateway")]
fn test_default_interface() {
println!("{:#?}", get_default_interface());
}
#[test]
fn sanity_check_loopback() {
let interfaces = get_interfaces();
assert!(interfaces.len() >= 2, "There should be at least 2 network interfaces on any machine, the loopback and one other one");
// Try and find the loopback interface
let loopback_interfaces: Vec<&Interface> = interfaces
.iter()
.filter(|iface| iface.if_type == InterfaceType::Loopback)
.collect();
assert_eq!(
loopback_interfaces.len(),
1,
"There should be exactly one loopback interface on the machine"
);
let loopback = loopback_interfaces[0];
// Make sure that 127.0.0.1 is one of loopback's IPv4 addresses
let loopback_expected_ipv4: std::net::Ipv4Addr = "127.0.0.1".parse().unwrap();
let matching_ipv4s: Vec<&Ipv4Net> = loopback
.ipv4
.iter()
.filter(|&ipv4_net| ipv4_net.addr() == loopback_expected_ipv4)
.collect();
assert_eq!(
matching_ipv4s.len(),
1,
"The loopback interface should have IP 127.0.0.1"
);
println!("Found IP {:?} on the loopback interface", matching_ipv4s[0]);
// Make sure that ::1 is one of loopback's IPv6 addresses
let loopback_expected_ipv6: std::net::Ipv6Addr = "::1".parse().unwrap();
let matching_ipv6s: Vec<&Ipv6Net> = loopback
.ipv6
.iter()
.filter(|&ipv6_net| ipv6_net.addr() == loopback_expected_ipv6)
.collect();
assert_eq!(
matching_ipv6s.len(),
1,
"The loopback interface should have IP ::1"
);
println!("Found IP {:?} on the loopback interface", matching_ipv6s[0]);
// Make sure that the loopback interface has the same number of scope IDs as it does IPv6 addresses
assert_eq!(loopback.ipv6.len(), loopback.ipv6_scope_ids.len());
// Check flags
assert!(
loopback.is_running(),
"Loopback interface should be running!"
);
assert!(
!loopback.is_physical(),
"Loopback interface should not be physical!"
);
// Make sure that, if the loopback interface has a MAC, it has a known loopback MAC
match loopback.mac_addr {
Some(mac) => assert!(
crate::db::oui::is_known_loopback_mac(&mac),
"Loopback interface MAC not a known loopback MAC"
),
None => {}
}
}
}