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    /// 设置为 128MB,为高性能场景预留空间
21    pub const MAX_BUFFER_SIZE: usize = 128 * 1024 * 1024; // 128MB
22
23    /// 默认文件命名格式
24    pub const DEFAULT_FILE_NAME_FORMAT: &str =
25        "yyMMdd_HHmmss_fffffff";
26}
27
28/// 错误代码枚举
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum PcapErrorCode {
31    /// 未知错误
32    Unknown = 0,
33    /// 文件未找到
34    FileNotFound = 1001,
35    /// 目录不存在
36    DirectoryNotFound = 1002,
37    /// 无效的文件格式
38    InvalidFormat = 2001,
39    /// 文件头损坏
40    CorruptedHeader = 2002,
41    /// 数据包损坏
42    CorruptedData = 2003,
43    /// 校验和不匹配
44    ChecksumMismatch = 2004,
45    /// 数据包大小无效
46    InvalidPacketSize = 3001,
47    /// 数据包长度超出文件剩余空间
48    PacketSizeExceedsRemainingBytes = 3002,
49    /// 时间戳解析错误
50    TimestampParseError = 3003,
51    /// 参数无效
52    InvalidArgument = 3004,
53    /// 操作状态无效
54    InvalidState = 3005,
55}
56
57impl std::fmt::Display for PcapErrorCode {
58    fn fmt(
59        &self,
60        f: &mut std::fmt::Formatter<'_>,
61    ) -> std::fmt::Result {
62        match self {
63            PcapErrorCode::Unknown => write!(f, "未知错误"),
64            PcapErrorCode::FileNotFound => {
65                write!(f, "文件未找到")
66            }
67            PcapErrorCode::DirectoryNotFound => {
68                write!(f, "目录不存在")
69            }
70            PcapErrorCode::InvalidFormat => {
71                write!(f, "无效的文件格式")
72            }
73            PcapErrorCode::CorruptedHeader => {
74                write!(f, "文件头损坏")
75            }
76            PcapErrorCode::CorruptedData => {
77                write!(f, "数据包损坏")
78            }
79            PcapErrorCode::ChecksumMismatch => {
80                write!(f, "校验和不匹配")
81            }
82            PcapErrorCode::InvalidPacketSize => {
83                write!(f, "数据包大小无效")
84            }
85            PcapErrorCode::PacketSizeExceedsRemainingBytes => {
86                write!(f, "数据包长度超出文件剩余空间")
87            }
88            PcapErrorCode::TimestampParseError => {
89                write!(f, "时间戳解析错误")
90            }
91            PcapErrorCode::InvalidArgument => {
92                write!(f, "参数无效")
93            }
94            PcapErrorCode::InvalidState => {
95                write!(f, "操作状态无效")
96            }
97        }
98    }
99}