1use std::{fmt::Display, time::Duration};
2
3use thiserror::Error;
4
5use crate::{
6 DataLink,
7 pcapng::blocks::{block_name, interface_description::InterfaceTsResolution},
8};
9
10#[derive(Debug, Error)]
14pub enum PcapNgError {
15 #[error(transparent)]
17 Parse(#[from] PcapNgParseError),
18 #[error(transparent)]
20 Read(#[from] PcapNgReadError),
21 #[error(transparent)]
23 Write(#[from] PcapNgWriteError),
24 #[error(transparent)]
26 BlockValidation(#[from] ContentValidationError),
27 #[error(transparent)]
29 StateUpdate(#[from] StateUpdateError),
30}
31
32#[derive(Debug, Error)]
36pub enum PcapNgParseError {
37 #[error("The buffer is too small: need {0}B, got {1}B")]
42 IncompleteBuffer(usize, usize),
43
44 #[error("Invalid raw block format")]
46 InvalidFormat(#[from] PcapNgFormatError),
47
48 #[error(transparent)]
50 BlockConversion(#[from] BlockConversionError),
51
52 #[error(transparent)]
54 StateUpdate(#[from] StateUpdateError),
55}
56
57impl From<RawBlockParseError> for PcapNgParseError {
58 fn from(value: RawBlockParseError) -> Self {
59 match value {
60 RawBlockParseError::IncompleteBuffer(needed, actual) => Self::IncompleteBuffer(needed, actual),
61 RawBlockParseError::InvalidFormat(format) => Self::InvalidFormat(format),
62 }
63 }
64}
65
66#[derive(Debug, Error)]
70pub enum PcapNgReadError {
71 #[error("I/O error while reading the pcapng")]
73 Io(#[source] std::io::Error),
74
75 #[error("Invalid raw block format")]
77 InvalidFormat(#[from] PcapNgFormatError),
78
79 #[error(transparent)]
81 BlockConversion(#[from] BlockConversionError),
82
83 #[error(transparent)]
85 StateUpdate(#[from] StateUpdateError),
86}
87
88impl From<PcapNgParseError> for PcapNgReadError {
89 fn from(value: PcapNgParseError) -> Self {
90 match value {
91 PcapNgParseError::IncompleteBuffer(_, _) => {
92 Self::Io(std::io::Error::from(std::io::ErrorKind::UnexpectedEof))
93 }
94 PcapNgParseError::InvalidFormat(e) => Self::InvalidFormat(e),
95 PcapNgParseError::BlockConversion(e) => Self::BlockConversion(e),
96 PcapNgParseError::StateUpdate(e) => Self::StateUpdate(e),
97 }
98 }
99}
100
101#[derive(Debug, Error)]
105pub enum PcapNgWriteError {
106 #[error("I/O error during writing")]
108 Io(#[from] std::io::Error),
109
110 #[error("Field `{field}` failed validation during writing")]
112 Validation {
113 field: &'static str,
115 source: Box<ContentValidationError>,
118 },
119
120 #[error("Invalid raw block format")]
122 InvalidFormat(#[from] PcapNgFormatError),
123
124 #[error("State update error during writing")]
126 StateUpdate(#[from] StateUpdateError),
127}
128
129impl PcapNgWriteError {
130 pub(crate) fn validation_error(field: &'static str, source: ContentValidationError) -> Self {
132 Self::Validation {
133 field,
134 source: Box::new(source),
135 }
136 }
137}
138
139#[derive(Debug, Error)]
143pub enum PcapNgFormatError {
144 #[error("The section header is missing")]
146 MissingSectionHeader,
147 #[error("Invalid magic number: {0:#X}")]
149 InvalidMagicNumber(u32),
150 #[error("Block length is not a multiple of 4: {0}B")]
152 BlockNotAligned(usize),
153 #[error("Block is too short: minimum {0}B, actual {1}B")]
158 BlockTooShort(usize, usize),
159 #[error("Block length fields don't match: initial {0}B, trailing {1}B")]
164 BlockLengthMismatch(u32, u32),
165 #[error("Block length doesn't match body length: expected {expected}B, got {actual}B")]
167 InvalidBlockLength {
168 expected: usize,
170 actual: u32,
172 },
173}
174
175#[derive(Debug, Error)]
179pub enum RawBlockParseError {
180 #[error("The buffer is too small: need {0}B, got {1}B")]
185 IncompleteBuffer(usize, usize),
186
187 #[error("Invalid raw block format")]
189 InvalidFormat(#[from] PcapNgFormatError),
190}
191
192#[derive(Debug, Error)]
196pub struct BlockConversionError {
197 pub type_: u32,
199 pub source: Box<BlockContentParseError>,
202}
203
204impl Display for BlockConversionError {
205 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
206 write!(
207 f,
208 "Invalid content for block '{}' ({:#X})",
209 block_name(self.type_),
210 self.type_
211 )
212 }
213}
214
215#[derive(Debug, Error)]
219pub enum BlockContentParseError {
220 #[error("Block content too small: need {needed}B, got {actual}B")]
222 BlockContentTooSmall {
223 needed: usize,
225 actual: usize,
227 },
228
229 #[error(transparent)]
231 Validation(#[from] ContentValidationError),
232
233 #[error("Error parsing the options")]
235 Option(#[from] OptionParseError),
236}
237
238#[derive(Debug, Error)]
242pub enum StateUpdateError {
243 #[error("Failed to convert raw block to update the pcapng state")]
245 BlockConversion(#[from] BlockConversionError),
246}
247
248#[derive(Debug, Error)]
252pub enum ContentValidationError {
253 #[error("Invalid magic number: {0:#X}")]
255 InvalidMagicNumber(u32),
256 #[error("Invalid reserved field: {0}")]
258 InvalidReservedField(u16),
259 #[error("Invalid timestamp resolution: {0:#X} (is_bin: {1}, resol:{2})")]
261 InvalidTsResolution(u8, bool, u8),
262 #[error("Invalid Linktype: {0:?}")]
264 InvalidLinktype(DataLink),
265
266 #[error("Section without any interface")]
268 NoInterface,
269 #[error("Invalid interface ID: {0}")]
271 InvalidInterfaceId(u32),
272
273 #[error(
275 "Failed to decode timestamp (high: {timestamp_high:#X}, low: {timestamp_low:#X}, combined: {}) with resolution {resolution} and offset {offset}s",
276 ((*timestamp_high as u64) << 32) | *timestamp_low as u64
277 )]
278 FailedToDecodeTimestamp {
279 timestamp_high: u32,
281 timestamp_low: u32,
283 resolution: InterfaceTsResolution,
285 offset: i64,
287 },
288
289 #[error("Failed to encode timestamp {timestamp:?} with resolution {resolution} and offset {offset}s")]
291 FailedToEncodeTimestamp {
292 timestamp: Duration,
294 resolution: InterfaceTsResolution,
296 offset: i64,
298 },
299
300 #[error("The original length of the packet is lower than its actual length: {0}B on wire, {1}B captured")]
302 InvalidOriginalLen(u32, usize),
303
304 #[error("Invalid captured length: expected {expected}B, got {actual}B")]
306 InvalidCapturedLen {
307 expected: usize,
309 actual: usize,
311 },
312
313 #[error("Wrong record size: expected {expected}B, got {actual}B")]
315 RecordWrongSize {
316 expected: usize,
318 actual: usize,
320 },
321 #[error("Wrong record minimum size: expected at least {min}B, got {actual}B")]
323 RecordWrongMinSize {
324 min: usize,
326 actual: usize,
328 },
329 #[error("Record length doesn't fit on a u16: {0}B")]
331 RecordTooBig(usize),
332 #[error("A record name is not valid UTF-8")]
334 RecordNameNotUtf8(#[source] std::str::Utf8Error),
335 #[error("Record without any name")]
337 RecordNamesEmpty,
338
339 #[error("Error in custom block conversion for PEN {0}: {1}")]
341 CustomBlockConversionError(u32, Box<dyn std::error::Error + Sync + Send>),
342
343 #[error("Block content doesn't fit on a u32: {0}B")]
345 BlockContentTooBig(u64),
346
347 #[error("Option content doesn't fit on a u16: {0}B")]
349 OptionTooBig(usize),
350}
351
352#[derive(Debug, Error)]
356pub enum OptionParseError {
357 #[error("The option field is too small to parse the options: need {needed}B, got {actual}B")]
359 ContentTooSmall {
360 needed: usize,
362 actual: usize,
364 },
365 #[error("Invalid option entry. Code: {code}, Name: {name}")]
367 InvalidEntry {
368 code: u16,
370 name: &'static str,
372 source: Box<OptionEntryError>,
375 },
376}
377
378#[derive(Debug, Error)]
382pub enum OptionEntryError {
383 #[error("Wrong entry size: expected {expected}B, got {actual}B")]
385 WrongSize {
386 expected: usize,
388 actual: usize,
390 },
391 #[error("Invalid UTF-8 format")]
393 InvalidUtf8(#[from] std::str::Utf8Error),
394 #[error(transparent)]
396 Validation(#[from] ContentValidationError),
397}