[−][src]Struct pcap_file::PcapWriter
This struct wraps another writer and uses it to write a Pcap formated stream.
Examples
use std::fs::File; use pcap_file::pcap::{PcapReader, PcapWriter}; let file_in = File::open("test.pcap").expect("Error opening file"); let pcap_reader = PcapReader::new(file_in).unwrap(); let file_out = File::create("out.pcap").expect("Error creating file out"); let mut pcap_writer = PcapWriter::new(file_out).expect("Error writing file"); // Read test.pcap for pcap in pcap_reader { //Check if there is no error let pcap = pcap.unwrap(); //Write each packet of test.pcap in out.pcap pcap_writer.write_packet(&pcap).unwrap(); }
Fields
header: PcapHeader
Methods
impl<W: Write> PcapWriter<W>
[src]
pub fn new(writer: W) -> Result<PcapWriter<W>, PcapError>
[src]
Creates a new PcapWriter
from an existing writer in the choosen endianess.
Defaults to the native endianness of the CPU.
Automatically writes this default global pcap header to the file:
PcapHeader { version_major : 2, version_minor : 4, ts_correction : 0, ts_accuracy : 0, snaplen : 65535, datalink : DataLink::ETHERNET };
Errors
Return an error if the writer can't be written to.
Examples
use std::fs::File; use pcap_file::pcap::PcapWriter; let file_out = File::create("out.pcap").expect("Error creating file"); let mut pcap_writer = PcapWriter::new(file_out);
pub fn with_header(
header: PcapHeader,
writer: W
) -> Result<PcapWriter<W>, PcapError>
[src]
header: PcapHeader,
writer: W
) -> Result<PcapWriter<W>, PcapError>
Create a new PcapWriter
from an existing writer with a user defined pcap header.
The endianness and the timestamp resolution are defined by the magic number of the header. It is possible to change them with 'set_endianess()' and 'set_ts_resolution()'
It Automatically writes the pcap header to the file.
Errors
Return an error if the writer can't be written to.
Examples
use std::fs::File; use pcap_file::{ DataLink, pcap::{PcapHeader, PcapWriter}, }; let file = File::create("out.pcap").expect("Error creating file"); let header = PcapHeader { magic_number : 0xa1b2c3d4, version_major : 2, version_minor : 4, ts_correction : 0, ts_accuracy : 0, snaplen : 65535, datalink : DataLink::ETHERNET }; let mut pcap_writer = PcapWriter::with_header(header, file);
pub fn into_writer(self) -> W
[src]
Consumes the PcapWriter
, returning the wrapped writer.
pub fn get_ref(&self) -> &W
[src]
Gets a reference to the underlying writer.
pub fn get_mut(&mut self) -> &mut W
[src]
Gets a mutable reference to the underlying writer.
It is inadvisable to directly write to the underlying writer.
pub fn write(
&mut self,
ts_sec: u32,
ts_nsec: u32,
data: &[u8],
orig_len: u32
) -> Result<(), PcapError>
[src]
&mut self,
ts_sec: u32,
ts_nsec: u32,
data: &[u8],
orig_len: u32
) -> Result<(), PcapError>
Writes some raw data, converting it to the pcap file format.
Examples
use std::fs::File; use pcap_file::pcap::PcapWriter; let data = [0u8; 10]; let file = File::create("out.pcap").expect("Error creating file"); let mut pcap_writer = PcapWriter::new(file).unwrap(); pcap_writer.write(1, 0, &data, data.len() as u32).unwrap();
pub fn write_packet(&mut self, packet: &Packet) -> Result<(), PcapError>
[src]
Writes a Packet
.
Examples
use std::fs::File; use pcap_file::pcap::{ Packet, PcapWriter }; let data = [0u8; 10]; let packet = Packet::new(1, 0, &data, data.len() as u32); let file = File::create("out.pcap").expect("Error creating file"); let mut pcap_writer = PcapWriter::new(file).unwrap(); pcap_writer.write_packet(&packet).unwrap();
Trait Implementations
Auto Trait Implementations
impl<W> Send for PcapWriter<W> where
W: Send,
W: Send,
impl<W> Sync for PcapWriter<W> where
W: Sync,
W: Sync,
impl<W> Unpin for PcapWriter<W> where
W: Unpin,
W: Unpin,
impl<W> UnwindSafe for PcapWriter<W> where
W: UnwindSafe,
W: UnwindSafe,
impl<W> RefUnwindSafe for PcapWriter<W> where
W: RefUnwindSafe,
W: RefUnwindSafe,
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,