Skip to content

Commit

Permalink
Refactor interface naming
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Jul 26, 2024
1 parent 82091d2 commit 8f5aacf
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 61 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ path = "proton_mac"

[dependencies.proton_nif]
path = "proton_nif"
features = ["cndn"]

[dependencies.proton_wap]
path = "proton_wap"
8 changes: 4 additions & 4 deletions examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use proton::{
ap::AccessPoint,
device::Device,
error::ProtonResult,
ifnames::DEFAULT_WIRELESS_INTERFACE,
};

#[tokio::main]
Expand All @@ -18,13 +17,14 @@ async fn main() -> ProtonResult<()> {
Ipv4Addr::new(192, 168, 0, 0), // Network address
24, // Network length
).unwrap(),
"wlp4s0",
)?;

println!("Scanning {}...", DEFAULT_WIRELESS_INTERFACE);
println!("Scanning network interface: {}...", "wlp4s0");

let devices: Vec<Device> = ap.scan(DEFAULT_WIRELESS_INTERFACE).await?;
let devices: Vec<Device> = ap.scan().await?;

println!("{:#?}", devices);
println!("Found: {:#?}", devices);

Ok (())
}
9 changes: 7 additions & 2 deletions proton_arp/src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,25 @@ pub struct ArpManager {

/// A correspondence between IPv4 addresses and MAC addresses.
cache: ArpCache,

/// The name of the network interface to be scanned.
ifname: String,
}

