PcapPlusPlus  20.08
PcapFileDevice.h
Go to the documentation of this file.
1 #ifndef PCAPPP_FILE_DEVICE
2 #define PCAPPP_FILE_DEVICE
3 
4 #include "PcapDevice.h"
5 #include "RawPacket.h"
6 
8 
13 namespace pcpp
14 {
15 
20  class IFileDevice : public IPcapDevice
21  {
22  protected:
23  char* m_FileName;
24 
25  IFileDevice(const char* fileName);
26  virtual ~IFileDevice();
27 
28  public:
29 
33  std::string getFileName() const;
34 
35 
36  //override methods
37 
41  virtual void close();
42  };
43 
44 
50  {
51  protected:
52  uint32_t m_NumOfPacketsRead;
53  uint32_t m_NumOfPacketsNotParsed;
54 
60  IFileReaderDevice(const char* fileName);
61 
62  public:
63 
67  virtual ~IFileReaderDevice() {}
68 
72  uint64_t getFileSize() const;
73 
74  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
75 
83  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
84 
91  static IFileReaderDevice* getReader(const char* fileName);
92  };
93 
94 
100  {
101  private:
102  LinkLayerType m_PcapLinkLayerType;
103 
104  // private copy c'tor
106  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
107 
108  public:
114  PcapFileReaderDevice(const char* fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
115 
120 
124  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
125 
126 
127  //overridden methods
128 
135  bool getNextPacket(RawPacket& rawPacket);
136 
142  bool open();
143 
148  void getStatistics(pcap_stat& stats) const;
149  };
150 
151 
157  {
158  private:
159  void* m_LightPcapNg;
160  struct bpf_program m_Bpf;
161  bool m_BpfInitialized;
162  int m_BpfLinkType;
163  std::string m_CurFilter;
164 
165  // private copy c'tor
167  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
168 
169  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timespec packetTimestamp, uint16_t linkType);
170 
171  public:
177  PcapNgFileReaderDevice(const char* fileName);
178 
183 
190  std::string getOS() const;
191 
198  std::string getHardware() const;
199 
206  std::string getCaptureApplication() const;
207 
214  std::string getCaptureFileComment() const;
215 
224  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
225 
226  //overridden methods
227 
234  bool getNextPacket(RawPacket& rawPacket);
235 
241  bool open();
242 
247  void getStatistics(pcap_stat& stats) const;
248 
254  bool setFilter(std::string filterAsString);
255 
259  void close();
260  };
261 
262 
268  {
269  protected:
270  uint32_t m_NumOfPacketsWritten;
271  uint32_t m_NumOfPacketsNotWritten;
272 
273  IFileWriterDevice(const char* fileName);
274 
275  public:
276 
280  virtual ~IFileWriterDevice() {}
281 
282  virtual bool writePacket(RawPacket const& packet) = 0;
283 
284  virtual bool writePackets(const RawPacketVector& packets) = 0;
285 
286  using IFileDevice::open;
287  virtual bool open(bool appendMode) = 0;
288  };
289 
290 
298  {
299  private:
300  pcap_dumper_t* m_PcapDumpHandler;
301  LinkLayerType m_PcapLinkLayerType;
302  bool m_AppendMode;
303  FILE* m_File;
304 
305  // private copy c'tor
307  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
308 
309  void closeFile();
310 
311  public:
318  PcapFileWriterDevice(const char* fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
319 
324 
333  bool writePacket(RawPacket const& packet);
334 
342  bool writePackets(const RawPacketVector& packets);
343 
344  //override methods
345 
352  virtual bool open();
353 
364  bool open(bool appendMode);
365 
369  virtual void close();
370 
374  void flush();
375 
380  virtual void getStatistics(pcap_stat& stats) const;
381  };
382 
383 
392  {
393  private:
394  void* m_LightPcapNg;
395  int m_CompressionLevel;
396  struct bpf_program m_Bpf;
397  bool m_BpfInitialized;
398  int m_BpfLinkType;
399  std::string m_CurFilter;
400 
401  // private copy c'tor
403  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
404 
405  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timespec packetTimestamp, uint16_t linkType);
406 
407  public:
408 
415  PcapNgFileWriterDevice(const char* fileName, int compressionLevel = 0);
416 
421 
437  bool open(const char* os, const char* hardware, const char* captureApp, const char* fileComment);
438 
447  bool writePacket(RawPacket const& packet, const char* comment);
448 
449  //overridden methods
450 
457  bool writePacket(RawPacket const& packet);
458 
466  bool writePackets(const RawPacketVector& packets);
467 
474  bool open();
475 
485  bool open(bool appendMode);
486 
490  void flush();
491 
495  void close();
496 
501  void getStatistics(pcap_stat& stats) const;
502 
508  bool setFilter(std::string filterAsString);
509 
510  };
511 
512 }// namespace pcpp
513 
514 #endif
pcpp::IFileReaderDevice::getFileSize
uint64_t getFileSize() const
pcpp::PcapFileWriterDevice::open
virtual bool open()
pcpp::IFileWriterDevice
Definition: PcapFileDevice.h:267
pcpp::PcapFileReaderDevice::open
bool open()
pcpp::PcapFileReaderDevice::getNextPacket
bool getNextPacket(RawPacket &rawPacket)
pcpp::PcapNgFileReaderDevice::~PcapNgFileReaderDevice
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:182
pcpp::IFileReaderDevice::getReader
static IFileReaderDevice * getReader(const char *fileName)
pcpp::LINKTYPE_ETHERNET
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:30
pcpp::PcapFileWriterDevice::close
virtual void close()
pcpp::PcapFileWriterDevice::writePackets
bool writePackets(const RawPacketVector &packets)
pcpp::PcapNgFileReaderDevice::getCaptureFileComment
std::string getCaptureFileComment() const
pcpp::PcapFileReaderDevice::PcapFileReaderDevice
PcapFileReaderDevice(const char *fileName)
Definition: PcapFileDevice.h:114
pcpp::PcapNgFileReaderDevice::getOS
std::string getOS() const
pcpp::PcapFileReaderDevice::getStatistics
void getStatistics(pcap_stat &stats) const
PcapDevice.h
pcpp::PcapFileWriterDevice::flush
void flush()
pcpp::PcapNgFileWriterDevice::open
bool open()
pcpp::PcapFileWriterDevice::getStatistics
virtual void getStatistics(pcap_stat &stats) const
pcpp::PcapNgFileReaderDevice::getNextPacket
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
RawPacket.h
pcpp::PcapNgFileReaderDevice::close
void close()
pcpp::PcapFileReaderDevice::getLinkLayerType
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:124
pcpp::IFileReaderDevice::~IFileReaderDevice
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:67
pcpp::IDevice::open
virtual bool open()=0
pcpp::IFileWriterDevice::~IFileWriterDevice
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:280
pcpp::PcapNgFileReaderDevice::getHardware
std::string getHardware() const
pcpp::IFileReaderDevice::IFileReaderDevice
IFileReaderDevice(const char *fileName)
pcpp::PcapNgFileReaderDevice
Definition: PcapFileDevice.h:156
pcpp::PcapNgFileWriterDevice::flush
void flush()
pcpp::PcapNgFileReaderDevice::getStatistics
void getStatistics(pcap_stat &stats) const
pcpp::PcapNgFileWriterDevice::writePacket
bool writePacket(RawPacket const &packet, const char *comment)
pcpp::PcapFileReaderDevice::~PcapFileReaderDevice
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:119
pcpp::PcapNgFileReaderDevice::setFilter
bool setFilter(std::string filterAsString)
pcpp::PcapNgFileWriterDevice::~PcapNgFileWriterDevice
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:420
pcpp::PcapFileWriterDevice
Definition: PcapFileDevice.h:297
pcpp::PcapNgFileWriterDevice::setFilter
bool setFilter(std::string filterAsString)
pcpp::PcapNgFileWriterDevice
Definition: PcapFileDevice.h:391
pcpp::IPcapDevice
Definition: PcapDevice.h:35
pcpp::IFileReaderDevice
Definition: PcapFileDevice.h:49
pcpp::PcapNgFileWriterDevice::getStatistics
void getStatistics(pcap_stat &stats) const
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::PcapNgFileWriterDevice::close
void close()
pcpp::PcapNgFileReaderDevice::getCaptureApplication
std::string getCaptureApplication() const
pcpp::LinkLayerType
LinkLayerType
Definition: RawPacket.h:25
pcpp::IFileDevice::close
virtual void close()
pcpp::PcapFileReaderDevice
Definition: PcapFileDevice.h:99
pcpp::PcapNgFileReaderDevice::open
bool open()
pcpp::PcapFileWriterDevice::writePacket
bool writePacket(RawPacket const &packet)
pcpp::IFileDevice::getFileName
std::string getFileName() const
pcpp::PointerVector
Definition: PointerVector.h:24
pcpp::IFileDevice
Definition: PcapFileDevice.h:20
pcpp::PcapNgFileWriterDevice::writePackets
bool writePackets(const RawPacketVector &packets)
pcpp::RawPacket
Definition: RawPacket.h:252
pcpp::PcapFileWriterDevice::~PcapFileWriterDevice
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:323
pcpp::IFileReaderDevice::getNextPackets
int getNextPackets(RawPacketVector &packetVec, int numOfPacketsToRead=-1)