Struct pcap_file::pcapng::PcapNgWriter
source · 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>
impl<W: Write> PcapNgWriter<W>
sourcepub fn new(writer: W) -> PcapResult<Self>
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.
sourcepub fn with_endianness(writer: W, endianness: Endianness) -> PcapResult<Self>
pub fn with_endianness(writer: W, endianness: Endianness) -> PcapResult<Self>
Creates a new PcapNgWriter
from an existing writer with the given endianness.
sourcepub fn with_section_header(
writer: W,
section: SectionHeaderBlock<'static>
) -> PcapResult<Self>
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.
sourcepub fn write_block(&mut self, block: &Block<'_>) -> PcapResult<usize>
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();
sourcepub fn write_pcapng_block<'a, B: PcapNgBlock<'a>>(
&mut self,
block: B
) -> PcapResult<usize>
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();
sourcepub fn write_raw_block(&mut self, block: &RawBlock<'_>) -> PcapResult<usize>
pub fn write_raw_block(&mut self, block: &RawBlock<'_>) -> PcapResult<usize>
Writes a RawBlock
.
Doesn’t check the validity of the written blocks.
sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consumes Self
, returning the wrapped writer.
sourcepub fn get_mut(&mut self) -> &mut W
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
sourcepub fn section(&self) -> &SectionHeaderBlock<'static>
pub fn section(&self) -> &SectionHeaderBlock<'static>
Returns the current SectionHeaderBlock
.
sourcepub fn interfaces(&self) -> &[InterfaceDescriptionBlock<'static>]
pub fn interfaces(&self) -> &[InterfaceDescriptionBlock<'static>]
Returns all the current InterfaceDescriptionBlock
.