impl ArpManager {
/// Construct a new ARP manager.
///
/// # Parameters
/// - `range` (`Ipv4Cidr`): the CIDR range of the network
/// - `ifname` (`&str`): the name of the network interface
///
/// # Returns
/// A new `ArpManager` with an empty cache.
pub fn new(range: Ipv4Cidr) -> Self {
pub fn new(range: Ipv4Cidr, ifname: &str) -> Self {
Self {
range,
cache: ArpCache::new(),
ifname: ifname.to_string(),
}
}

Expand All @@ -60,7 +65,7 @@ impl ArpManager {
}

// Scan the network and update the cache
self.cache.set(scan(addresses).await?);
self.cache.set(scan(addresses, &self.ifname).await?);

Ok (())
}
Expand Down
10 changes: 4 additions & 6 deletions proton_arp/src/scan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ use tokio::{

use proton_err::ProtonResult;

use proton_nif::{
ifnames::DEFAULT_WIRELESS_INTERFACE,
NetworkInterface,
};
use proton_nif::NetworkInterface;

use crate::ArpCacheEntry;

Expand All @@ -35,13 +32,14 @@ pub static ARP_LISTENER_DELAY: Duration = Duration::from_millis(2_500);
///
/// # Parameters
/// - `ips` (`Vec<Ipv4Addr>`): the IPv4 addresses to scan
/// - `ifname` (`&str`): the name of the wireless interface to scan
///
/// # Returns
/// A `ProtonResult<Vec<ArpCacheEntry>>` containing the ARP responses
/// received, if the scan was successful.
pub async fn scan(ips: Vec<Ipv4Addr>) -> ProtonResult<Vec<ArpCacheEntry>> {
pub async fn scan(ips: Vec<Ipv4Addr>, ifname: &str) -> ProtonResult<Vec<ArpCacheEntry>> {
// Get the wireless network interface
let interface = NetworkInterface::new(DEFAULT_WIRELESS_INTERFACE)?;
let interface = NetworkInterface::new(ifname)?;

// Create an asynchronous communication channel for received replies
let (reply_tx, reply_rx) = mpsc::channel::<ArpCacheEntry>(ARP_CHANNEL_BUFFER_SIZE);
Expand Down
14 changes: 10 additions & 4 deletions proton_dev/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub struct DeviceManager {
/// Network interface socket.
socket: Socket,

/// Wireless interface name.
wlifname: String,

/// ARP cache manager.
arp_manager: ArpManager,
}
Expand All @@ -38,14 +41,17 @@ impl DeviceManager {
///
/// # Parameters
/// - `range` (`Ipv4Cidr`): the CIDR range of the network
/// - `wlifname` (`&str`): the name of the wireless interface
/// over which to scan for connected devices
///
/// # Returns
/// The result type `ProtonResult<DeviceManager>` containing the device
/// manager, if its initialization was successful.
pub fn new(range: Ipv4Cidr) -> ProtonResult<Self> {
pub fn new(range: Ipv4Cidr, wlifname: &str) -> ProtonResult<Self> {
Ok (Self {
socket: Socket::connect()?,
arp_manager: ArpManager::new(range),
wlifname: wlifname.to_string(),
arp_manager: ArpManager::new(range, wlifname),
})
}

Expand All @@ -57,12 +63,12 @@ impl DeviceManager {
/// # Returns
/// The result type `ProtonResult<Vec<Device>>` containing a list of
/// connected devices.
pub async fn scan(&mut self, ifname: &str) -> ProtonResult<Vec<Device>> {
pub async fn scan(&mut self) -> ProtonResult<Vec<Device>> {
// Perform an ARP scan of the network to get IPs
self.arp_manager.scan().await?;

// Determine Wi-Fi device by name
let check_wifi_device = |iface: &Interface| parse_string(&iface.name.clone().unwrap_or_default()) == ifname;
let check_wifi_device = |iface: &Interface| parse_string(&iface.name.clone().unwrap_or_default()) == self.wlifname;

// Get the Wi-Fi device
let interface = self.socket.get_interfaces_info()?
Expand Down
6 changes: 1 addition & 5 deletions proton_nif/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,4 @@ features = ["full"]
version = "0.35.0"

[dependencies.proton_err]
path = "../proton_err"

[features]
ethx = []
cndn = []
path = "../proton_err"
28 changes: 1 addition & 27 deletions proton_nif/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,4 @@

mod nif;

pub use nif::NetworkInterface;

#[cfg(all(feature = "cndn", feature = "ethx"))]
compile_error!("features `cndn` and `ethx` are mutually exclusive");

#[cfg(not(any(feature = "cndn", feature = "ethx")))]
compile_error!("you must select exactly one of features `cndn` and `ethx`");

// Feature `cndn` should be used on machines that use Consistent Network Device Naming.
#[cfg(feature = "cndn")]
pub mod ifnames {
/// The default wireless network interface to use.
pub const DEFAULT_WIRELESS_INTERFACE: &str = "wlp4s0";

/// The default wired network interface to use.
pub const DEFAULT_WIRED_INTERFACE: &str = "eno1";
}

// Feature `ethx` should be used on machines that use the old `ethX` network interface naming system.
#[cfg(feature = "ethx")]
pub mod ifnames {
/// The default wireless network interface to use.
pub const DEFAULT_WIRELESS_INTERFACE: &str = "wlan0";

/// The default wired network interface to use.
pub const DEFAULT_WIRED_INTERFACE: &str = "eth0";
}
pub use nif::NetworkInterface;
16 changes: 9 additions & 7 deletions proton_wap/src/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use proton_dev::{

use proton_err::ProtonResult;

#[allow(dead_code)]
/// A wireless access point.
pub struct AccessPoint {
#[allow(dead_code)]
/// CIDR network range.
range: Ipv4Cidr,

Expand All @@ -25,30 +25,32 @@ impl AccessPoint {
/// # Parameters
/// - `range` (`Ipv4Cidr`): the internal network range associated to
/// this access point
/// - `wlifname` (`&str`): the name of the wireless interface over which
/// this access point connects to remote devices
///
/// # Returns
/// A `ProtonResult<AccessPoint>` containing a new `AccessPoint` if
/// initialization was successful.
pub fn new(
range: Ipv4Cidr,
wlifname: &str,
) -> ProtonResult<Self> {
Ok (Self {
range,
manager: DeviceManager::new(range)?,
manager: DeviceManager::new(range, wlifname)?,
})
}

/// Get a list of all connected devices.
///
/// # Parameters
/// - `ifname` (`&str`): the name of the wireless network interface to
/// scan
/// None.
///
/// # Returns
/// A `ProtonResult<Vec<Device>>` wrappping the list of devices, if
/// the network scan was successful.
pub async fn scan(&mut self, ifname: &str) -> ProtonResult<Vec<Device>> {
Ok (self.manager.scan(ifname).await?)
pub async fn scan(&mut self) -> ProtonResult<Vec<Device>> {
Ok (self.manager.scan().await?)
}

/// Continuously route packets, monitoring both the Data Link Layer and
Expand All @@ -62,6 +64,6 @@ impl AccessPoint {
///
/// This function does not return during nominal operation.
pub async fn run(&mut self) -> ProtonResult<()> {
loop { }
todo!()
}
}
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@ pub mod error {
ProtonError,
ProtonResult,
};
}

/// Network interface names.
pub mod ifnames {
pub use proton_nif::ifnames::*;
}

0 comments on commit 8f5aacf

Please sign in to comment.