Skip to content

Commit cd04d74

Browse files
committed
uefi-raw: net: add convenient into_* helpers
1 parent 82bf2b0 commit cd04d74

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
- Added `HiiConfigAccessProtocol`.
88
- Added `::octets()` for `Ipv4Address`, `Ipv6Address`, and
99
`MacAddress` to streamline the API with `core::net`.
10+
- Added `::into_core_addr()` for `IpAddress`
11+
- Added `::into_ethernet_addr()` for `MacAddress`
1012

1113
## Changed
1214
- **Breaking:** The MSRV is now 1.85.1 and the crate uses the Rust 2024 edition.

uefi-raw/src/net.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ impl IpAddress {
9898
v6: Ipv6Address(ip_addr),
9999
}
100100
}
101+
102+
/// Transforms this EFI type to the Rust standard library's type
103+
/// [`core::net::IpAddr`].
104+
///
105+
/// # Arguments
106+
/// - `is_ipv6`: Whether the internal data should be interpreted as IPv6 or
107+
/// IPv4 address.
108+
///
109+
/// # Safety
110+
/// Callers must ensure that the `v4` field is valid if `is_ipv6` is false,
111+
/// and that the `v6` field is valid if `is_ipv6` is true
112+
#[must_use]
113+
pub unsafe fn into_core_addr(self, is_ipv6: bool) -> core::net::IpAddr {
114+
if is_ipv6 {
115+
// SAFETY: Caller assumes that the underlying data is initialized.
116+
core::net::IpAddr::V6(core::net::Ipv6Addr::from(unsafe { self.v6.octets() }))
117+
} else {
118+
// SAFETY: Caller assumes that the underlying data is initialized.
119+
core::net::IpAddr::V4(core::net::Ipv4Addr::from(unsafe { self.v4.octets() }))
120+
}
121+
}
101122
}
102123

103124
impl Debug for IpAddress {
@@ -147,6 +168,14 @@ impl MacAddress {
147168
pub const fn octets(self) -> [u8; 32] {
148169
self.0
149170
}
171+
172+
/// Interpret the MAC address as normal 6-byte MAC address, as used in
173+
/// Ethernet.
174+
pub fn into_ethernet_addr(self) -> [u8; 6] {
175+
let mut buffer = [0; 6];
176+
buffer.copy_from_slice(&self.octets()[6..]);
177+
buffer
178+
}
150179
}
151180

152181
// Normal/typical MAC addresses, such as in Ethernet.

0 commit comments

Comments
 (0)