Struct LegacyPcapReader

Source
pub struct LegacyPcapReader<R>
where R: Read,
{ /* private fields */ }
Expand description

Parsing iterator over legacy pcap data (streaming version)

§Pcap Reader

This reader is a streaming parser based on a circular buffer, which means memory usage is constant, and that it can be used to parse huge files or infinite streams. It creates an abstraction over any input providing the Read trait, and takes care of managing the circular buffer to provide an iterator-like interface.

The first call to next will return the file header. Some information of this header must be stored (for ex. the data link type) to be able to parse following block contents. Following calls to next will always return legacy data blocks.

The size of the circular buffer has to be big enough for at least one complete block. Using a larger value (at least 65k) is advised to avoid frequent reads and buffer shifts.

There are precautions to take when reading multiple blocks before consuming data. See PcapReaderIterator for details.

§Example

use pcap_parser::*;
use pcap_parser::traits::PcapReaderIterator;
use std::fs::File;

let file = File::open(path).unwrap();
let mut num_blocks = 0;
let mut reader = LegacyPcapReader::new(65536, file).expect("LegacyPcapReader");
loop {
    match reader.next() {
        Ok((offset, block)) => {
            println!("got new block");
            num_blocks += 1;
            match block {
                PcapBlockOwned::LegacyHeader(_hdr) => {
                    // save hdr.network (linktype)
                },
                PcapBlockOwned::Legacy(_b) => {
                    // use linktype to parse b.data()
                },
                PcapBlockOwned::NG(_) => unreachable!(),
            }
            reader.consume(offset);
        },
        Err(PcapError::Eof) => break,
        Err(PcapError::Incomplete(_)) => {
            reader.refill().unwrap();
        },
        Err(e) => panic!("error while reading: {:?}", e),
    }
}
println!("num_blocks: {}", num_blocks);

Implementations§

Source§

impl<R> LegacyPcapReader<R>
where R: Read,

Source

pub fn new( capacity: usize, reader: R, ) -> Result<LegacyPcapReader<R>, PcapError<&'static [u8]>>

Creates a new LegacyPcapReader<R> with the provided buffer capacity.

Source

pub fn from_buffer( buffer: Buffer, reader: R, ) -> Result<LegacyPcapReader<R>, PcapError<&'static [u8]>>

Creates a new LegacyPcapReader<R> using the provided Buffer.

Trait Implementations§

Source§

impl<R> PcapReaderIterator for LegacyPcapReader<R>
where R: Read,

Source§

fn next(&mut self) -> Result<(usize, PcapBlockOwned<'_>), PcapError<&[u8]>>

Get the next pcap block, if possible. Returns the number of bytes read and the block. Read more
Source§

fn consume(&mut self, offset: usize)

Consume data, and shift buffer if needed. Read more
Source§

fn consume_noshift(&mut self, offset: usize)

Consume date, but do not change the buffer. Blocks already read are still valid.
Source§

fn consumed(&self) -> usize

Get the number of consumed bytes
Source§

fn refill(&mut self) -> Result<(), PcapError<&[u8]>>

Refill the internal buffer, shifting it if necessary. Read more
Source§

fn position(&self) -> usize

Get the position in the internal buffer. Can be used to determine if refill is required.
Source§

fn grow(&mut self, new_size: usize) -> bool

Grow size of the internal buffer.
Source§

fn data(&self) -> &[u8]

Returns a slice with all the available data
Source§

fn reader_exhausted(&self) -> bool

Returns true if underlying reader is exhausted Read more

Auto Trait Implementations§

§

impl<R> Freeze for LegacyPcapReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for LegacyPcapReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for LegacyPcapReader<R>
where R: Send,

§

impl<R> Sync for LegacyPcapReader<R>
where R: Sync,

§

impl<R> Unpin for LegacyPcapReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for LegacyPcapReader<R>
where R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.