Skip to content

Commit 7789a22

Browse files
committed
uefi-raw: IPAddress: remove type duplication; use core::net types
This simplifies things as we have less types to work with. The overall convenience is improved and people will be less confused with the automated imports suggested by their IDE. As the core::Net types are ABI compatible, and it is unlikely that they will ever change in core, we should go for this simplification.
1 parent cc48a0a commit 7789a22

File tree

6 files changed

+74
-94
lines changed

6 files changed

+74
-94
lines changed

uefi-raw/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
- Added `DevicePathProtocol::length()` properly constructing the `u16` value
1818

1919
## Changed
20+
- **Breaking:** Types `Ipv4Address` and `Ipv6Address` have been removed. They
21+
were replaced by `core::net::Ipv4Addr` and `core::net::Ipv6Addr`, as they are
22+
ABI compatible. This mainly affects the `IpAddress` wrapper type.
2023
- `DevicePathProtocol` now derives
2124
`Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash`
2225

uefi-raw/src/lib.rs

+28-65
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub use uguid::{guid, Guid};
3838

3939
use core::ffi::c_void;
4040
use core::fmt::{self, Debug, Formatter};
41+
use core::net::{IpAddr, Ipv4Addr, Ipv6Addr};
4142

4243
/// Handle to an event structure.
4344
pub type Event = *mut c_void;
@@ -106,40 +107,6 @@ impl From<Boolean> for bool {
106107
}
107108
}
108109

