mirror of
https://github.com/jmcorgan/fips.git
synced 2026-07-30 19:46:15 +00:00
transport: give ethernet a canonical addr.rs home for MAC parsing
Moves parse_mac_string into ethernet/addr.rs and re-exports it at the ethernet module root so the ethernet::parse_mac_string path stays byte-identical for its one external consumer (zero churn there). Its unit tests stay in mod.rs, calling through the re-export. Pure relocation; no logic change.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
//! Ethernet address parsing.
|
||||
|
||||
use crate::transport::TransportError;
|
||||
|
||||
/// Parse a colon-separated MAC string (e.g., "aa:bb:cc:dd:ee:ff") into bytes.
|
||||
pub fn parse_mac_string(s: &str) -> Result<[u8; 6], TransportError> {
|
||||
let parts: Vec<&str> = s.split(':').collect();
|
||||
if parts.len() != 6 {
|
||||
return Err(TransportError::InvalidAddress(format!(
|
||||
"invalid MAC format: expected 6 colon-separated hex bytes, got '{}'",
|
||||
s
|
||||
)));
|
||||
}
|
||||
let mut mac = [0u8; 6];
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
mac[i] = u8::from_str_radix(part, 16).map_err(|_| {
|
||||
TransportError::InvalidAddress(format!("invalid hex byte '{}' in MAC address", part))
|
||||
})?;
|
||||
}
|
||||
Ok(mac)
|
||||
}
|
||||
@@ -5,10 +5,13 @@
|
||||
//! Works on wired Ethernet and WiFi interfaces (kernel mac80211 abstracts
|
||||
//! 802.11 transparently on Linux).
|
||||
|
||||
pub mod addr;
|
||||
pub mod discovery;
|
||||
pub mod io;
|
||||
pub mod stats;
|
||||
|
||||
pub use addr::parse_mac_string;
|
||||
|
||||
use super::{
|
||||
DiscoveredPeer, PacketTx, ReceivedPacket, Transport, TransportAddr, TransportError,
|
||||
TransportId, TransportState, TransportType,
|
||||
@@ -632,24 +635,6 @@ pub fn format_mac(mac: &[u8; 6]) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
/// Parse a colon-separated MAC string (e.g., "aa:bb:cc:dd:ee:ff") into bytes.
|
||||
pub fn parse_mac_string(s: &str) -> Result<[u8; 6], TransportError> {
|
||||
let parts: Vec<&str> = s.split(':').collect();
|
||||
if parts.len() != 6 {
|
||||
return Err(TransportError::InvalidAddress(format!(
|
||||
"invalid MAC format: expected 6 colon-separated hex bytes, got '{}'",
|
||||
s
|
||||
)));
|
||||
}
|
||||
let mut mac = [0u8; 6];
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
mac[i] = u8::from_str_radix(part, 16).map_err(|_| {
|
||||
TransportError::InvalidAddress(format!("invalid hex byte '{}' in MAC address", part))
|
||||
})?;
|
||||
}
|
||||
Ok(mac)
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
Reference in New Issue
Block a user