1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#![no_std]

#[cfg(any(
    target_os = "macos",
    target_os = "freebsd",
    target_os = "linux",
    target_os = "android"
))]
pub mod unix;

#[cfg(any(target_os = "macos", target_os = "freebsd"))]
pub mod bsd;

#[cfg(any(target_os = "linux", target_os = "android"))]
pub mod linux;

pub mod homeplug;

mod ether_addr;
mod ether_type;
mod oui;

use core::time::Duration;

pub use ether_addr::*;
pub use ether_type::*;
pub use oui::*;

pub trait EtherSocket: core::fmt::Debug {
    type Error: core::fmt::Debug + core::fmt::Display;
    fn sendto(&mut self, destination: EtherAddr, data: &[u8]) -> Result<(), Self::Error>;
    fn recvfrom<'a>(
        &mut self,
        buffer: &'a mut [u8],
        timeout: Option<Duration>,
    ) -> Result<Option<(EtherAddr, &'a [u8])>, Self::Error>;
}

pub trait EtherInterface: core::fmt::Debug {
    type Error: core::fmt::Debug + core::fmt::Display;
    type Socket: EtherSocket<Error = Self::Error>;
    fn open(&self, ethertype: EtherType) -> Result<Self::Socket, Self::Error>;
    fn name(&self) -> &str;
    fn address(&self) -> EtherAddr;
    fn is_up(&self) -> bool;
    fn is_loopback(&self) -> bool;
}

#[cfg(any(target_os = "macos", target_os = "freebsd"))]
pub fn platform_interfaces(
) -> Result<impl Iterator<Item = impl EtherInterface>, impl core::fmt::Debug> {
    bsd::BsdBpfInterface::interfaces()
}

#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn platform_interfaces(
) -> Result<impl Iterator<Item = impl EtherInterface>, impl core::fmt::Debug> {
    linux::LinuxInterface::interfaces()
}