pub struct PcapNgWriter<W: Write> { /* private fields */ }
Expand description

Writes a PcapNg to a writer.

Examples

use std::fs::File;

use pcap_file::pcapng::{PcapNgReader, PcapNgWriter};

let file_in = File::open("test.pcapng").expect("Error opening file");
let mut pcapng_reader = PcapNgReader::new(file_in).unwrap();

let mut out = Vec::new();
let mut pcapng_writer = PcapNgWriter::new(out).unwrap();

// Read test.pcapng
while let Some(block) = pcapng_reader.next_block() {
    // Check if there is no error
    let block = block.unwrap();

    // Write back parsed Block
    pcapng_writer.write_block(&block).unwrap();
}

Implementations§

source§

impl<W: Write> PcapNgWriter<W>

source

pub fn new(writer: W) -> PcapResult<Self>

Creates a new PcapNgWriter from an existing writer.

Defaults to the native endianness of the CPU.

Writes this global pcapng header to the file:

Self {
    endianness: Endianness::Native,
    major_version: 1,
    minor_version: 0,
    section_length: -1,
    options: vec![]
}
Errors

The writer can’t be written to.

source

pub fn with_endianness(writer: W, endianness: Endianness) -> PcapResult<Self>

Creates a new PcapNgWriter from an existing writer with the given endianness.

source

pub fn with_section_header(
    writer: W,
    section: SectionHeaderBlock<'static>
) -> PcapResult<Self>

Creates a new PcapNgWriter from an existing writer with the given section header.

source

pub fn write_block(&mut self, block: &Block<'_>) -> PcapResult<usize>

Writes a Block.

Example
use std::borrow::Cow;
use std::fs::File;
use std::time::Duration;

use pcap_file::pcapng::blocks::enhanced_packet::EnhancedPacketBlock;
use pcap_file::pcapng::blocks::interface_description::InterfaceDescriptionBlock;
use pcap_file::pcapng::{PcapNgBlock, PcapNgWriter};
use pcap_file::DataLink;

let data = [0u8; 10];

let interface = InterfaceDescriptionBlock {
    linktype: DataLink::ETHERNET,
    snaplen: 0xFFFF,
    options: vec![],
};

let packet = EnhancedPacketBlock {
    interface_id: 0,
    timestamp: Duration::from_secs(0),
    original_len: data.len() as u32,
    data: Cow::Borrowed(&data),
    options: vec![],
};

let file = File::create("out.pcap").expect("Error creating file");
let mut pcap_ng_writer = PcapNgWriter::new(file).unwrap();

pcap_ng_writer.write_block(&interface.into_block()).unwrap();
pcap_ng_writer.write_block(&packet.into_block()).unwrap();
source

pub fn write_pcapng_block<'a, B: PcapNgBlock<'a>>(
    &mut self,
    block: B
) -> PcapResult<usize>

Writes a PcapNgBlock.

Example
use std::borrow::Cow;
use std::fs::File;
use std::time::Duration;

use pcap_file::pcapng::blocks::enhanced_packet::EnhancedPacketBlock;
use pcap_file::pcapng::blocks::interface_description::InterfaceDescriptionBlock;
use pcap_file::pcapng::{PcapNgBlock, PcapNgWriter};
use pcap_file::DataLink;

let data = [0u8; 10];

let interface = InterfaceDescriptionBlock {
    linktype: DataLink::ETHERNET,
    snaplen: 0xFFFF,
    options: vec![],
};

let packet = EnhancedPacketBlock {
    interface_id: 0,
    timestamp: Duration::from_secs(0),
    original_len: data.len() as u32,
    data: Cow::Borrowed(&data),
    options: vec![],
};

let file = File::create("out.pcap").expect("Error creating file");
let mut pcap_ng_writer = PcapNgWriter::new(file).unwrap();

pcap_ng_writer.write_pcapng_block(interface).unwrap();
pcap_ng_writer.write_pcapng_block(packet).unwrap();
source

pub fn write_raw_block(&mut self, block: &RawBlock<'_>) -> PcapResult<usize>

Writes a RawBlock.

Doesn’t check the validity of the written blocks.

source

pub fn into_inner(self) -> W

Consumes Self, returning the wrapped writer.

source

pub fn get_ref(&self) -> &W

Gets a reference to the underlying writer.

source

pub fn get_mut(&mut self) -> &mut W

Gets a mutable reference to the underlying writer.

You should not be used unless you really know what you’re doing

source

pub fn section(&self) -> &SectionHeaderBlock<'static>

Returns the current SectionHeaderBlock.

source

pub fn interfaces(&self) -> &[InterfaceDescriptionBlock<'static>]

Returns all the current InterfaceDescriptionBlock.

Auto Trait Implementations§

§

impl<W> RefUnwindSafe for PcapNgWriter<W>where
    W: RefUnwindSafe,

§

impl<W> Send for PcapNgWriter<W>where
    W: Send,

§

impl<W> Sync for PcapNgWriter<W>where
    W: Sync,

§

impl<W> Unpin for PcapNgWriter<W>where
    W: Unpin,

§

impl<W> UnwindSafe for PcapNgWriter<W>where
    W: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.