109-
/// An IPv4 internet protocol address.
110-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
111-
#[repr(transparent)]
112-
pub struct Ipv4Address(pub [u8; 4]);
113-
114-
impl From<core::net::Ipv4Addr> for Ipv4Address {
115-
fn from(ip: core::net::Ipv4Addr) -> Self {
116-
Self(ip.octets())
117-
}
118-
}
119-
120-
impl From<Ipv4Address> for core::net::Ipv4Addr {
121-
fn from(ip: Ipv4Address) -> Self {
122-
Self::from(ip.0)
123-
}
124-
}
125-
126-
/// An IPv6 internet protocol address.
127-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
128-
#[repr(transparent)]
129-
pub struct Ipv6Address(pub [u8; 16]);
130-
131-
impl From<core::net::Ipv6Addr> for Ipv6Address {
132-
fn from(ip: core::net::Ipv6Addr) -> Self {
133-
Self(ip.octets())
134-
}
135-
}
136-
137-
impl From<Ipv6Address> for core::net::Ipv6Addr {
138-
fn from(ip: Ipv6Address) -> Self {
139-
Self::from(ip.0)
140-
}
141-
}
142-
143110
/// An IPv4 or IPv6 internet protocol address.
144111
///
145112
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
@@ -150,10 +117,10 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
150117
#[repr(C)]
151118
pub union IpAddress {
152119
/// An IPv4 internet protocol address.
153-
pub v4: Ipv4Address,
120+
pub v4: Ipv4Addr,
154121

155122
/// An IPv6 internet protocol address.
156-
pub v6: Ipv6Address,
123+
pub v6: Ipv6Addr,
157124

158125
/// This member serves to align the whole type to 4 bytes as required by
159126
/// the spec. Note that this is slightly different from `repr(align(4))`,
@@ -164,17 +131,17 @@ pub union IpAddress {
164131
impl IpAddress {
165132
/// Construct a new IPv4 address.
166133
#[must_use]
167-
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
134+
pub fn new_v4(ip_addr: [u8; 4]) -> Self {
168135
Self {
169-
v4: Ipv4Address(ip_addr),
136+
v4: Ipv4Addr::from(ip_addr),
170137
}
171138
}
172139

173140
/// Construct a new IPv6 address.
174141
#[must_use]
175-
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
142+
pub fn new_v6(ip_addr: [u8; 16]) -> Self {
176143
Self {
177-
v6: Ipv6Address(ip_addr),
144+
v6: Ipv6Addr::from(ip_addr),
178145
}
179146
}
180147
}
@@ -190,18 +157,20 @@ impl Debug for IpAddress {
190157

191158
impl Default for IpAddress {
192159
fn default() -> Self {
193-
Self { _align_helper: [0u32; 4] }
160+
Self {
161+
_align_helper: [0u32; 4],
162+
}
194163
}
195164
}
196165

197-
impl From<core::net::IpAddr> for IpAddress {
198-
fn from(t: core::net::IpAddr) -> Self {
166+
impl From<IpAddr> for IpAddress {
167+
fn from(t: IpAddr) -> Self {
199168
match t {
200-
core::net::IpAddr::V4(ip) => Self {
201-
v4: Ipv4Address::from(ip),
169+
IpAddr::V4(ip) => Self {
170+
v4: Ipv4Addr::from(ip),
202171
},
203-
core::net::IpAddr::V6(ip) => Self {
204-
v6: Ipv6Address::from(ip),
172+
IpAddr::V6(ip) => Self {
173+
v6: Ipv6Addr::from(ip),
205174
},
206175
}
207176
}
@@ -261,33 +230,27 @@ mod tests {
261230
assert!(bool::from(Boolean(0b11111111)));
262231
}
263232

264-
/// Test round-trip conversion between `Ipv4Address` and `core::net::Ipv4Addr`.
265-
#[test]
266-
fn test_ip_addr4_conversion() {
267-
let uefi_addr = Ipv4Address(TEST_IPV4);
268-
let core_addr = core::net::Ipv4Addr::from(uefi_addr);
269-
assert_eq!(uefi_addr, Ipv4Address::from(core_addr));
270-
}
271-
272-
/// Test round-trip conversion between `Ipv6Address` and `core::net::Ipv6Addr`.
233+
/// We test that the core::net-types are ABI compatible with the EFI types.
234+
/// As long as this is the case, we can reuse core functionality and
235+
/// prevent type duplication.
273236
#[test]
274-
fn test_ip_addr6_conversion() {
275-
let uefi_addr = Ipv6Address(TEST_IPV6);
276-
let core_addr = core::net::Ipv6Addr::from(uefi_addr);
277-
assert_eq!(uefi_addr, Ipv6Address::from(core_addr));
237+
fn net_abi() {
238+
assert_eq!(size_of::<Ipv4Addr>(), 4);
239+
assert_eq!(align_of::<Ipv4Addr>(), 1);
240+
assert_eq!(size_of::<Ipv6Addr>(), 16);
241+
assert_eq!(align_of::<Ipv6Addr>(), 1);
278242
}
279-
280243
/// Test conversion from `core::net::IpAddr` to `IpvAddress`.
281244
///
282245
/// Note that conversion in the other direction is not possible.
283246
#[test]
284247
fn test_ip_addr_conversion() {
285-
let core_addr = core::net::IpAddr::V4(core::net::Ipv4Addr::from(TEST_IPV4));
248+
let core_addr = IpAddr::V4(core::net::Ipv4Addr::from(TEST_IPV4));
286249
let uefi_addr = IpAddress::from(core_addr);
287-
assert_eq!(unsafe { uefi_addr.v4.0 }, TEST_IPV4);
250+
assert_eq!(unsafe { uefi_addr.v4.octets() }, TEST_IPV4);
288251

289-
let core_addr = core::net::IpAddr::V6(core::net::Ipv6Addr::from(TEST_IPV6));
252+
let core_addr = IpAddr::V6(core::net::Ipv6Addr::from(TEST_IPV6));
290253
let uefi_addr = IpAddress::from(core_addr);
291-
assert_eq!(unsafe { uefi_addr.v6.0 }, TEST_IPV6);
254+
assert_eq!(unsafe { uefi_addr.v6.octets() }, TEST_IPV6);
292255
}
293256
}

uefi-raw/src/protocol/network/dhcp4.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use crate::{guid, Boolean, Char8, Event, Guid, Ipv4Address, MacAddress, Status};
3+
use crate::{guid, Boolean, Char8, Event, Guid, MacAddress, Status};
44
use core::ffi::c_void;
5+
use core::net::Ipv4Addr;
56

67
newtype_enum! {
78
pub enum Dhcp4Event: i32 => {
@@ -58,10 +59,10 @@ pub struct Dhcp4Header {
5859
pub xid: u32,
5960
pub seconds: u16,
6061
pub reserved: u16,
61-
pub client_addr: Ipv4Address,
62-
pub your_addr: Ipv4Address,
63-
pub server_addr: Ipv4Address,
64-
pub gateway_addr: Ipv4Address,
62+
pub client_addr: Ipv4Addr,
63+
pub your_addr: Ipv4Addr,
64+
pub server_addr: Ipv4Addr,
65+
pub gateway_addr: Ipv4Addr,
6566
pub client_hw_addr: [u8; 16],
6667
pub server_name: [Char8; 64],
6768
pub boot_file_name: [Char8; 128],
@@ -86,7 +87,7 @@ pub struct Dhcp4ConfigData {
8687
pub discover_timeout: *mut u32,
8788
pub request_try_count: u32,
8889
pub request_timeout: *mut u32,
89-
pub client_address: Ipv4Address,
90+
pub client_address: Ipv4Addr,
9091
pub callback: Option<
9192
unsafe extern "efiapi" fn(
9293
this: *mut Dhcp4Protocol,
@@ -107,20 +108,20 @@ pub struct Dhcp4ConfigData {
107108
pub struct Dhcp4ModeData {
108109
pub state: Dhcp4State,
109110
pub config_data: Dhcp4ConfigData,
110-
pub client_address: Ipv4Address,
111+
pub client_address: Ipv4Addr,
111112
pub client_mac_address: MacAddress,
112-
pub server_address: Ipv4Address,
113-
pub router_address: Ipv4Address,
114-
pub subnet_mask: Ipv4Address,
113+
pub server_address: Ipv4Addr,
114+
pub router_address: Ipv4Addr,
115+
pub subnet_mask: Ipv4Addr,
115116
pub lease_time: u32,
116117
pub reply_packet: *const Dhcp4Packet,
117118
}
118119

119120
#[derive(Debug)]
120121
#[repr(C)]
121122
pub struct Dhcp4ListenPoint {
122-
pub listen_address: Ipv4Address,
123-
pub subnet_mask: Ipv4Address,
123+
pub listen_address: Ipv4Addr,
124+
pub subnet_mask: Ipv4Addr,
124125
pub listen_port: u16,
125126
}
126127

@@ -129,9 +130,9 @@ pub struct Dhcp4ListenPoint {
129130
pub struct Dhcp4TransmitReceiveToken {
130131
pub status: Status,
131132
pub completion_event: Event,
132-
pub remote_address: Ipv4Address,
133+
pub remote_address: Ipv4Addr,
133134
pub remote_port: u16,
134-
pub gateway_address: Ipv4Address,
135+
pub gateway_address: Ipv4Addr,
135136
pub listen_point_count: u32,
136137
pub listen_points: *mut Dhcp4ListenPoint,
137138
pub timeout_value: u32,

uefi-raw/src/protocol/network/http.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use crate::{guid, Boolean, Char16, Char8, Event, Guid, Ipv4Address, Ipv6Address, Status};
3+
use crate::{guid, Boolean, Char16, Char8, Event, Guid, Status};
44
use core::ffi::c_void;
55
use core::fmt::{self, Debug, Formatter};
6+
use core::net::{Ipv4Addr, Ipv6Addr};
67
use core::ptr;
78

89
#[derive(Debug, Default)]
@@ -23,19 +24,30 @@ newtype_enum! {
2324
}
2425
}
2526

26-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
27+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
2728
#[repr(C)]
2829
pub struct HttpV4AccessPoint {
2930
pub use_default_addr: Boolean,
30-
pub local_address: Ipv4Address,
31-
pub local_subnet: Ipv4Address,
31+
pub local_address: Ipv4Addr,
32+
pub local_subnet: Ipv4Addr,
3233
pub local_port: u16,
3334
}
3435

35-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
36+
impl Default for HttpV4AccessPoint {
37+
fn default() -> Self {
38+
Self {
39+
use_default_addr: Default::default(),
40+
local_address: Ipv4Addr::from_bits(0),
41+
local_subnet: Ipv4Addr::from_bits(0),
42+
local_port: 0,
43+
}
44+
}
45+
}
46+
47+
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
3648
#[repr(C)]
3749
pub struct HttpV6AccessPoint {
38-
pub local_address: Ipv6Address,
50+
pub local_address: Ipv6Addr,
3951
pub local_port: u16,
4052
}
4153

uefi-raw/src/protocol/network/ip4.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3-
use crate::Ipv4Address;
3+
use core::net::Ipv4Addr;
44

55
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
66
#[repr(C)]
77
pub struct Ip4RouteTable {
8-
pub subnet_addr: Ipv4Address,
9-
pub subnet_mask: Ipv4Address,
10-
pub gateway_addr: Ipv4Address,
8+
pub subnet_addr: Ipv4Addr,
9+
pub subnet_mask: Ipv4Addr,
10+
pub gateway_addr: Ipv4Addr,
1111
}

uefi-raw/src/protocol/network/ip4_config2.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

33
use crate::protocol::network::ip4::Ip4RouteTable;
4-
use crate::{guid, Char16, Event, Guid, Ipv4Address, MacAddress, Status};
4+
use crate::{guid, Char16, Event, Guid, MacAddress, Status};
55
use core::ffi::c_void;
6+
use core::net::Ipv4Addr;
67

78
newtype_enum! {
89
pub enum Ip4Config2DataType: i32 => {
@@ -22,8 +23,8 @@ pub struct Ip4Config2InterfaceInfo {
2223
pub if_type: u8,
2324
pub hw_addr_size: u32,
2425
pub hw_addr: MacAddress,
25-
pub station_addr: Ipv4Address,
26-
pub subnet_mask: Ipv4Address,
26+
pub station_addr: Ipv4Addr,
27+
pub subnet_mask: Ipv4Addr,
2728
pub route_table_size: u32,
2829
pub route_table: *mut Ip4RouteTable,
2930
}
@@ -39,8 +40,8 @@ newtype_enum! {
3940
#[derive(Debug)]
4041
#[repr(C)]
4142
pub struct Ip4Config2ManualAddress {
42-
pub address: Ipv4Address,
43-
pub subnet_mask: Ipv4Address,
43+
pub address: Ipv4Addr,
44+
pub subnet_mask: Ipv4Addr,
4445
}
4546

4647
#[derive(Debug)]

0 commit comments

Comments
 (0)