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§

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.

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

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

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();

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();

Writes a RawBlock.

Doesn’t check the validity of the written blocks.

Consumes Self, returning the wrapped writer.

Gets a reference to the underlying writer.

Gets a mutable reference to the underlying writer.

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

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.