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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use byteorder::{BigEndian, LittleEndian};
use crate::{
Endianness,
errors::*,
pcap::Packet,
pcap::PcapHeader,
peek_reader::PeekReader
};
use std::io::Read;
#[derive(Debug)]
pub struct PcapReader<T: Read> {
pub header: PcapHeader,
reader: PeekReader<T>
}
impl <T:Read> PcapReader<T>{
pub fn new(mut reader:T) -> ResultParsing<PcapReader<T>> {
Ok(
PcapReader {
header : PcapHeader::from_reader(&mut reader)?,
reader : PeekReader::new(reader)
}
)
}
pub fn into_reader(self) -> T{
self.reader.inner
}
pub fn get_ref(&self) -> &T{
&self.reader.inner
}
pub fn get_mut(&mut self) -> &mut T{
&mut self.reader.inner
}
}
impl <T:Read> Iterator for PcapReader<T> {
type Item = ResultParsing<Packet<'static>>;
fn next(&mut self) -> Option<ResultParsing<Packet<'static>>> {
match self.reader.is_empty() {
Ok(is_empty) if is_empty => {
return None;
},
Err(err) => return Some(Err(err.into())),
_ => {}
}
let ts_resolution = self.header.ts_resolution();
Some(
match self.header.endianness() {
Endianness::Big => Packet::from_reader::<_, BigEndian>(&mut self.reader, ts_resolution),
Endianness::Little => Packet::from_reader::<_, LittleEndian>(&mut self.reader, ts_resolution)
}
)
}
}