Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions crates/host_env/src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,31 @@ pub use ::dns_lookup as dns;
#[cfg(not(target_arch = "wasm32"))]
pub use ::socket2 as raw;

/// Returns the first non-loopback MAC address as 6 bytes, or `None` when no
/// MAC address is available or the lookup fails.
#[cfg(not(any(
target_os = "ios",
target_os = "android",
target_os = "windows",
target_arch = "wasm32",
target_os = "redox"
)))]
fn select_mac_address(addresses: impl IntoIterator<Item = [u8; 6]>) -> Option<[u8; 6]> {
let mut first_local = None;

for address in addresses {
if address == [0; 6] {
continue;
}
if address[0] & 0x02 == 0 {
return Some(address);
}
first_local.get_or_insert(address);
}

first_local
}

/// Returns a universally administered MAC address when available, otherwise
/// the first locally administered address, or `None` when lookup fails.
#[cfg(not(any(
target_os = "ios",
target_os = "android",
Expand All @@ -28,10 +51,44 @@ pub use ::socket2 as raw;
target_os = "redox"
)))]
pub fn mac_address() -> Option<[u8; 6]> {
mac_address::get_mac_address()
.ok()
.flatten()
.map(|m| m.bytes())
let addresses = mac_address::MacAddressIterator::new().ok()?;
select_mac_address(addresses.map(|address| address.bytes()))
}

#[cfg(test)]
#[cfg(not(any(
target_os = "ios",
target_os = "android",
target_os = "windows",
target_arch = "wasm32",
target_os = "redox"
)))]
mod mac_address_tests {
use super::select_mac_address;

#[test]
fn prefers_universally_administered_address() {
let local = [0x02, 0, 0, 0, 0, 1];
let universal = [0x00, 0, 0, 0, 0, 2];

assert_eq!(select_mac_address([local, universal]), Some(universal));
}

#[test]
fn falls_back_to_first_locally_administered_address() {
let first = [0x02, 0, 0, 0, 0, 1];
let second = [0x06, 0, 0, 0, 0, 2];

assert_eq!(select_mac_address([first, second]), Some(first));
}

#[test]
fn ignores_zero_address() {
let universal = [0x00, 0, 0, 0, 0, 1];

assert_eq!(select_mac_address([[0; 6], universal]), Some(universal));
assert_eq!(select_mac_address([[0; 6]]), None);
}
}

#[cfg(unix)]
Expand Down
16 changes: 13 additions & 3 deletions crates/stdlib/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,17 @@ mod _uuid {
use std::sync::OnceLock;
use uuid::{ContextV1, Uuid, timestamp::Timestamp};

fn hardware_node_id() -> Option<&'static [u8; 6]> {
static HARDWARE_NODE_ID: OnceLock<Option<[u8; 6]>> = OnceLock::new();
HARDWARE_NODE_ID
.get_or_init(rustpython_host_env::socket::mac_address)
.as_ref()
}

fn get_node_id() -> [u8; 6] {
// os_random is expensive, but this is only ever called once
rustpython_host_env::socket::mac_address()
hardware_node_id()
.copied()
.unwrap_or_else(rustpython_common::rand::os_random::<6>)
}

Expand All @@ -28,6 +36,8 @@ mod _uuid {
0
}

#[pyattr(name = "has_stable_extractable_node")]
const HAS_STABLE_EXTRACTABLE_NODE: bool = false;
#[pyattr]
fn has_stable_extractable_node(_vm: &VirtualMachine) -> u32 {
hardware_node_id().is_some().into()
}
}
Loading