@@ -38,6 +38,7 @@ pub use uguid::{guid, Guid};
38
38
39
39
use core:: ffi:: c_void;
40
40
use core:: fmt:: { self , Debug , Formatter } ;
41
+ use core:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
41
42
42
43
/// Handle to an event structure.
43
44
pub type Event = * mut c_void ;
@@ -106,40 +107,6 @@ impl From<Boolean> for bool {
106
107
}
107
108
}
108
109
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
-
143
110
/// An IPv4 or IPv6 internet protocol address.
144
111
///
145
112
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
@@ -150,10 +117,10 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
150
117
#[ repr( C ) ]
151
118
pub union IpAddress {
152
119
/// An IPv4 internet protocol address.
153
- pub v4 : Ipv4Address ,
120
+ pub v4 : Ipv4Addr ,
154
121
155
122
/// An IPv6 internet protocol address.
156
- pub v6 : Ipv6Address ,
123
+ pub v6 : Ipv6Addr ,
157
124
158
125
/// This member serves to align the whole type to 4 bytes as required by
159
126
/// the spec. Note that this is slightly different from `repr(align(4))`,
@@ -164,17 +131,17 @@ pub union IpAddress {
164
131
impl IpAddress {
165
132
/// Construct a new IPv4 address.
166
133
#[ must_use]
167
- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
134
+ pub fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
168
135
Self {
169
- v4 : Ipv4Address ( ip_addr) ,
136
+ v4 : Ipv4Addr :: from ( ip_addr) ,
170
137
}
171
138
}
172
139
173
140
/// Construct a new IPv6 address.
174
141
#[ must_use]
175
- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
142
+ pub fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
176
143
Self {
177
- v6 : Ipv6Address ( ip_addr) ,
144
+ v6 : Ipv6Addr :: from ( ip_addr) ,
178
145
}
179
146
}
180
147
}
@@ -190,18 +157,20 @@ impl Debug for IpAddress {
190
157
191
158
impl Default for IpAddress {
192
159
fn default ( ) -> Self {
193
- Self { _align_helper : [ 0u32 ; 4 ] }
160
+ Self {
161
+ _align_helper : [ 0u32 ; 4 ] ,
162
+ }
194
163
}
195
164
}
196
165
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 {
199
168
match t {
200
- core :: net :: IpAddr :: V4 ( ip) => Self {
201
- v4 : Ipv4Address :: from ( ip) ,
169
+ IpAddr :: V4 ( ip) => Self {
170
+ v4 : Ipv4Addr :: from ( ip) ,
202
171
} ,
203
- core :: net :: IpAddr :: V6 ( ip) => Self {
204
- v6 : Ipv6Address :: from ( ip) ,
172
+ IpAddr :: V6 ( ip) => Self {
173
+ v6 : Ipv6Addr :: from ( ip) ,
205
174
} ,
206
175
}
207
176
}
@@ -261,33 +230,27 @@ mod tests {
261
230
assert ! ( bool :: from( Boolean ( 0b11111111 ) ) ) ;
262
231
}
263
232
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.
273
236
#[ 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 ) ;
278
242
}
279
-
280
243
/// Test conversion from `core::net::IpAddr` to `IpvAddress`.
281
244
///
282
245
/// Note that conversion in the other direction is not possible.
283
246
#[ test]
284
247
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 ) ) ;
286
249
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 ) ;
288
251
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 ) ) ;
290
253
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 ) ;
292
255
}
293
256
}
0 commit comments