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
2 changes: 2 additions & 0 deletions uefi-raw/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 30 additions & 0 deletions uefi-raw/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
Loading