Skip to content

Commit b8215f3

Browse files
devnexentgross35
authored andcommitted
freebsd add basic ethernet frames support
[reference](https://github.com/freebsd/freebsd-src/blob/stable/14/sys/net/ethernet.h)
1 parent 2324acf commit b8215f3

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

libc-test/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,6 +2392,7 @@ fn test_freebsd(target: &str) {
23922392
"memstat.h",
23932393
"mqueue.h",
23942394
"net/bpf.h",
2395+
"net/ethernet.h",
23952396
"net/if.h",
23962397
"net/if_arp.h",
23972398
"net/if_dl.h",

libc-test/semver/freebsd.txt

Lines changed: 13 additions & 0 deletions
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
@@ -1917,6 +1928,8 @@ endpwent
19171928
endservent
19181929
endutxent
19191930
erand48
1931+
ether_addr
1932+
ether_header
19201933
eui64_aton
19211934
eui64_hostton
19221935
eui64_ntoa

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,18 @@ s_no_extra_traits! {
17221722
pub uc_flags: c_int,
17231723
__spare__: [c_int; 4],
17241724
}
1725+
1726+
#[repr(packed)]
1727+
pub struct ether_header {
1728+
pub ether_dhost: [crate::u_char; ETHER_ADDR_LEN as usize],
1729+
pub ether_shost: [crate::u_char; ETHER_ADDR_LEN as usize],
1730+
pub ether_type: crate::u_short,
1731+
}
1732+
1733+
#[repr(packed)]
1734+
pub struct ether_addr {
1735+
pub octet: [crate::u_char; ETHER_ADDR_LEN as usize],
1736+
}
17251737
}
17261738

17271739
cfg_if! {
@@ -2602,6 +2614,65 @@ cfg_if! {
26022614
.finish()
26032615
}
26042616
}
2617+
2618+
// FIXME(msrv): `derive` on packed structs cannot be used below 1.67
2619+
2620+
impl PartialEq for ether_header {
2621+
fn eq(&self, other: &ether_header) -> bool {
2622+
self.ether_dhost
2623+
.iter()
2624+
.zip(other.ether_dhost.iter())
2625+
.all(|(a, b)| a == b)
2626+
&& self
2627+
.ether_dhost
2628+
.iter()
2629+
.zip(other.ether_shost.iter())
2630+
.all(|(a, b)| a == b)
2631+
&& self.ether_type == other.ether_type
2632+
}
2633+
}
2634+
2635+
impl Eq for ether_header {}
2636+
impl fmt::Debug for ether_header {
2637+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2638+
f.debug_struct("ether_header")
2639+
.field("ether_dhost", &{ self.ether_dhost })
2640+
.field("ether_shost", &{ self.ether_shost })
2641+
.field("ether_type", &{ self.ether_type })
2642+
.finish()
2643+
}
2644+
}
2645+
2646+
impl hash::Hash for ether_header {
2647+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
2648+
{ self.ether_dhost }.hash(state);
2649+
{ self.ether_shost }.hash(state);
2650+
{ self.ether_type }.hash(state);
2651+
}
2652+
}
2653+
2654+
impl PartialEq for ether_addr {
2655+
fn eq(&self, other: &ether_addr) -> bool {
2656+
self.octet
2657+
.iter()
2658+
.zip(other.octet.iter())
2659+
.all(|(a, b)| a == b)
2660+
}
2661+
}
2662+
2663+
impl Eq for ether_addr {}
2664+
impl fmt::Debug for ether_addr {
2665+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2666+
f.debug_struct("ether_addr")
2667+
.field("octet", &{ self.octet })
2668+
.finish()
2669+
}
2670+
}
2671+
impl hash::Hash for ether_addr {
2672+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
2673+
{ self.octet }.hash(state);
2674+
}
2675+
}
26052676
}
26062677
}
26072678

@@ -4704,6 +4775,16 @@ pub const RTM_VERSION: c_int = 5;
47044775

47054776
pub const RTAX_MAX: c_int = 8;
47064777

4778+
// net/ethernet.h
4779+
4780+
pub const ETHER_ADDR_LEN: c_int = 6;
4781+
pub const ETHER_TYPE_LEN: c_int = 2;
4782+
pub const ETHER_CRC_LEN: c_int = 4;
4783+
pub const ETHER_HDR_LEN: c_int = ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN;
4784+
pub const ETHER_MIN_LEN: c_int = 64;
4785+
pub const ETHER_MAX_LEN: c_int = 1518;
4786+
pub const ETHER_MAX_LEN_JUMBO: c_int = 9018;
4787+
47074788
// sys/signal.h
47084789
pub const SIGTHR: c_int = 32;
47094790
pub const SIGLWP: c_int = SIGTHR;
@@ -5012,6 +5093,34 @@ f! {
50125093
pub fn PROT_MAX_EXTRACT(x: c_int) -> c_int {
50135094
(x >> 16) & (crate::PROT_READ | crate::PROT_WRITE | crate::PROT_EXEC)
50145095
}
5096+
5097+
pub {const} fn ETHER_IS_MULTICAST(addr: *mut u_char) -> bool {
5098+
(*addr.add(0)) & 0x01 != 0x00
5099+
}
5100+
5101+
pub {const} fn ETHER_IS_IPV6_MULTICAST(addr: *mut u_char) -> bool {
5102+
(*addr.add(0)) == 0x33 && (*addr.add(1)) == 0x33
5103+
}
5104+
5105+
pub {const} fn ETHER_IS_BROADCAST(addr: *mut u_char) -> bool {
5106+
(*addr.add(0))
5107+
& (*addr.add(1))
5108+
& (*addr.add(2))
5109+
& (*addr.add(3))
5110+
& (*addr.add(4))
5111+
& (*addr.add(5))
5112+
== 0xff
5113+
}
5114+
5115+
pub {const} fn ETHER_IS_ZERO(addr: *mut u_char) -> bool {
5116+
(*addr.add(0))
5117+
| (*addr.add(1))
5118+
| (*addr.add(2))
5119+
| (*addr.add(3))
5120+
| (*addr.add(4))
5121+
| (*addr.add(5))
5122+
== 0x00
5123+
}
50155124
}
50165125

50175126
safe_f! {

0 commit comments

Comments
 (0)