use core::fmt;
use core::str::FromStr;
use std::io;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MacAddr(pub [u8; 6]);
impl MacAddr {
pub const BROADCAST: MacAddr = MacAddr([0xff; 6]);
pub const fn octets(&self) -> [u8; 6] {
self.0
}
pub const fn is_multicast(&self) -> bool {
self.0[0] & 1 == 1
}
}
impl fmt::Display for MacAddr {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let [a, b, c, d, e, g] = self.0;
write!(f, "{a:02x}:{b:02x}:{c:02x}:{d:02x}:{e:02x}:{g:02x}")
}
}
impl FromStr for MacAddr {
type Err = io::Error;
fn from_str(s: &str) -> io::Result<MacAddr> {
let bad = || io::Error::new(io::ErrorKind::InvalidInput, format!("not a MAC address: {s:?}"));
let mut out = [0u8; 6];
let mut parts = s.split([':', '-']);
for slot in &mut out {
let part = parts.next().ok_or_else(bad)?;
if part.is_empty() || part.len() > 2 {
return Err(bad());
}
*slot = u8::from_str_radix(part, 16).map_err(|_| bad())?;
}
if parts.next().is_some() {
return Err(bad());
}
Ok(MacAddr(out))
}
}
impl From<[u8; 6]> for MacAddr {
fn from(octets: [u8; 6]) -> MacAddr {
MacAddr(octets)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum TimestampSource {
Hardware,
Software,
User,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Timestamp {
pub secs: i64,
pub nanos: u32,
pub source: TimestampSource,
}
impl Timestamp {
pub fn as_nanos(&self) -> i128 {
i128::from(self.secs) * 1_000_000_000 + i128::from(self.nanos)
}
}
#[derive(Debug)]
pub struct Packet<'a> {
pub(crate) data: &'a [u8],
pub(crate) original_len: usize,
pub(crate) timestamp: Option<Timestamp>,
pub(crate) vlan_restored: bool,
}
impl<'a> Packet<'a> {
pub fn data(&self) -> &'a [u8] {
self.data
}
pub fn original_len(&self) -> usize {
self.original_len
}
pub fn is_truncated(&self) -> bool {
self.data.len() < self.original_len
}
pub fn timestamp(&self) -> Option<Timestamp> {
self.timestamp
}
pub fn frame(&self) -> Option<crate::Frame<'a>> {
crate::Frame::new(self.data)
}
pub fn vlan_restored(&self) -> bool {
self.vlan_restored
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Stats {
pub received: u64,
pub dropped: u64,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TxErrors {
pub missed: u64,
pub invalid: u64,
}
impl TxErrors {
pub fn total(&self) -> u64 {
self.missed + self.invalid
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RxPath {
Ring,
Plain,
Bpf,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mac_addresses_parse_with_either_separator_and_print_with_colons() {
let m: MacAddr = "01-0C-cd-01-00-05".parse().expect("parse");
assert_eq!(m, MacAddr([1, 0x0c, 0xcd, 1, 0, 5]));
assert_eq!(m.to_string(), "01:0c:cd:01:00:05");
assert!(m.is_multicast());
assert_eq!(
"aa:bb:cc:dd:ee:ff".parse::<MacAddr>().expect("parse"),
MacAddr([0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff])
);
for bad in ["", "01:02:03:04:05", "01:02:03:04:05:06:07", "001:02:03:04:05:06", "zz:02:03:04:05:06"] {
assert!(bad.parse::<MacAddr>().is_err(), "{bad}");
}
}
#[test]
fn a_timestamp_counts_nanoseconds() {
let t = Timestamp { secs: 2, nanos: 5, source: TimestampSource::Software };
assert_eq!(t.as_nanos(), 2_000_000_005);
}
}