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;


/// Wraps another reader and uses it to read a Pcap formated stream.
///
/// It implements the Iterator trait in order to read one packet at a time
///
/// # Examples
///
/// ```rust,no_run
/// use std::fs::File;
/// use pcap_file::pcap::PcapReader;
///
/// let file_in = File::open("test.pcap").expect("Error opening file");
/// let pcap_reader = PcapReader::new(file_in).unwrap();
///
/// // Read test.pcap
/// for pcap in pcap_reader {
///
///     //Check if there is no error
///     let pcap = pcap.unwrap();
///
///     //Do something
/// }
/// ```
#[derive(Debug)]
pub struct PcapReader<T: Read> {

    pub header: PcapHeader,
    reader: PeekReader<T>
}

impl <T:Read> PcapReader<T>{

    /// Create a new PcapReader from an existing reader.
    /// This function read the global pcap header of the file to verify its integrity.
    ///
    /// The underlying reader must point to a valid pcap file/stream.
    ///
    /// # Errors
    /// Return an error if the data stream is not in a valid pcap file format.
    /// Or if the underlying data are not readable.
    ///
    /// # Examples
    /// ```rust,no_run
    /// use std::fs::File;
    /// use pcap_file::pcap::PcapReader;
    ///
    /// let file_in = File::open("test.pcap").expect("Error opening file");
    /// let pcap_reader = PcapReader::new(file_in).unwrap();
    /// ```
    pub fn new(mut reader:T) -> ResultParsing<PcapReader<T>> {

        Ok(
            PcapReader {

                header : PcapHeader::from_reader(&mut reader)?,
                reader : PeekReader::new(reader)
            }
        )
    }

    /// Consumes the `PcapReader`, returning the wrapped reader.
    pub fn into_reader(self) -> T{
        self.reader.inner
    }

    /// Gets a reference to the underlying reader.
    ///
    /// It is not advised to directly read from the underlying reader.
    pub fn get_ref(&self) -> &T{
        &self.reader.inner
    }

    /// Gets a mutable reference to the underlying reader.
    ///
    /// It is not advised to directly read from the underlying reader.
    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)
            }
        )
    }

}