Skip to content

Commit 75aaf74

Browse files
committed
1 parent ca7695a commit 75aaf74

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

libc-test/build.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,7 @@ fn test_freebsd(target: &str) {
21982198
"memstat.h",
21992199
"mqueue.h",
22002200
"net/bpf.h",
2201+
"net/ethernet.h",
22012202
"net/if.h",
22022203
"net/if_arp.h",
22032204
"net/if_dl.h",

libc-test/semver/freebsd.txt

+13
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,17 @@ ERA_T_FMT
377377
EREMOTE
378378
ERPCMISMATCH
379379
ESOCKTNOSUPPORT
380+
ETHER_ADDR_LEN
381+
ETHER_CRC_LEN
382+
ETHER_HDR_LEN
383+
ETHER_IS_BROADCAST
384+
ETHER_IS_IPV6_MULTICAST
385+
ETHER_IS_MULTICAST
386+
ETHER_IS_ZERO
387+
ETHER_MIN_LEN
388+
ETHER_MAX_LEN
389+
ETHER_MAX_LEN_JUMBO
390+
ETHER_TYPE_LEN
380391
ETOOMANYREFS
381392
EUSERS
382393
EVFILT_AIO
@@ -1915,6 +1926,8 @@ endpwent
19151926
endservent
19161927
endutxent
19171928
erand48
1929+
ether_addr
1930+
ether_header
19181931
eui64_aton
19191932
eui64_hostton
19201933
eui64_ntoa

src/unix/bsd/freebsdlike/freebsd/mod.rs

+109
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,18 @@ s_no_extra_traits! {
16771677
pub uc_flags: c_int,
16781678
__spare__: [c_int; 4],
16791679
}
1680+
1681+
#[repr(packed)]
1682+
pub struct ether_header {
1683+
pub ether_dhost: [crate::u_char; ETHER_ADDR_LEN as usize],
1684+
pub ether_shost: [crate::u_char; ETHER_ADDR_LEN as usize],
1685+
pub ether_type: crate::u_short,
1686+
}
1687+
1688+
#[repr(packed)]
1689+
pub struct ether_addr {
1690+
pub octet: [crate::u_char; ETHER_ADDR_LEN as usize],
1691+
}
16801692
}
16811693

16821694
cfg_if! {
@@ -2557,6 +2569,65 @@ cfg_if! {
25572569
.finish()
25582570
}
25592571
}
2572+
2573+
// FIXME(msrv): `derive` on packed structs cannot be used below 1.67
2574+
2575+
impl PartialEq for ether_header {
2576+
fn eq(&self, other: &ether_header) -> bool {
2577+
self.ether_dhost
2578+
.iter()
2579+
.zip(other.ether_dhost.iter())
2580+
.all(|(a, b)| a == b)
2581+
&& self
2582+
.ether_dhost
2583+
.iter()
2584+
.zip(other.ether_shost.iter())
2585+
.all(|(a, b)| a == b)
2586+
&& self.ether_type == other.ether_type
2587+
}
2588+
}
2589+
2590+
impl Eq for ether_header {}
2591+
impl fmt::Debug for ether_header {
2592+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2593+
f.debug_struct("ether_header")
2594+
.field("ether_dhost", &{ self.ether_dhost })
2595+
.field("ether_shost", &{ self.ether_shost })
2596+
.field("ether_type", &{ self.ether_type })
2597+
.finish()
2598+
}
2599+
}
2600+
2601+
impl hash::Hash for ether_header {
2602+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
2603+
{ self.ether_dhost }.hash(state);
2604+
{ self.ether_shost }.hash(state);
2605+
{ self.ether_type }.hash(state);
2606+
}
2607+
}
2608+
2609+
impl PartialEq for ether_addr {
2610+
fn eq(&self, other: &ether_addr) -> bool {
2611+
self.octet
2612+
.iter()
2613+
.zip(other.octet.iter())
2614+
.all(|(a, b)| a == b)
2615+
}
2616+
}
2617+
2618+
impl Eq for ether_addr {}
2619+
impl fmt::Debug for ether_addr {
2620+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2621+
f.debug_struct("ether_addr")
2622+
.field("octet", &{ self.octet })
2623+
.finish()
2624+
}
2625+
}
2626+
impl hash::Hash for ether_addr {
2627+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
2628+
{ self.octet }.hash(state);
2629+
}
2630+
}
25602631
}
25612632
}
25622633

@@ -4663,6 +4734,16 @@ pub const RTM_VERSION: c_int = 5;
46634734

46644735
pub const RTAX_MAX: c_int = 8;
46654736

4737+
// net/ethernet.h
4738+
4739+
pub const ETHER_ADDR_LEN: c_int = 6;
4740+
pub const ETHER_TYPE_LEN: c_int = 2;
4741+
pub const ETHER_CRC_LEN: c_int = 4;
4742+
pub const ETHER_HDR_LEN: c_int = ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN;
4743+
pub const ETHER_MIN_LEN: c_int = 64;
4744+
pub const ETHER_MAX_LEN: c_int = 1518;
4745+
pub const ETHER_MAX_LEN_JUMBO: c_int = 9018;
4746+
46664747
// sys/signal.h
46674748
pub const SIGTHR: c_int = 32;
46684749
pub const SIGLWP: c_int = SIGTHR;
@@ -4973,6 +5054,34 @@ f! {
49735054
pub fn PROT_MAX_EXTRACT(x: c_int) -> c_int {
49745055
(x >> 16) & (crate::PROT_READ | crate::PROT_WRITE | crate::PROT_EXEC)
49755056
}
5057+
5058+
pub {const} fn ETHER_IS_MULTICAST(addr: *mut u_char) -> bool {
5059+
(*addr.add(0)) & 0x01 != 0x00
5060+
}
5061+
5062+
pub {const} fn ETHER_IS_IPV6_MULTICAST(addr: *mut u_char) -> bool {
5063+
(*addr.add(0)) == 0x33 && (*addr.add(1)) == 0x33
5064+
}
5065+
5066+
pub {const} fn ETHER_IS_BROADCAST(addr: *mut u_char) -> bool {
5067+
(*addr.add(0))
5068+
& (*addr.add(1))
5069+
& (*addr.add(2))
5070+
& (*addr.add(3))
5071+
& (*addr.add(4))
5072+
& (*addr.add(5))
5073+
== 0xff
5074+
}
5075+
5076+
pub {const} fn ETHER_IS_ZERO(addr: *mut u_char) -> bool {
5077+
(*addr.add(0))
5078+
| (*addr.add(1))
5079+
| (*addr.add(2))
5080+
| (*addr.add(3))
5081+
| (*addr.add(4))
5082+
| (*addr.add(5))
5083+
== 0x00
5084+
}
49765085
}
49775086

49785087
safe_f! {

0 commit comments

Comments
 (0)