Skip to content

Commit

Permalink
adding ethhdr type for linux/android for proper packet filtering.
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Jan 12, 2025
1 parent 4abcd81 commit 55f537d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc-test/semver/android.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3230,6 +3230,7 @@ epoll_create1
epoll_ctl
epoll_event
epoll_wait
ethhdr
eventfd
eventfd_read
eventfd_write
Expand Down
1 change: 1 addition & 0 deletions libc-test/semver/linux.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3722,6 +3722,7 @@ epoll_params
epoll_pwait
epoll_wait
erand48
ethhdr
eventfd
eventfd_read
eventfd_write
Expand Down
35 changes: 35 additions & 0 deletions src/unix/linux_like/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub type __u16 = c_ushort;
pub type __s16 = c_short;
pub type __u32 = c_uint;
pub type __s32 = c_int;
pub type __be16 = __u16;

// linux/elf.h

Expand Down Expand Up @@ -638,6 +639,13 @@ s_no_extra_traits! {
pub ifc_len: c_int,
pub ifc_ifcu: __c_anonymous_ifc_ifcu,
}

#[repr(C)]
pub struct ethhdr {
pub h_dest: [c_uchar; crate::ETH_ALEN as usize],
pub h_source: [c_uchar; crate::ETH_ALEN as usize],
pub h_proto: crate::__be16,
}
}

cfg_if! {
Expand Down Expand Up @@ -1020,6 +1028,33 @@ cfg_if! {
.finish()
}
}

impl Eq for ethhdr {}

impl PartialEq for ethhdr {
fn eq(&self, other: &ethhdr) -> bool {
self.h_dest
.iter()
.zip(other.h_dest.iter())
.all(|(a, b)| a == b)
&& self
.h_source
.iter()
.zip(other.h_source.iter())
.all(|(a, b)| a == b)
&& self.h_proto == other.h_proto
}
}

impl fmt::Debug for ethhdr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ethhdr")
.field("h_dest", &self.h_dest)
.field("h_source", &self.h_source)
.field("h_proto", &{ self.h_proto })
.finish()
}
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/unix/linux_like/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub type __u16 = c_ushort;
pub type __s16 = c_short;
pub type __u32 = c_uint;
pub type __s32 = c_int;
pub type __be16 = __u16;

pub type Elf32_Half = u16;
pub type Elf32_Word = u32;
Expand Down Expand Up @@ -1784,6 +1785,13 @@ s_no_extra_traits! {
pub request: xsk_tx_metadata_request,
pub completion: xsk_tx_metadata_completion,
}

#[repr(C)]
pub struct ethhdr {
pub h_dest: [c_uchar; crate::ETH_ALEN as usize],
pub h_source: [c_uchar; crate::ETH_ALEN as usize],
pub h_proto: crate::__be16,
}
}

cfg_if! {
Expand Down Expand Up @@ -2211,6 +2219,33 @@ cfg_if! {
.finish()
}
}

impl Eq for ethhdr {}

impl PartialEq for ethhdr {
fn eq(&self, other: &ethhdr) -> bool {
self.h_dest
.iter()
.zip(other.h_dest.iter())
.all(|(a, b)| a == b)
&& self
.h_source
.iter()
.zip(other.h_source.iter())
.all(|(a, b)| a == b)
&& self.h_proto == other.h_proto
}
}

impl fmt::Debug for ethhdr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ethhdr")
.field("h_dest", &self.h_dest)
.field("h_source", &self.h_source)
.field("h_proto", &{ self.h_proto })
.finish()
}
}
}
}

Expand Down

0 comments on commit 55f537d

Please sign in to comment.