Skip to main content

L2Receiver

Struct L2Receiver 

Source
pub struct L2Receiver { /* private fields */ }
Expand description

Receiver for raw L2 Ethernet frames via AF_PACKET socket.

Implementations§

Source§

impl L2Receiver

Source

pub fn new(ifname: &str) -> Result<Self, IoError>

Open an AF_PACKET socket and bind to the given interface.

Examples found in repository?
examples/packet_dump.rs (line 14)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    use wireforge_io::{L2Receiver, IoError};
12
13    let ifname = std::env::args().nth(1).unwrap_or_else(|| "lo".into());
14    let mut rx = L2Receiver::new(&ifname)?;
15    println!("Listening on {}...", ifname);
16
17    loop {
18        match rx.recv() {
19            Ok(eth) => {
20                println!(
21                    "{} -> {}  EtherType={:?}  payload={} bytes",
22                    hex_mac(&eth.src_mac()),
23                    hex_mac(&eth.dst_mac()),
24                    eth.ethertype(),
25                    eth.payload().len(),
26                );
27            }
28            Err(IoError::Timeout) => continue,
29            Err(e) => {
30                eprintln!("Error: {}", e);
31                break;
32            }
33        }
34    }
35    Ok(())
36}
More examples
Hide additional examples
examples/cross_platform_dump.rs (line 26)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18    let ifname = std::env::args().nth(1).unwrap_or_else(|| {
19        list_interfaces()
20            .ok()
21            .and_then(|ifaces| ifaces.into_iter().find(|i| !i.is_loopback && i.is_up).map(|i| i.name))
22            .unwrap_or_else(|| "lo".into())
23    });
24
25    println!("Opening {} ...", ifname);
26    let mut rx = DefaultL2Reader::new(&ifname)?;
27    rx.set_timeout(Some(Duration::from_secs(1)))?;
28
29    println!("Listening on {} (Ctrl+C to stop)...\n", ifname);
30    for i in 0..10 {
31        match rx.recv() {
32            Ok(eth) => {
33                println!("#{}: {} → {}  EtherType={:?}  len={}",
34                    i + 1,
35                    mac_hex(&eth.src_mac()),
36                    mac_hex(&eth.dst_mac()),
37                    eth.ethertype(),
38                    eth.payload().len() + 14,
39                );
40            }
41            Err(wireforge_io::IoError::Timeout) => continue,
42            Err(e) => { eprintln!("Error: {e}"); break; }
43        }
44    }
45    Ok(())
46}
Source

pub fn recv(&mut self) -> Result<EthernetPacket<'_>, IoError>

Receive a single Ethernet frame and parse it.

Examples found in repository?
examples/packet_dump.rs (line 18)
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11    use wireforge_io::{L2Receiver, IoError};
12
13    let ifname = std::env::args().nth(1).unwrap_or_else(|| "lo".into());
14    let mut rx = L2Receiver::new(&ifname)?;
15    println!("Listening on {}...", ifname);
16
17    loop {
18        match rx.recv() {
19            Ok(eth) => {
20                println!(
21                    "{} -> {}  EtherType={:?}  payload={} bytes",
22                    hex_mac(&eth.src_mac()),
23                    hex_mac(&eth.dst_mac()),
24                    eth.ethertype(),
25                    eth.payload().len(),
26                );
27            }
28            Err(IoError::Timeout) => continue,
29            Err(e) => {
30                eprintln!("Error: {}", e);
31                break;
32            }
33        }
34    }
35    Ok(())
36}
More examples
Hide additional examples
examples/cross_platform_dump.rs (line 31)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18    let ifname = std::env::args().nth(1).unwrap_or_else(|| {
19        list_interfaces()
20            .ok()
21            .and_then(|ifaces| ifaces.into_iter().find(|i| !i.is_loopback && i.is_up).map(|i| i.name))
22            .unwrap_or_else(|| "lo".into())
23    });
24
25    println!("Opening {} ...", ifname);
26    let mut rx = DefaultL2Reader::new(&ifname)?;
27    rx.set_timeout(Some(Duration::from_secs(1)))?;
28
29    println!("Listening on {} (Ctrl+C to stop)...\n", ifname);
30    for i in 0..10 {
31        match rx.recv() {
32            Ok(eth) => {
33                println!("#{}: {} → {}  EtherType={:?}  len={}",
34                    i + 1,
35                    mac_hex(&eth.src_mac()),
36                    mac_hex(&eth.dst_mac()),
37                    eth.ethertype(),
38                    eth.payload().len() + 14,
39                );
40            }
41            Err(wireforge_io::IoError::Timeout) => continue,
42            Err(e) => { eprintln!("Error: {e}"); break; }
43        }
44    }
45    Ok(())
46}
Source

pub fn set_promiscuous(&mut self, on: bool) -> Result<(), IoError>

Enable or disable promiscuous mode.

Source

pub fn set_timeout(&mut self, dur: Option<Duration>) -> Result<(), IoError>

Set receive timeout.

Examples found in repository?
examples/cross_platform_dump.rs (line 27)
17fn main() -> Result<(), Box<dyn std::error::Error>> {
18    let ifname = std::env::args().nth(1).unwrap_or_else(|| {
19        list_interfaces()
20            .ok()
21            .and_then(|ifaces| ifaces.into_iter().find(|i| !i.is_loopback && i.is_up).map(|i| i.name))
22            .unwrap_or_else(|| "lo".into())
23    });
24
25    println!("Opening {} ...", ifname);
26    let mut rx = DefaultL2Reader::new(&ifname)?;
27    rx.set_timeout(Some(Duration::from_secs(1)))?;
28
29    println!("Listening on {} (Ctrl+C to stop)...\n", ifname);
30    for i in 0..10 {
31        match rx.recv() {
32            Ok(eth) => {
33                println!("#{}: {} → {}  EtherType={:?}  len={}",
34                    i + 1,
35                    mac_hex(&eth.src_mac()),
36                    mac_hex(&eth.dst_mac()),
37                    eth.ethertype(),
38                    eth.payload().len() + 14,
39                );
40            }
41            Err(wireforge_io::IoError::Timeout) => continue,
42            Err(e) => { eprintln!("Error: {e}"); break; }
43        }
44    }
45    Ok(())
46}

Trait Implementations§

Source§

impl L2Reader for L2Receiver

Source§

fn recv(&mut self) -> Result<EthernetPacket<'_>, IoError>

Receive a single Ethernet frame and parse it.
Source§

fn set_timeout(&mut self, dur: Option<Duration>) -> Result<(), IoError>

Set the receive timeout. None means blocking (no timeout).
Source§

fn set_promiscuous(&mut self, on: bool) -> Result<(), IoError>

Enable or disable promiscuous mode.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.