PcapPlusPlus  21.05+
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 
7 // forward decleration for structs and typedefs defined in pcap.h
8 struct pcap_dumper;
9 typedef struct pcap_dumper pcap_dumper_t;
10 
12 
17 namespace pcpp
18 {
19 
24  class IFileDevice : public IPcapDevice
25  {
26  protected:
27  std::string m_FileName;
28 
29  IFileDevice(const std::string& fileName);
30  virtual ~IFileDevice();
31 
32  public:
33 
37  std::string getFileName() const;
38 
39 
40  //override methods
41 
46  virtual void close();
47  };
48 
49 
55  {
56  protected:
57  uint32_t m_NumOfPacketsRead;
58  uint32_t m_NumOfPacketsNotParsed;
59 
65  IFileReaderDevice(const std::string& fileName);
66 
67  public:
68 
72  virtual ~IFileReaderDevice() {}
73 
77  uint64_t getFileSize() const;
78 
79  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
80 
88  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
89 
96  static IFileReaderDevice* getReader(const std::string& fileName);
97  };
98 
99 
105  {
106  private:
107  LinkLayerType m_PcapLinkLayerType;
108 
109  // private copy c'tor
111  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
112 
113  public:
119  PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
120 
125 
129  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
130 
131 
132  //overridden methods
133 
140  bool getNextPacket(RawPacket& rawPacket);
141 
147  bool open();
148 
153  void getStatistics(PcapStats& stats) const;
154  };
155 
156 
162  {
163  private:
164  void* m_LightPcapNg;
165  BpfFilterWrapper m_BpfWrapper;
166 
167  // private copy c'tor
169  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
170 
171  public:
177  PcapNgFileReaderDevice(const std::string& 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(PcapStats& 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 std::string& 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 std::string& 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(PcapStats& stats) const;
381  };
382 
383 
392  {
393  private:
394  void* m_LightPcapNg;
395  int m_CompressionLevel;
396  BpfFilterWrapper m_BpfWrapper;
397 
398  // private copy c'tor
400  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
401 
402  public:
403 
410  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
411 
416 
432  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp, const std::string& fileComment);
433 
442  bool writePacket(RawPacket const& packet, const std::string& comment);
443 
444  //overridden methods
445 
452  bool writePacket(RawPacket const& packet);
453 
461  bool writePackets(const RawPacketVector& packets);
462 
469  bool open();
470 
480  bool open(bool appendMode);
481 
485  void flush();
486 
490  void close();
491 
496  void getStatistics(PcapStats& stats) const;
497 
503  bool setFilter(std::string filterAsString);
504 
505  };
506 
507 }// namespace pcpp
508 
509 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:24
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:54
std::string getFileName() const
Definition: PcapFileDevice.h:161
Definition: PcapDevice.h:39
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:124
Definition: PointerVector.h:24
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:72
Definition: RawPacket.h:252
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:280
Definition: PcapFileDevice.h:391
virtual bool open()=0
Definition: PcapFileDevice.h:297
virtual void close()
Definition: PcapFilter.h:77
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:323
Definition: PcapFileDevice.h:104
LinkLayerType
Definition: RawPacket.h:25
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:119
virtual void getStatistics(PcapStats &stats) const =0
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:129
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:415
virtual bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:267
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:182
Definition: PcapDevice.h:53