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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
extern crate bincode;
extern crate pcap_file;
extern crate serde;
#[macro_use]
extern crate serde_derive;
use pcap_file::errors::ResultChain;
use pcap_file::*;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::io::BufWriter;
use std::io::Result as IoResult;
use std::io::SeekFrom;
trait PcapReaderSeek {
fn tell(&mut self) -> IoResult<u64>;
fn seek(&mut self, offset: u64) -> IoResult<u64>;
}
impl<T> PcapReaderSeek for PcapReader<T>
where
T: Read + Seek,
{
fn tell(&mut self) -> IoResult<u64> {
self.get_mut().seek(SeekFrom::Current(0))
}
fn seek(&mut self, offset: u64) -> IoResult<u64> {
self.get_mut().seek(SeekFrom::Start(offset))
}
}
#[derive(Debug, Serialize, Deserialize)]
struct PacketOffsets {
inner: Vec<u64>,
}
impl PacketOffsets {
pub fn from_pcap(pcap: &str) -> ResultChain<Self> {
let file = File::open(pcap)?;
let mut pcap_reader = PcapReader::new(BufReader::new(file))?;
let mut inner: Vec<u64> = Vec::new();
loop {
let tell = pcap_reader.tell()?;
match pcap_reader.next() {
Some(_) => {
inner.push(tell);
continue;
}
None => break,
}
}
Ok(PacketOffsets { inner })
}
pub fn save_to(&self, offset_path: &str) -> bincode::Result<()> {
let file = File::create(offset_path)?;
let buf = BufWriter::new(file);
bincode::serialize_into(buf, self)
}
pub fn load_from(offset_path: &str) -> bincode::Result<PacketOffsets> {
let file = File::open(offset_path)?;
let buf = BufReader::new(file);
bincode::deserialize_from(buf)
}
}
#[derive(Debug)]
pub struct PcapReaderIndex {
inner: PcapReader<BufReader<File>>,
offsets: PacketOffsets,
pcap_path: String,
offset_path: String,
}
impl PcapReaderIndex {
pub fn new_full_control(
pcap_path: &str,
offset_path: &str,
recalc_offset: bool,
save_offset_file: bool,
) -> Result<PcapReaderIndex, Box<Error>> {
let offsets = if recalc_offset {
PacketOffsets::from_pcap(pcap_path)?
} else {
PacketOffsets::load_from(offset_path)?
};
if save_offset_file {
offsets.save_to(offset_path)?;
}
Ok(PcapReaderIndex {
pcap_path: pcap_path.to_owned(),
offset_path: offset_path.to_owned(),
inner: PcapReader::new(BufReader::new(File::open(&pcap_path)?))?,
offsets: offsets,
})
}
pub fn from_pcap(pcap_path: &str) -> Result<PcapReaderIndex, Box<Error>> {
let offset_path = Self::default_offset_path(pcap_path);
let res = Self::new_full_control(pcap_path, &offset_path, false, false);
if res.is_err() {
Self::new_full_control(pcap_path, &offset_path, true, true)
} else {
res
}
}
pub fn default_offset_path(pcap_path: &str) -> String {
format!("{}.offset.bincode", pcap_path)
}
pub fn get(&mut self, index: usize) -> Option<ResultChain<Packet<'static>>> {
if index >= self.offsets.inner.len() {
return None;
}
let offset = self.offsets.inner[index];
let seek_result = self.inner.seek(offset);
if let Err(_) = seek_result {
return None;
}
self.inner.next()
}
pub fn len(&self) -> usize {
self.offsets.inner.len()
}
}
impl Iterator for PcapReaderIndex {
type Item = ResultChain<Packet<'static>>;
fn next(&mut self) -> Option<ResultChain<Packet<'static>>> {
self.inner.next()
}
}
#[cfg(test)]
mod tests {
use *;
const PCAP_PATH: &str = "tests/test_in.pcap";
mod pcap_reader_seek {
use self::tests::*;
use PcapReaderSeek;
use *;
#[test]
fn compare_with_calculated_offsets() {
let file = File::open(PCAP_PATH).unwrap();
let mut pcap_reader = PcapReader::new(file).unwrap();
let mut calculated_offset = 24;
loop {
let offset = pcap_reader.tell().unwrap();
assert_eq!(offset, calculated_offset);
if let Some(Ok(pkt)) = pcap_reader.next() {
calculated_offset += 16 + pkt.header.incl_len as u64;
} else {
break;
}
}
}
}
mod packet_offsets {
use self::tests::*;
use *;
#[test]
fn methods() {
const OFFSET_PATH: &str = "tests/OFFSET_for_methods";
let _ = std::fs::remove_file(OFFSET_PATH);
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
let offsets = PacketOffsets::from_pcap(PCAP_PATH).expect("Error opening pcap file");
assert_eq!(
offsets.inner,
vec![24, 157, 442, 528, 614, 708, 941, 1027, 1221, 1319]
);
offsets.save_to(OFFSET_PATH).unwrap();
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), true);
let load = PacketOffsets::load_from(OFFSET_PATH).unwrap();
assert_eq!(offsets.inner, load.inner);
std::fs::remove_file(OFFSET_PATH).unwrap();
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
}
}
mod pcap_reader_index {
use self::tests::*;
use *;
fn asserts_for_pcap(pcap: &mut PcapReaderIndex) {
assert_eq!(pcap.len(), 10);
assert_eq!(pcap.get(0).unwrap().unwrap().header.incl_len, 117);
assert_eq!(pcap.get(9).unwrap().unwrap().header.incl_len, 120);
assert_eq!(pcap.get(3).unwrap().unwrap().header.incl_len, 70);
assert!(pcap.get(10).is_none());
}
#[test]
fn new() {
const OFFSET_PATH: &str = "tests/OFFSET_for_new";
let _ = std::fs::remove_file(OFFSET_PATH);
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
let pcap = PcapReaderIndex::new_full_control(PCAP_PATH, OFFSET_PATH, false, false);
assert_eq!(pcap.is_err(), true);
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
let mut pcap = PcapReaderIndex::new_full_control(PCAP_PATH, OFFSET_PATH, true, true).unwrap();
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), true);
asserts_for_pcap(&mut pcap);
let mut pcap = PcapReaderIndex::new_full_control(PCAP_PATH, OFFSET_PATH, false, false).unwrap();
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), true);
asserts_for_pcap(&mut pcap);
std::fs::remove_file(OFFSET_PATH).unwrap();
assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
}
#[test]
fn from_pcap() {
let offset_path = PcapReaderIndex::default_offset_path(PCAP_PATH);
let _ = std::fs::remove_file(&offset_path);
assert_eq!(std::path::Path::new(&offset_path).exists(), false);
let mut pcap = PcapReaderIndex::from_pcap(PCAP_PATH).unwrap();
assert_eq!(std::path::Path::new(&offset_path).exists(), true);
asserts_for_pcap(&mut pcap);
let mut pcap = PcapReaderIndex::from_pcap(PCAP_PATH).unwrap();
assert_eq!(std::path::Path::new(&offset_path).exists(), true);
asserts_for_pcap(&mut pcap);
let _ = std::fs::remove_file(&offset_path);
assert_eq!(std::path::Path::new(&offset_path).exists(), false);
}
#[test]
fn iterator() {
const OFFSET_PATH: &str = "tests/OFFSET_for_iterator";
let pcap = PcapReaderIndex::new_full_control(PCAP_PATH, OFFSET_PATH, true, true).unwrap();
let incl_lens: Vec<_> = pcap.map(|p| p.unwrap().header.incl_len).collect();
assert_eq!(incl_lens, vec![117, 269, 70, 70, 78, 217, 70, 178, 82, 120]);
std::fs::remove_file(OFFSET_PATH).unwrap();
}
}
}