pcapfile_io/foundation/
types.rs

1//! 公共类型和常量定义
2//!
3//! 定义整个库使用的通用类型和常量,为所有层提供基础数据类型支持。
4
5/// PCAP格式常量定义
6pub mod constants {
7    /// PCAP文件标识,固定值 0xD4C3B2A1
8    pub const PCAP_MAGIC_NUMBER: u32 = 0xD4C3B2A1;
9
10    /// 主版本号,固定值 0x0002
11    pub const MAJOR_VERSION: u16 = 2;
12
13    /// 次版本号,固定值 0x0004,表示支持纳秒级时间量
14    pub const MINOR_VERSION: u16 = 4;
15
16    /// 每个PCAP文件最大数据包数量
17    pub const DEFAULT_MAX_PACKETS_PER_FILE: usize = 500;
18
19    /// 最大缓冲区大小(字节)
20    pub const MAX_BUFFER_SIZE: usize = 50 * 1024 * 1024; // 50MB
21
22    /// 默认文件命名格式
23    pub const DEFAULT_FILE_NAME_FORMAT: &str =
24        "yyMMdd_HHmmss_fffffff";
25}
26
27/// 错误代码枚举
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum PcapErrorCode {
30    /// 未知错误
31    Unknown = 0,
32    /// 文件未找到
33    FileNotFound = 1001,
34    /// 目录不存在
35    DirectoryNotFound = 1002,
36    /// 无效的文件格式
37    InvalidFormat = 2001,
38    /// 文件头损坏
39    CorruptedHeader = 2002,
40    /// 数据包损坏
41    CorruptedData = 2003,
42    /// 校验和不匹配
43    ChecksumMismatch = 2004,
44    /// 数据包大小无效
45    InvalidPacketSize = 3001,
46    /// 数据包长度超出文件剩余空间
47    PacketSizeExceedsRemainingBytes = 3002,
48    /// 时间戳解析错误
49    TimestampParseError = 3003,
50    /// 参数无效
51    InvalidArgument = 3004,
52    /// 操作状态无效
53    InvalidState = 3005,
54}
55
56impl std::fmt::Display for PcapErrorCode {
57    fn fmt(
58        &self,
59        f: &mut std::fmt::Formatter<'_>,
60    ) -> std::fmt::Result {
61        match self {
62            PcapErrorCode::Unknown => write!(f, "未知错误"),
63            PcapErrorCode::FileNotFound => {
64                write!(f, "文件未找到")
65            }
66            PcapErrorCode::DirectoryNotFound => {
67                write!(f, "目录不存在")
68            }
69            PcapErrorCode::InvalidFormat => {
70                write!(f, "无效的文件格式")
71            }
72            PcapErrorCode::CorruptedHeader => {
73                write!(f, "文件头损坏")
74            }
75            PcapErrorCode::CorruptedData => {
76                write!(f, "数据包损坏")
77            }
78            PcapErrorCode::ChecksumMismatch => {
79                write!(f, "校验和不匹配")
80            }
81            PcapErrorCode::InvalidPacketSize => {
82                write!(f, "数据包大小无效")
83            }
84            PcapErrorCode::PacketSizeExceedsRemainingBytes => {
85                write!(f, "数据包长度超出文件剩余空间")
86            }
87            PcapErrorCode::TimestampParseError => {
88                write!(f, "时间戳解析错误")
89            }
90            PcapErrorCode::InvalidArgument => {
91                write!(f, "参数无效")
92            }
93            PcapErrorCode::InvalidState => {
94                write!(f, "操作状态无效")
95            }
96        }
97    }
98}