Skip to main content

pcap_file/pcap/
errors.rs

1use std::time::Duration;
2
3use thiserror::Error;
4
5/* ----- PcapError ----- */
6
7/// Convenience error type that wraps all errors that can occur while parsing,
8/// reading, writing, or validating a pcap file.
9#[derive(Debug, Error)]
10pub enum PcapError {
11    /// Error while parsing pcap data.
12    #[error(transparent)]
13    Parse(#[from] PcapParseError),
14    /// Error while reading pcap data from an I/O source.
15    #[error(transparent)]
16    Read(#[from] PcapReadError),
17    /// Error while writing pcap data.
18    #[error(transparent)]
19    Write(#[from] PcapWriteError),
20    /// Validation error for decoded or encoded pcap content.
21    #[error(transparent)]
22    Validation(#[from] PcapValidationError),
23}
24
25/* ----- enum PcapParseError ----- */
26
27/// Errors that can occur while parsing typed pcap data.
28#[derive(Debug, Error)]
29pub enum PcapParseError {
30    /// The buffer is too small to parse the expected data.
31    /// # Fields
32    /// - 0: needed size to parse the data
33    /// - 1: actual size of the buffer
34    #[error("The buffer too small: need {0}B, got {1}B")]
35    IncompleteBuffer(usize, usize),
36
37    /// A field of the pcap file is invalid.
38    #[error(transparent)]
39    Validation(#[from] PcapValidationError),
40}
41
42/* ----- enum PcapReadError ----- */
43
44/// Errors that can occur while reading pcap data from an I/O source.
45#[derive(Debug, Error)]
46pub enum PcapReadError {
47    /// An I/O error occurred while reading the pcap.
48    #[error("I/O error while reading the pcap")]
49    Io(#[source] std::io::Error),
50    /// A field of the pcap file is invalid.
51    #[error(transparent)]
52    Validation(#[from] PcapValidationError),
53}
54
55/* ----- enum PcapWriteError ----- */
56
57/// Errors that can occur while writing pcap data.
58#[derive(Debug, Error)]
59pub enum PcapWriteError {
60    /// An I/O error occurred while writing the pcap stream.
61    #[error("I/O error while writing the pcap")]
62    Io(#[source] std::io::Error),
63    /// An I/O error occurred while writing a field in the file.
64    /// # Fields
65    /// - 0: field that failed to be written
66    /// - 1: underlying I/O error
67    #[error("I/O error while writing the field {0}")]
68    FieldWriteFailed(&'static str, #[source] std::io::Error),
69    /// A field of the pcap file is invalid.
70    #[error(transparent)]
71    Validation(#[from] PcapValidationError),
72}
73
74/* ----- enum PcapValidationError ----- */
75
76/// Errors that can occur while validating a pcap file.
77#[derive(Debug, Error)]
78pub enum PcapValidationError {
79    /// The magic number of the pcap header is invalid.
80    #[error("Invalid magic number: {0:#X}")]
81    InvalidMagicNumber(u32),
82    /// The fractional timestamp part is too large for microsecond resolution.
83    #[error("Ts_frac micro too big: {0} >= 1_000_000 us")]
84    TsFracMicroTooBig(u32),
85    /// The fractional timestamp part is too large for nanosecond resolution.
86    #[error("Ts_frac nano too big: {0} >= 1_000_000_000 ns")]
87    TsFracNanoTooBig(u32),
88    /// The timestamp is too large to be represented in a 32-bit seconds field.
89    #[error("Timestamp too big: {0:?} > 2^32 seconds")]
90    TimestampTooBig(Duration),
91    /// The captured packet length is larger than the file snaplen.
92    #[error("included_len > snap_len: {0} > {1}")]
93    IncludedLenTooBig(u32, u32),
94    /// The original packet length is smaller than the captured packet length.
95    #[error("origin_len < included_len: {0} < {1}")]
96    OriginLenTooSmall(u32, u32),
97    /// The packet data length is larger than `u32::MAX`.
98    #[error("data length too big: {0} > u32::MAX")]
99    DataTooBig(usize),
100    /// The packet length is larger than the file snaplen.
101    #[error("Packet length > snap_len: {0} > {1}")]
102    PacketLenTooBig(u32, u32),
103}