PcapPlusPlus  20.08
pcpp::PcapFileWriterDevice Class Reference

#include <PcapFileDevice.h>

Inheritance diagram for pcpp::PcapFileWriterDevice:
pcpp::IFileWriterDevice pcpp::IFileDevice pcpp::IPcapDevice pcpp::IDevice pcpp::IFilterableDevice

Public Member Functions

 PcapFileWriterDevice (const char *fileName, LinkLayerType linkLayerType=LINKTYPE_ETHERNET)
 
 ~PcapFileWriterDevice ()
 
bool writePacket (RawPacket const &packet)
 
bool writePackets (const RawPacketVector &packets)
 
virtual bool open ()
 
bool open (bool appendMode)
 
virtual void close ()
 
void flush ()
 
virtual void getStatistics (pcap_stat &stats) const
 
- Public Member Functions inherited from pcpp::IFileWriterDevice
virtual ~IFileWriterDevice ()
 
- Public Member Functions inherited from pcpp::IFileDevice
std::string getFileName () const
 
- Public Member Functions inherited from pcpp::IPcapDevice
virtual bool setFilter (std::string filterAsString)
 
bool clearFilter ()
 
virtual bool setFilter (GeneralFilter &filter)
 
virtual bool setFilter (std::string filterAsString)=0
 
- Public Member Functions inherited from pcpp::IDevice
bool isOpened ()
 
- Public Member Functions inherited from pcpp::IFilterableDevice
virtual bool setFilter (GeneralFilter &filter)
 

Additional Inherited Members

- Static Public Member Functions inherited from pcpp::IPcapDevice
static std::string getPcapLibVersionInfo ()
 
static bool verifyFilter (std::string filterAsString)
 
static bool matchPacketWithFilter (std::string filterAsString, RawPacket *rawPacket)
 
static bool matchPacketWithFilter (GeneralFilter &filter, RawPacket *rawPacket)
 

Detailed Description

A class for opening a pcap file for writing or create a new pcap file and write packets to it. This class adds a unique capability that isn't supported in WinPcap and in older libpcap versions which is to open a pcap file in append mode where packets are written at the end of the pcap file instead of running it over

Constructor & Destructor Documentation

◆ PcapFileWriterDevice()

pcpp::PcapFileWriterDevice::PcapFileWriterDevice ( const char *  fileName,
LinkLayerType  linkLayerType = LINKTYPE_ETHERNET 
)

A constructor for this class that gets the pcap full path file name to open for writing or create. Notice that after calling this constructor the file isn't opened yet, so writing packets will fail. For opening the file call open()

Parameters
[in]fileNameThe full path of the file
[in]linkLayerTypeThe link layer type all packet in this file will be based on. The default is Ethernet

◆ ~PcapFileWriterDevice()

pcpp::PcapFileWriterDevice::~PcapFileWriterDevice ( )
inline

A destructor for this class

Member Function Documentation

◆ close()

virtual void pcpp::PcapFileWriterDevice::close ( )
virtual

Flush and close the pacp file

Reimplemented from pcpp::IFileDevice.

◆ flush()

void pcpp::PcapFileWriterDevice::flush ( )

Flush packets to disk.

◆ getStatistics()

virtual void pcpp::PcapFileWriterDevice::getStatistics ( pcap_stat &  stats) const
virtual

Get statistics of packets written so far. In the pcap_stat struct, only ps_recv member is relevant. The rest of the members will contain 0

Parameters
[out]statsThe stats struct where stats are returned

Implements pcpp::IPcapDevice.

◆ open() [1/2]

virtual bool pcpp::PcapFileWriterDevice::open ( )
virtual

Open the file in a write mode. If file doesn't exist, it will be created. If it does exist it will be overwritten, meaning all its current content will be deleted

Returns
True if file was opened/created successfully or if file is already opened. False if opening the file failed for some reason (an error will be printed to log)

Implements pcpp::IDevice.

◆ open() [2/2]

bool pcpp::PcapFileWriterDevice::open ( bool  appendMode)
virtual

Same as open(), but enables to open the file in append mode in which packets will be appended to the file instead of overwrite its current content. In append mode file must exist, otherwise opening will fail

Parameters
[in]appendModeA boolean indicating whether to open the file in append mode or not. If set to false this method will act exactly like open(). If set to true, file will be opened in append mode
Returns
True of managed to open the file successfully. In case appendMode is set to true, false will be returned if file wasn't found or couldn't be read, if file type is not pcap, or if link type specified in c'tor is different from current file link type. In case appendMode is set to false, please refer to open() for return values

Implements pcpp::IFileWriterDevice.

◆ writePacket()

bool pcpp::PcapFileWriterDevice::writePacket ( RawPacket const &  packet)
virtual

Write a RawPacket to the file. Before using this method please verify the file is opened using open(). This method won't change the written packet

Parameters
[in]packetA reference for an existing RawPcket to write to the file
Returns
True if a packet was written successfully. False will be returned if the file isn't opened or if the packet link layer type is different than the one defined for the file (in all cases, an error will be printed to log)

Implements pcpp::IFileWriterDevice.

◆ writePackets()

bool pcpp::PcapFileWriterDevice::writePackets ( const RawPacketVector packets)
virtual

Write multiple RawPacket to the file. Before using this method please verify the file is opened using open(). This method won't change the written packets or the RawPacketVector instance

Parameters
[in]packetsA reference for an existing RawPcketVector, all of its packets will be written to the file
Returns
True if all packets were written successfully to the file. False will be returned if the file isn't opened (also, an error log will be printed) or if at least one of the packets wasn't written successfully to the file

Implements pcpp::IFileWriterDevice.