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
//! # pcap-file-ra
//! 
//! The crate provides random access to the underlying `PcapReader`.
//!
//! ## Examples
//!
//! ```
//! use pcap_file_ra::PcapReaderIndex;
//!
//! let mut pcap = PcapReaderIndex::from_pcap("tests/test_in.pcap").unwrap();
//!
//! // offset file is created:
//! assert_eq!(std::path::Path::new("tests/test_in.pcap.offset.bincode").exists(), true);
//!
//! 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());
//! ```

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;

/// Extension methods for `PcapReader`.
/// 
/// It is used by `PacketOffsets`.
trait PcapReaderSeek {
    /// Returns the current offset
    fn tell(&mut self) -> IoResult<u64>;

    /// Seeks to the specified offset
    fn seek(&mut self, offset: u64) -> IoResult<u64>;
}

/// Extension methods for `PcapReader` when the underlying `Reader` also provides `Seek` trait.
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))
    }
}

/// Stores packet offsets included within the pcap file
#[derive(Debug, Serialize, Deserialize)]
struct PacketOffsets {
    inner: Vec<u64>,
}

impl PacketOffsets {
    /// Creates offsets for nth packets of pcap file.
    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 })
    }

    /// Save offsets into file
    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)
    }

    /// Load offsets from file
    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)
    }
}

/// `PcapReader` that support random access
///
/// It wraps `PcapReader`, and creates 'offset' file if necccesary.
///
/// For the time being, the path of the offset file is determined automatically.
#[derive(Debug)]
pub struct PcapReaderIndex {
    /// Underlying reader
    inner: PcapReader<BufReader<File>>,
    /// Stores offsets for each packet
    offsets: PacketOffsets,
    /// path for the pcap file
    pcap_path: String,
    /// path for the offset file
    offset_path: String,
}

impl PcapReaderIndex {
    /// Creates the struct with full control.
    pub fn new_full_control(
        pcap_path: &str,
        offset_path: &str,
        recalc_offset: bool,
        save_offset_file: bool,
    ) -> Result<PcapReaderIndex, Box<Error>> {
        // TODO: add timestamp check for offset file?

        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,
        })
    }

    /// Creates the struct with the default offset file name.
    /// If offset file is already created, reuse it.
    ///
    /// ## Example
    ///
    /// ```
    /// let pcap = pcap_file_ra::PcapReaderIndex::from_pcap("tests/test_in.pcap").unwrap();
    ///
    /// assert_eq!(std::path::Path::new("tests/test_in.pcap.offset.bincode").exists(), true);
    /// ```
    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
        }
    }

    /// Returns the default offset path for the given `pcap_path`.
    /// By default, offset file name is created by just adding ".offset.bincode" suffix.
    pub fn default_offset_path(pcap_path: &str) -> String {
        format!("{}.offset.bincode", pcap_path)
    }

    /// Returns the Packet at the specified `index`.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut pcap = pcap_file_ra::PcapReaderIndex::from_pcap("tests/test_in.pcap").unwrap();
    ///
    /// 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());
    /// ```
    pub fn get(&mut self, index: usize) -> Option<ResultChain<Packet<'static>>> {
        if index >= self.offsets.inner.len() {
            return None; // out of range
        }
        let offset = self.offsets.inner[index];
        let seek_result = self.inner.seek(offset);
        if let Err(_) = seek_result {
            return None;
        }
        self.inner.next()
    }

    /// Returns the number of packets.
    ///
    /// ## Example
    ///
    /// ```
    /// let mut pcap = pcap_file_ra::PcapReaderIndex::from_pcap("tests/test_in.pcap").unwrap();
    ///
    /// assert_eq!(pcap.len(), 10);
    /// ```
    pub fn len(&self) -> usize {
        self.offsets.inner.len()
    }
}

/// Wraps the underlying `PcapReader`'s `Iterator`
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; // provides .tell()
        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; // initial value == size of pcap file header
            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"; // must use dedicated file

            // Setup
            let _ = std::fs::remove_file(OFFSET_PATH); // remove if it already exists
            assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);

            // Create
            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]
            );

            // Save
            offsets.save_to(OFFSET_PATH).unwrap();
            assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), true);

            // Load
            let load = PacketOffsets::load_from(OFFSET_PATH).unwrap();
            assert_eq!(offsets.inner, load.inner);

            // Teardown
            std::fs::remove_file(OFFSET_PATH).unwrap(); // clean up
            assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
        }

    }

    mod pcap_reader_index {
        use self::tests::*;
        use *;

        /// asserts for test file
        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() {
            // Setup
            // We need to specify a didicated offset file here because tests are run concurrently.
            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);

            // 1st: Reuse offset file => fail
            let pcap = PcapReaderIndex::new_full_control(PCAP_PATH, OFFSET_PATH, false, false);
            assert_eq!(pcap.is_err(), true); // should fail because offset_path does not exist.
            assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);

            // 2nd: Create new offset file => success
            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);

            // 3rd: Reuse offset file => success
            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);

            // Teardown
            std::fs::remove_file(OFFSET_PATH).unwrap(); // clean up
            assert_eq!(std::path::Path::new(OFFSET_PATH).exists(), false);
        }

        #[test]
        fn from_pcap() {
            // setup
            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);

            // 1st run
            let mut pcap = PcapReaderIndex::from_pcap(PCAP_PATH).unwrap();
            assert_eq!(std::path::Path::new(&offset_path).exists(), true);
            asserts_for_pcap(&mut pcap);

            // 2nd run
            let mut pcap = PcapReaderIndex::from_pcap(PCAP_PATH).unwrap();
            assert_eq!(std::path::Path::new(&offset_path).exists(), true);
            asserts_for_pcap(&mut pcap);

            // teardown
            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]);

            // teardown
            std::fs::remove_file(OFFSET_PATH).unwrap();
        }
    }
}