diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 8468d9e0d..c0600a991 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -7,6 +7,8 @@ - Added `HiiConfigAccessProtocol`. - Added `::octets()` for `Ipv4Address`, `Ipv6Address`, and `MacAddress` to streamline the API with `core::net`. +- Added `::into_core_addr()` for `IpAddress` +- Added `::into_ethernet_addr()` for `MacAddress` ## Changed - **Breaking:** The MSRV is now 1.85.1 and the crate uses the Rust 2024 edition. diff --git a/uefi-raw/src/net.rs b/uefi-raw/src/net.rs index 773dd2e8f..f866d2b05 100644 --- a/uefi-raw/src/net.rs +++ b/uefi-raw/src/net.rs @@ -98,6 +98,27 @@ impl IpAddress { v6: Ipv6Address(ip_addr), } } + + /// Transforms this EFI type to the Rust standard library's type + /// [`core::net::IpAddr`]. + /// + /// # Arguments + /// - `is_ipv6`: Whether the internal data should be interpreted as IPv6 or + /// IPv4 address. + /// + /// # Safety + /// Callers must ensure that the `v4` field is valid if `is_ipv6` is false, + /// and that the `v6` field is valid if `is_ipv6` is true + #[must_use] + pub unsafe fn into_core_addr(self, is_ipv6: bool) -> core::net::IpAddr { + if is_ipv6 { + // SAFETY: Caller assumes that the underlying data is initialized. + core::net::IpAddr::V6(core::net::Ipv6Addr::from(unsafe { self.v6.octets() })) + } else { + // SAFETY: Caller assumes that the underlying data is initialized. + core::net::IpAddr::V4(core::net::Ipv4Addr::from(unsafe { self.v4.octets() })) + } + } } impl Debug for IpAddress { @@ -147,6 +168,15 @@ impl MacAddress { pub const fn octets(self) -> [u8; 32] { self.0 } + + /// Interpret the MAC address as normal 6-byte MAC address, as used in + /// Ethernet. + #[must_use] + pub fn into_ethernet_addr(self) -> [u8; 6] { + let mut buffer = [0; 6]; + buffer.copy_from_slice(&self.octets()[6..]); + buffer + } } // Normal/typical MAC addresses, such as in Ethernet.