#[derive(Clone, Debug)]
pub struct PacketStatistics {
pub(crate) received: u32,
pub(crate) dropped: u32,
}
impl PacketStatistics {
#[inline]
pub fn received(&self) -> u32 {
self.received
}
#[inline]
pub fn dropped(&self) -> u32 {
self.dropped
}
}
#[repr(C)]
pub struct BpfProgram {
#[cfg(target_os = "windows")]
bf_len: u32,
#[cfg(not(target_os = "windows"))]
bf_len: u16,
bf_insns: *mut BpfInstruction,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct BpfInstruction {
pub code: libc::c_ushort,
pub jt: libc::c_uchar,
pub jf: libc::c_uchar,
pub k: libc::c_uint,
}
#[derive(Clone)]
pub struct PacketFilter {
filter: Vec<BpfInstruction>,
}
impl PacketFilter {
#[inline]
pub fn from_vec(filter: Vec<BpfInstruction>) -> Self {
Self { filter }
}
#[inline]
pub unsafe fn as_bpf_program(&mut self) -> BpfProgram {
#[cfg(target_os = "windows")]
let bf_len = self.filter.len() as u32;
#[cfg(not(target_os = "windows"))]
let bf_len = self.filter.len() as u16;
BpfProgram {
bf_len,
bf_insns: self.filter.as_mut_ptr(),
}
}
#[inline]
pub fn accept_all() -> Self {
Self {
filter: vec![BpfInstruction {
code: 0x06,
jt: 0,
jf: 0,
k: 0xffffffff,
}],
}
}
pub fn is_accept_all(&self) -> bool {
match self.filter.first() {
None => false,
Some(inst) => {
self.filter.len() == 1
&& inst.code == 0x06
&& inst.jt == 0
&& inst.jf == 0
&& inst.k == 0xffffffff
}
}
}
#[inline]
pub fn reject_all() -> Self {
Self {
filter: vec![BpfInstruction {
code: 0x06,
jt: 0,
jf: 0,
k: 0,
}],
}
}
}