Skip to content

Commit

Permalink
Add native MAC address structure
Browse files Browse the repository at this point in the history
  • Loading branch information
josephrhobbs committed Jul 26, 2024
1 parent 8f5aacf commit 6551a0a
Show file tree
Hide file tree
Showing 14 changed files with 95 additions and 240 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ use proton::{

#[tokio::main]
async fn main() -> ProtonResult<()> {
let ifname = "wlp4s0";

let mut ap = AccessPoint::new(
Ipv4Cidr::new( // Internal network range
Ipv4Addr::new(192, 168, 0, 0), // Network address
24, // Network length
).unwrap(),
"wlp4s0",
ifname,
)?;

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

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

Expand Down
3 changes: 3 additions & 0 deletions proton_arp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ version = "0.35.0"
[dependencies.proton_err]
path = "../proton_err"

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

[dependencies.proton_nif]
path = "../proton_nif"
4 changes: 2 additions & 2 deletions proton_arp/src/arp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::net::Ipv4Addr;

use cidr::Ipv4Cidr;

use pnet::datalink::MacAddr;

use proton_err::ProtonResult;

use proton_mac::MacAddr;

use crate::{
ArpCache,
ArpCacheIterator,
Expand Down
2 changes: 1 addition & 1 deletion proton_arp/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
},
};

use pnet::datalink::MacAddr;
use proton_mac::MacAddr;

#[derive(Clone)]
/// An address resolution cache.
Expand Down
2 changes: 1 addition & 1 deletion proton_arp/src/scan/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub async fn listen(
// Construct cache entry
let entry = ArpCacheEntry::new(
arp_packet.get_sender_proto_addr(),
arp_packet.get_sender_hw_addr(),
arp_packet.get_sender_hw_addr().into(),
);

// Send the reply
Expand Down
5 changes: 4 additions & 1 deletion proton_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ features = ["derive"]
path = "../proton_arp"

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

[dependencies.proton_mac]
path = "../proton_mac"
11 changes: 7 additions & 4 deletions proton_dev/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ use serde::Serialize;

use proton_arp::ArpManager;

use proton_mac::MacAddr;

#[derive(Serialize, Clone, Copy, Debug)]
/// Information about a connected network device.
pub struct Device {
/// MAC address of the device.
pub mac: [u8; 6],
pub mac: MacAddr,

/// IPv4 address of the device.
pub ipv4: Ipv4Addr,
Expand All @@ -32,13 +34,14 @@ impl Device {
/// Convert a `Station` into a `Device` by checking the ARP cache.
pub fn from_station(station: Station, arp: &ArpManager) -> Self {
// Get hardware address of the station
let mac: [u8; 6] = station.bssid
let mac: MacAddr = station.bssid
.unwrap_or_default()
.try_into()
.unwrap_or([0; 6]);
.unwrap_or([0; 6])
.into();

// Get IPv4 address of the station
let ipv4: Ipv4Addr = arp.lookup_mac(mac.into())
let ipv4: Ipv4Addr = arp.lookup_mac(mac)
.unwrap_or(Ipv4Addr::new(0, 0, 0, 0));

// Get signal strength of this station
Expand Down
2 changes: 1 addition & 1 deletion proton_dev/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl DeviceManager {
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()) == self.wlifname;
let check_wifi_device = |iface: &Interface| parse_string(&iface.name.clone().unwrap_or_default()).trim_end_matches('\0') == self.wlifname;

// Get the Wi-Fi device
let interface = self.socket.get_interfaces_info()?
Expand Down
13 changes: 11 additions & 2 deletions proton_mac/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ name = "proton_mac"
version = "0.1.0"
edition = "2021"

[lib]
name = "proton_mac"
path = "src/lib.rs"

[dependencies]
serde_json = "1.0.120"

[dependencies.pnet]
version = "0.35.0"
default-features = false
features = ["std"]

[dependencies.serde]
version = "1.0.204"
features = ["derive"]
30 changes: 0 additions & 30 deletions proton_mac/src/error.rs

This file was deleted.

11 changes: 3 additions & 8 deletions proton_mac/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
//! MAC address policy management for the Proton access point management library.
//! MAC address data structure for the Proton access point management library.

#![deny(warnings)]
#![deny(missing_docs)]
mod mac;

mod error;
mod mac_addr_policy;

pub use error::MacAddrPolicyError;
pub use mac_addr_policy::MacAddrPolicy;
pub use mac::MacAddr;
54 changes: 54 additions & 0 deletions proton_mac/src/mac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//! MAC address type.

use std::fmt::{
Display,
Debug,
Formatter,
Result,
};

use serde::Serialize;

#[derive(Serialize, PartialEq, Eq, Clone, Copy)]
/// A hardware (MAC) address consisting of six octets.
pub struct MacAddr ([u8; 6]);

impl Display for MacAddr {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
f,
"{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.0[0],
self.0[1],
self.0[2],
self.0[3],
self.0[4],
self.0[5],
)
}
}

impl Debug for MacAddr {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{}", self)
}
}

impl From<[u8; 6]> for MacAddr {
fn from(octets: [u8; 6]) -> Self {
Self (octets)
}
}

impl From<pnet::datalink::MacAddr> for MacAddr {
fn from(mac: pnet::datalink::MacAddr) -> Self {
Self ([
mac.0,
mac.1,
mac.2,
mac.3,
mac.4,
mac.5,
])
}
}
Loading

0 comments on commit 6551a0a

Please sign in to comment.