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 
45  virtual void close();
46  };
47 
48 
54  {
55  protected:
56  uint32_t m_NumOfPacketsRead;
57  uint32_t m_NumOfPacketsNotParsed;
58 
64  IFileReaderDevice(const std::string& fileName);
65 
66  public:
67 
71  virtual ~IFileReaderDevice() {}
72 
76  uint64_t getFileSize() const;
77 
78  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
79 
87  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
88 
95  static IFileReaderDevice* getReader(const std::string& fileName);
96  };
97 
98 
104  {
105  private:
106  LinkLayerType m_PcapLinkLayerType;
107 
108  // private copy c'tor
110  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
111 
112  public:
118  PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
119 
124 
128  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
129 
130 
131  //overridden methods
132 
139  bool getNextPacket(RawPacket& rawPacket);
140 
146  bool open();
147 
152  void getStatistics(PcapStats& stats) const;
153  };
154 
155 
161  {
162  private:
163  void* m_LightPcapNg;
164  BpfFilterWrapper m_BpfWrapper;
165 
166  // private copy c'tor
168  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
169 
170  public:
176  PcapNgFileReaderDevice(const std::string& fileName);
177 
182 
189  std::string getOS() const;
190 
197  std::string getHardware() const;
198 
205  std::string getCaptureApplication() const;
206 
213  std::string getCaptureFileComment() const;
214 
223  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
224 
225  //overridden methods
226 
233  bool getNextPacket(RawPacket& rawPacket);
234 
240  bool open();
241 
246  void getStatistics(PcapStats& stats) const;
247 
253  bool setFilter(std::string filterAsString);
254 
258  void close();
259  };
260 
261 
267  {
268  protected:
269  uint32_t m_NumOfPacketsWritten;
270  uint32_t m_NumOfPacketsNotWritten;
271 
272  IFileWriterDevice(const std::string& fileName);
273 
274  public:
275 
279  virtual ~IFileWriterDevice() {}
280 
281  virtual bool writePacket(RawPacket const& packet) = 0;
282 
283  virtual bool writePackets(const RawPacketVector& packets) = 0;
284 
285  using IFileDevice::open;
286  virtual bool open(bool appendMode) = 0;
287  };
288 
289 
297  {
298  private:
299  pcap_dumper_t* m_PcapDumpHandler;
300  LinkLayerType m_PcapLinkLayerType;
301  bool m_AppendMode;
302  FILE* m_File;
303 
304  // private copy c'tor
306  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
307 
308  void closeFile();
309 
310  public:
317  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
318 
323 
332  bool writePacket(RawPacket const& packet);
333 
341  bool writePackets(const RawPacketVector& packets);
342 
343  //override methods
344 
351  virtual bool open();
352 
363  bool open(bool appendMode);
364 
368  virtual void close();
369 
373  void flush();
374 
379  virtual void getStatistics(PcapStats& stats) const;
380  };
381 
382 
391  {
392  private:
393  void* m_LightPcapNg;
394  int m_CompressionLevel;
395  BpfFilterWrapper m_BpfWrapper;
396 
397  // private copy c'tor
399  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
400 
401  public:
402 
409  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
410 
415 
431  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp, const std::string& fileComment);
432 
441  bool writePacket(RawPacket const& packet, const std::string& comment);
442 
443  //overridden methods
444 
451  bool writePacket(RawPacket const& packet);
452 
460  bool writePackets(const RawPacketVector& packets);
461 
468  bool open();
469 
479  bool open(bool appendMode);
480 
484  void flush();
485 
489  void close();
490 
495  void getStatistics(PcapStats& stats) const;
496 
502  bool setFilter(std::string filterAsString);
503 
504  };
505 
506 }// namespace pcpp
507 
508 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:24
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:53
std::string getFileName() const
Definition: PcapFileDevice.h:160
Definition: PcapDevice.h:39
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:123
Definition: PointerVector.h:24
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:71
Definition: RawPacket.h:252
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:279
Definition: PcapFileDevice.h:390
virtual bool open()=0
Definition: PcapFileDevice.h:296
virtual void close()
Definition: PcapFilter.h:77
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:322
Definition: PcapFileDevice.h:103
LinkLayerType
Definition: RawPacket.h:25
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:118
virtual void getStatistics(PcapStats &stats) const =0
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:128
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:414
virtual bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:266
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:181
Definition: PcapDevice.h:53