1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::io::Read;
use super::blocks::block_common::{Block, RawBlock};
use super::blocks::enhanced_packet::EnhancedPacketBlock;
use super::blocks::interface_description::InterfaceDescriptionBlock;
use super::blocks::section_header::SectionHeaderBlock;
use super::PcapNgParser;
use crate::errors::PcapError;
use crate::read_buffer::ReadBuffer;
pub struct PcapNgReader<R: Read> {
parser: PcapNgParser,
reader: ReadBuffer<R>,
}
impl<R: Read> PcapNgReader<R> {
pub fn new(reader: R) -> Result<PcapNgReader<R>, PcapError> {
let mut reader = ReadBuffer::new(reader);
let parser = reader.parse_with(PcapNgParser::new)?;
Ok(Self { parser, reader })
}
pub fn next_block(&mut self) -> Option<Result<Block, PcapError>> {
match self.reader.has_data_left() {
Ok(has_data) => {
if has_data {
Some(self.reader.parse_with(|src| self.parser.next_block(src)))
}
else {
None
}
},
Err(e) => Some(Err(PcapError::IoError(e))),
}
}
pub fn next_raw_block(&mut self) -> Option<Result<RawBlock, PcapError>> {
match self.reader.has_data_left() {
Ok(has_data) => {
if has_data {
Some(self.reader.parse_with(|src| self.parser.next_raw_block(src)))
}
else {
None
}
},
Err(e) => Some(Err(PcapError::IoError(e))),
}
}
pub fn section(&self) -> &SectionHeaderBlock<'static> {
self.parser.section()
}
pub fn interfaces(&self) -> &[InterfaceDescriptionBlock<'static>] {
self.parser.interfaces()
}
pub fn packet_interface(&self, packet: &EnhancedPacketBlock) -> Option<&InterfaceDescriptionBlock> {
self.interfaces().get(packet.interface_id as usize)
}
pub fn into_inner(self) -> R {
self.reader.into_inner()
}
pub fn get_ref(&self) -> &R {
self.reader.get_ref()
}
}