Skip to main content

wireforge_io/
error.rs

1/// I/O error type for wireforge-io.
2#[derive(Debug)]
3pub enum IoError {
4    /// Underlying socket error.
5    Socket(std::io::Error),
6    /// Packet parse failure.
7    Parse(&'static str),
8    /// Operation timed out.
9    Timeout,
10    /// Invalid pcap magic number.
11    InvalidMagic(u32),
12    /// Unsupported pcap/pcapng version.
13    UnsupportedVersion { major: u16, minor: u16 },
14    /// Truncated pcap record.
15    TruncatedRecord { expected: u32, got: usize },
16    /// DNS name label exceeds 63 bytes.
17    NameTooLong(usize),
18    /// DNS name compression pointer loop detected.
19    CompressionLoop,
20    /// Specified interface does not exist.
21    InterfaceNotFound(String),
22    /// Permission denied (root or Administrator required).
23    PermissionDenied,
24    /// BPF-specific error (macOS/BSD).
25    Bpf(&'static str),
26    /// NPcap-specific error (Windows).
27    Npcap(String),
28    /// Operation not supported on the current platform.
29    UnsupportedPlatform,
30}
31
32impl core::fmt::Display for IoError {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        match self {
35            Self::Socket(e) => write!(f, "socket error: {e}"),
36            Self::Parse(msg) => write!(f, "parse error: {msg}"),
37            Self::Timeout => write!(f, "timeout"),
38            Self::InvalidMagic(m) => write!(f, "invalid pcap magic number: 0x{m:08X}"),
39            Self::UnsupportedVersion { major, minor } => {
40                write!(f, "unsupported pcap version: {major}.{minor}")
41            }
42            Self::TruncatedRecord { expected, got } => {
43                write!(f, "truncated pcap record: expected {expected} bytes, got {got}")
44            }
45            Self::NameTooLong(len) => write!(f, "DNS name label too long: {len} bytes (max 63)"),
46            Self::CompressionLoop => write!(f, "DNS name compression loop detected"),
47            Self::InterfaceNotFound(name) => write!(f, "interface not found: {name}"),
48            Self::PermissionDenied => write!(f, "permission denied (root/Administrator required)"),
49            Self::Bpf(msg) => write!(f, "BPF error: {msg}"),
50            Self::Npcap(msg) => write!(f, "NPcap error: {msg}"),
51            Self::UnsupportedPlatform => write!(f, "operation not supported on this platform"),
52        }
53    }
54}
55
56impl std::error::Error for IoError {}
57
58impl From<std::io::Error> for IoError {
59    fn from(e: std::io::Error) -> Self {
60        IoError::Socket(e)
61    }
62}