Skip to main content

wireforge_io/
lib.rs

1//! WireForge IO — cross-platform raw socket I/O for packet capture and injection.
2//!
3//! # Platform Support
4//!
5//! | Platform | L2 Capture | L3 Capture | Backend |
6//! |----------|------------|------------|---------|
7//! | Linux    | AF_PACKET  | raw socket | socket2 + libc |
8//! | macOS    | BPF        | raw socket | /dev/bpf + libc |
9//! | Windows  | NPcap      | raw socket | npcap crate |
10//!
11//! # Quick Start
12//!
13//! ```rust,no_run
14//! # use wireforge_io::{DefaultL2Reader, traits::L2Reader};
15//! # fn main() -> Result<(), wireforge_io::IoError> {
16//! let mut rx = DefaultL2Reader::new("eth0")?;
17//! let eth = rx.recv()?;
18//! println!("EtherType: {:?}", eth.ethertype());
19//! # Ok(())
20//! # }
21//! ```
22//!
23//! # Permissions
24//!
25//! L2 capture requires root (Linux/macOS) or Administrator (Windows).
26//! `list_interfaces()` does not require elevated privileges.
27
28mod common;
29mod error;
30pub mod traits;
31
32#[cfg(target_os = "linux")]
33pub mod linux;
34#[cfg(target_os = "macos")]
35pub mod macos;
36#[cfg(target_os = "windows")]
37pub mod windows;
38
39mod interface;
40
41pub use error::IoError;
42pub use interface::{list_interfaces, Interface};
43
44// Re-export existing concrete types from their new platform homes
45#[cfg(target_os = "linux")]
46pub use linux::{L2Receiver, L2Sender, L3Receiver, L3Sender};
47
48// Platform-adaptive type aliases — use these in cross-platform code.
49// Each alias resolves to the appropriate platform implementation at compile time.
50
51/// The default L2 frame reader for the current platform.
52/// - Linux: [`linux::L2Receiver`] (AF_PACKET)
53/// - macOS: [`macos::BpfReader`] (BPF)
54/// - Windows: [`windows::NpcapReader`] (NPcap)
55#[cfg(target_os = "linux")]
56pub type DefaultL2Reader = linux::L2Receiver;
57#[cfg(target_os = "macos")]
58pub type DefaultL2Reader = macos::BpfReader;
59#[cfg(target_os = "windows")]
60pub type DefaultL2Reader = windows::NpcapReader;
61
62/// The default L2 frame sender for the current platform.
63#[cfg(target_os = "linux")]
64pub type DefaultL2Writer = linux::L2Sender;
65#[cfg(target_os = "macos")]
66pub type DefaultL2Writer = macos::BpfWriter;
67#[cfg(target_os = "windows")]
68pub type DefaultL2Writer = windows::NpcapWriter;
69
70/// The default L3 IPv4 packet reader for the current platform.
71/// - Linux: [`linux::L3Receiver`] (raw socket)
72/// - macOS: [`macos::BsdL3Receiver`] (raw socket)
73/// - Windows: [`windows::WinL3Receiver`] (raw socket)
74#[cfg(target_os = "linux")]
75pub type DefaultL3Reader = linux::L3Receiver;
76#[cfg(target_os = "macos")]
77pub type DefaultL3Reader = macos::BsdL3Receiver;
78#[cfg(target_os = "windows")]
79pub type DefaultL3Reader = windows::WinL3Receiver;
80
81/// The default L3 IPv4 packet sender for the current platform.
82#[cfg(target_os = "linux")]
83pub type DefaultL3Writer = linux::L3Sender;
84#[cfg(target_os = "macos")]
85pub type DefaultL3Writer = macos::BsdL3Sender;
86#[cfg(target_os = "windows")]
87pub type DefaultL3Writer = windows::WinL3Sender;