Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement conversions for IpAddress and MacAddress #1564

Merged
merged 3 commits into from
Mar 9, 2025
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
1 change: 1 addition & 0 deletions uefi-raw/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Added
- Added `Boolean` type
- Added `protocol::network::pxe` module.
- Added conversions between `MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses.


# uefi-raw - 0.10.0 (2025-02-07)
Expand Down
19 changes: 19 additions & 0 deletions uefi-raw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ impl Default for IpAddress {
#[repr(transparent)]
pub struct MacAddress(pub [u8; 32]);

impl From<[u8; 6]> for MacAddress {
fn from(octets: [u8; 6]) -> Self {
let mut buffer = [0; 32];
buffer[0] = octets[0];
buffer[1] = octets[1];
buffer[2] = octets[2];
buffer[3] = octets[3];
buffer[4] = octets[4];
buffer[5] = octets[5];
Self(buffer)
}
}

impl From<MacAddress> for [u8; 6] {
fn from(MacAddress(o): MacAddress) -> Self {
[o[0], o[1], o[2], o[3], o[4], o[5]]
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 2 additions & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Added
- Added `boot::signal_event`.
- Added conversions between `proto::network::IpAddress` and `core::net` types.
- Added conversions between `proto::network::MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses.

## Changed
- **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no
Expand Down
37 changes: 37 additions & 0 deletions uefi/src/proto/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,40 @@ impl IpAddress {
Self(ip_addr)
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicholasbishop Is there a reason why this type is not yet in uefi-raw?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed an issue about our IP address types: #1575

impl From<core::net::Ipv4Addr> for IpAddress {
fn from(t: core::net::Ipv4Addr) -> Self {
Self::new_v4(t.octets())
}
}

impl From<IpAddress> for core::net::Ipv4Addr {
fn from(IpAddress(o): IpAddress) -> Self {
Self::from([o[0], o[1], o[2], o[3]])
}
}

impl From<core::net::Ipv6Addr> for IpAddress {
fn from(t: core::net::Ipv6Addr) -> Self {
Self::new_v6(t.octets())
}
}

impl From<IpAddress> for core::net::Ipv6Addr {
fn from(value: IpAddress) -> Self {
Self::from(value.0)
}
}

impl From<core::net::IpAddr> for IpAddress {
fn from(t: core::net::IpAddr) -> Self {
match t {
core::net::IpAddr::V4(a) => a.into(),
core::net::IpAddr::V6(a) => a.into(),
}
}
}

// NOTE: We cannot impl From<IpAddress> for core::net::IpAddr
// because IpAddress is a raw union, with nothing indicating
// whether it should be considered v4 or v6.