Skip to content

Commit 51be429

Browse files
feat(netwatch): expose local addresses on the netmon state
1 parent 59a6c29 commit 51be429

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

netwatch/src/interfaces.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use self::linux::default_route;
3131
use self::windows::default_route;
3232
#[cfg(not(wasm_browser))]
3333
use crate::ip::is_link_local;
34-
use crate::ip::{is_private_v6, is_up};
34+
use crate::ip::{is_private_v6, is_up, LocalAddresses};
3535
#[cfg(not(wasm_browser))]
3636
use crate::netmon::is_interesting_interface;
3737

@@ -161,6 +161,8 @@ impl IpNet {
161161
pub struct State {
162162
/// Maps from an interface name interface.
163163
pub interfaces: HashMap<String, Interface>,
164+
/// List of machine's local IP addresses.
165+
pub local_addresses: LocalAddresses,
164166

165167
/// Whether this machine has an IPv6 Global or Unique Local Address
166168
/// which might provide connectivity.
@@ -212,6 +214,8 @@ impl State {
212214
let mut have_v4 = false;
213215

214216
let ifaces = netdev::interface::get_interfaces();
217+
let local_addresses = LocalAddresses::from_raw_interfaces(&ifaces);
218+
215219
for iface in ifaces {
216220
let ni = Interface { iface };
217221
let if_up = ni.is_up();
@@ -235,6 +239,7 @@ impl State {
235239

236240
State {
237241
interfaces,
242+
local_addresses,
238243
have_v4,
239244
have_v6,
240245
is_expensive: false,
@@ -250,6 +255,7 @@ impl State {
250255
let ifname = fake.iface.name.clone();
251256
Self {
252257
interfaces: [(ifname.clone(), fake)].into_iter().collect(),
258+
local_addresses: LocalAddresses::from_raw_interfaces(&[]),
253259
have_v6: true,
254260
have_v4: true,
255261
is_expensive: false,

netwatch/src/ip.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,22 @@ impl LocalAddresses {
3333
/// addresses because we know of environments where these are used with NAT to provide connectivity.
3434
pub fn new() -> Self {
3535
let ifaces = netdev::interface::get_interfaces();
36+
Self::from_raw_interfaces(&ifaces)
37+
}
3638

39+
pub(crate) fn from_raw_interfaces(ifaces: &[netdev::Interface]) -> Self {
3740
let mut loopback = Vec::new();
3841
let mut regular4 = Vec::new();
3942
let mut regular6 = Vec::new();
4043
let mut linklocal4 = Vec::new();
4144
let mut ula6 = Vec::new();
4245

4346
for iface in ifaces {
44-
if !is_up(&iface) {
47+
if !is_up(iface) {
4548
// Skip down interfaces
4649
continue;
4750
}
48-
let ifc_is_loopback = is_loopback(&iface);
51+
let ifc_is_loopback = is_loopback(iface);
4952
let addrs = iface
5053
.ipv4
5154
.iter()

0 commit comments

Comments
 (0)