[][src]Struct pcap_file::PcapWriter

pub struct PcapWriter<W: Write> {
    pub header: PcapHeader,
    // some fields omitted
}

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:

This example is not tested
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]

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]

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

impl<W: Debug + Write> Debug for PcapWriter<W>[src]

Auto Trait Implementations

impl<W> Send for PcapWriter<W> where
    W: Send

impl<W> Sync for PcapWriter<W> where
    W: Sync

impl<W> Unpin for PcapWriter<W> where
    W: Unpin

impl<W> UnwindSafe for PcapWriter<W> where
    W: UnwindSafe

impl<W> RefUnwindSafe for PcapWriter<W> where
    W: RefUnwindSafe

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]