Skip to content

Commit 002ac6f

Browse files
committedJul 24, 2024·
Remove stale IPv4 detection from ARP cache
1 parent b819ad2 commit 002ac6f

File tree

3 files changed

+14
-59
lines changed

3 files changed

+14
-59
lines changed
 

‎proton_arp/src/arp.rs

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! Address resolution manager.
22
3-
use std::{
4-
net::Ipv4Addr,
5-
time::Duration,
6-
};
3+
use std::net::Ipv4Addr;
74

85
use cidr::Ipv4Cidr;
96

@@ -31,15 +28,13 @@ impl ArpManager {
3128
///
3229
/// # Parameters
3330
/// - `range` (`Ipv4Cidr`): the CIDR range of the network
34-
/// - `refresh` (`Duration`): the amount of time after which ARP
35-
/// cache entries should be refreshed.
36-
/// ArpCache
31+
///
3732
/// # Returns
3833
/// A new `ArpManager` with an empty cache.
39-
pub fn new(range: Ipv4Cidr, refresh: Duration) -> Self {
34+
pub fn new(range: Ipv4Cidr) -> Self {
4035
Self {
4136
range,
42-
cache: ArpCache::new(refresh),
37+
cache: ArpCache::new(),
4338
}
4439
}
4540

@@ -69,22 +64,6 @@ impl ArpManager {
6964
Ok (())
7065
}
7166

72-
/// Refresh the cache without scanning for new devices.
73-
///
74-
/// # Parameters
75-
/// None.
76-
///
77-
/// # Returns
78-
/// An `ArpResult` indicating whether or not the refresh was successful.
79-
pub async fn refresh(&mut self) -> ArpResult {
80-
let addresses = self.cache.get_stale_ips();
81-
82-
// Scan the network and update the cache
83-
self.cache.set(scan(addresses).await?);
84-
85-
Ok (())
86-
}
87-
8867
/// Get an iterator of the cache, without consuming the cache.
8968
///
9069
/// # Parameters

‎proton_arp/src/cache.rs

+2-27
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,19 @@ use pnet::datalink::MacAddr;
1919
pub struct ArpCache {
2020
/// Cache entries consisting of IPv4 addresses, MAC addresses, and the times of caching.
2121
cache: Vec<ArpCacheEntry>,
22-
23-
/// The amount of time after which an ARP cache entry should be refreshed.
24-
refresh: Duration,
2522
}
2623

2724
impl ArpCache {
2825
/// Construct a new ARP cache.
2926
///
3027
/// # Parameters
31-
/// - `refresh` (`Duration`): the amount of time after which ARP cache
32-
/// entries should be refreshed
28+
/// None.
3329
///
3430
/// # Returns
3531
/// A new, empty `ArpCache`.
36-
pub fn new(refresh: Duration) -> Self {
32+
pub fn new() -> Self {
3733
Self {
3834
cache: Vec::new(),
39-
refresh,
4035
}
4136
}
4237

@@ -66,26 +61,6 @@ impl ArpCache {
6661
pub fn set(&mut self, cache: Vec<ArpCacheEntry>) {
6762
self.cache = cache;
6863
}
69-
70-
/// Get a list of stale addresses in the ARP cache.
71-
///
72-
/// # Parameters
73-
/// None.
74-
///
75-
/// # Returns
76-
/// A `Vec<Ipv4Addr>` with a list of stale IPv4 addresses to check.
77-
pub fn get_stale_ips(&mut self) -> Vec<Ipv4Addr> {
78-
let mut stale_ips = Vec::new();
79-
80-
for entry in &self.cache {
81-
// Check if the entry needs to be refreshed
82-
if entry.check(self.refresh) {
83-
stale_ips.push(entry.ipv4);
84-
}
85-
}
86-
87-
stale_ips
88-
}
8964
}
9065

9166
impl IntoIterator for ArpCache {

‎proton_dev/src/manager.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Device discovery manager.
22
3-
use std::time::Duration;
4-
53
use cidr::Ipv4Cidr;
64

75
use neli::err::NlError;
@@ -38,16 +36,14 @@ impl DeviceManager {
3836
///
3937
/// # Parameters
4038
/// - `range` (`Ipv4Cidr`): the CIDR range of the network
41-
/// - `refresh` (`Duration`): the amount of time after which to refresh
42-
/// the ARP cache
4339
///
4440
/// # Returns
4541
/// The result type `NetlinkResult<DeviceManager>` containing the device
4642
/// manager, if its initialization was successful.
47-
pub fn new(range: Ipv4Cidr, refresh: Duration) -> NetlinkResult<Self> {
43+
pub fn new(range: Ipv4Cidr) -> NetlinkResult<Self> {
4844
Ok (Self {
4945
socket: Socket::connect()?,
50-
arp_manager: ArpManager::new(range, refresh),
46+
arp_manager: ArpManager::new(range),
5147
})
5248
}
5349

@@ -59,7 +55,12 @@ impl DeviceManager {
5955
/// # Returns
6056
/// The result type `NetlinkResult<Vec<Device>>` containing a list of
6157
/// connected devices.
62-
pub fn scan(&mut self, ifname: &str) -> NetlinkResult<Vec<Device>> {
58+
pub async fn scan(&mut self, ifname: &str) -> NetlinkResult<Vec<Device>> {
59+
// Perform an ARP scan of the network to get IPs
60+
if self.arp_manager.scan().await.is_err() {
61+
return Err (NlError::Msg ("could not scan network".to_string()));
62+
};
63+
6364
// Determine Wi-Fi device by name
6465
let check_wifi_device = |iface: &Interface| parse_string(&iface.name.clone().unwrap_or_default()) == ifname;
6566

0 commit comments

Comments
 (0)
Please sign in to comment.