pub struct L2Receiver { /* private fields */ }Expand description
Receiver for raw L2 Ethernet frames via AF_PACKET socket.
Implementations§
Source§impl L2Receiver
impl L2Receiver
Sourcepub fn new(ifname: &str) -> Result<Self, IoError>
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(ð.src_mac()),
23 hex_mac(ð.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
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(ð.src_mac()),
36 mac_hex(ð.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}Sourcepub fn recv(&mut self) -> Result<EthernetPacket<'_>, IoError>
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(ð.src_mac()),
23 hex_mac(ð.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
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(ð.src_mac()),
36 mac_hex(ð.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}Sourcepub fn set_promiscuous(&mut self, on: bool) -> Result<(), IoError>
pub fn set_promiscuous(&mut self, on: bool) -> Result<(), IoError>
Enable or disable promiscuous mode.
Sourcepub fn set_timeout(&mut self, dur: Option<Duration>) -> Result<(), IoError>
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(ð.src_mac()),
36 mac_hex(ð.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§
Auto Trait Implementations§
impl Freeze for L2Receiver
impl RefUnwindSafe for L2Receiver
impl Send for L2Receiver
impl Sync for L2Receiver
impl Unpin for L2Receiver
impl UnsafeUnpin for L2Receiver
impl UnwindSafe for L2Receiver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more