Skip to main content

wireforge_io/
traits.rs

1//! Platform-independent I/O traits for L2/L3 packet capture and injection.
2//!
3//! Each platform provides its own implementation of these traits:
4//! - Linux: `L2Receiver`/`L2Sender` (AF_PACKET), `L3Receiver`/`L3Sender` (raw socket)
5//! - macOS: `BpfReader`/`BpfWriter` (BPF), `BsdL3Receiver`/`BsdL3Sender` (raw socket)
6//! - Windows: `NpcapReader`/`NpcapWriter` (NPcap), `WinL3Receiver`/`WinL3Sender` (raw socket)
7
8use std::net::SocketAddrV4;
9use std::time::Duration;
10
11use wireforge_core::ether::EthernetPacket;
12use wireforge_core::ipv4::Ipv4Packet;
13
14use crate::error::IoError;
15
16/// Receiver for raw L2 Ethernet frames.
17///
18/// Implementations internally manage a receive buffer (typically 64 KB)
19/// and return zero-copy parsed `EthernetPacket` references.
20pub trait L2Reader {
21    /// Receive a single Ethernet frame and parse it.
22    fn recv(&mut self) -> Result<EthernetPacket<'_>, IoError>;
23
24    /// Set the receive timeout. `None` means blocking (no timeout).
25    fn set_timeout(&mut self, dur: Option<Duration>) -> Result<(), IoError>;
26
27    /// Enable or disable promiscuous mode.
28    fn set_promiscuous(&mut self, on: bool) -> Result<(), IoError>;
29}
30
31/// Sender for raw L2 Ethernet frames.
32pub trait L2Writer {
33    /// Send raw bytes as an Ethernet frame. Returns the number of bytes sent.
34    fn send(&self, packet: &[u8]) -> Result<usize, IoError>;
35}
36
37/// Receiver for raw L3 IPv4 packets.
38pub trait L3Reader {
39    /// Receive a single IPv4 packet and parse it.
40    fn recv(&mut self) -> Result<Ipv4Packet<'_>, IoError>;
41
42    /// Set the receive timeout. `None` means blocking (no timeout).
43    fn set_timeout(&mut self, dur: Option<Duration>) -> Result<(), IoError>;
44
45    /// Enable or disable IP_HDRINCL (header included in send).
46    fn set_header_included(&mut self, on: bool) -> Result<(), IoError>;
47}
48
49/// Sender for raw L3 IPv4 packets.
50pub trait L3Writer {
51    /// Send an IPv4 packet to the given destination.
52    fn send_to(&self, packet: &[u8], dst: SocketAddrV4) -> Result<usize, IoError>;
53}