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 
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  char* m_FileName;
28 
29  IFileDevice(const char* 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 char* 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 char* 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 char* 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  bpf_program* m_Bpf;
165  bool m_BpfInitialized;
166  int m_BpfLinkType;
167  std::string m_CurFilter;
168 
169  // private copy c'tor
171  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
172 
173  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timespec packetTimestamp, uint16_t linkType);
174 
175  public:
181  PcapNgFileReaderDevice(const char* fileName);
182 
187 
194  std::string getOS() const;
195 
202  std::string getHardware() const;
203 
210  std::string getCaptureApplication() const;
211 
218  std::string getCaptureFileComment() const;
219 
228  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
229 
230  //overridden methods
231 
238  bool getNextPacket(RawPacket& rawPacket);
239 
245  bool open();
246 
251  void getStatistics(PcapStats& stats) const;
252 
258  bool setFilter(std::string filterAsString);
259 
263  void close();
264  };
265 
266 
272  {
273  protected:
274  uint32_t m_NumOfPacketsWritten;
275  uint32_t m_NumOfPacketsNotWritten;
276 
277  IFileWriterDevice(const char* fileName);
278 
279  public:
280 
284  virtual ~IFileWriterDevice() {}
285 
286  virtual bool writePacket(RawPacket const& packet) = 0;
287 
288  virtual bool writePackets(const RawPacketVector& packets) = 0;
289 
290  using IFileDevice::open;
291  virtual bool open(bool appendMode) = 0;
292  };
293 
294 
302  {
303  private:
304  pcap_dumper_t* m_PcapDumpHandler;
305  LinkLayerType m_PcapLinkLayerType;
306  bool m_AppendMode;
307  FILE* m_File;
308 
309  // private copy c'tor
311  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
312 
313  void closeFile();
314 
315  public:
322  PcapFileWriterDevice(const char* fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
323 
328 
337  bool writePacket(RawPacket const& packet);
338 
346  bool writePackets(const RawPacketVector& packets);
347 
348  //override methods
349 
356  virtual bool open();
357 
368  bool open(bool appendMode);
369 
373  virtual void close();
374 
378  void flush();
379 
384  virtual void getStatistics(PcapStats& stats) const;
385  };
386 
387 
396  {
397  private:
398  void* m_LightPcapNg;
399  int m_CompressionLevel;
400  bpf_program* m_Bpf;
401  bool m_BpfInitialized;
402  int m_BpfLinkType;
403  std::string m_CurFilter;
404 
405  // private copy c'tor
407  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
408 
409  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timespec packetTimestamp, uint16_t linkType);
410 
411  public:
412 
419  PcapNgFileWriterDevice(const char* fileName, int compressionLevel = 0);
420 
425 
441  bool open(const char* os, const char* hardware, const char* captureApp, const char* fileComment);
442 
451  bool writePacket(RawPacket const& packet, const char* comment);
452 
453  //overridden methods
454 
461  bool writePacket(RawPacket const& packet);
462 
470  bool writePackets(const RawPacketVector& packets);
471 
478  bool open();
479 
489  bool open(bool appendMode);
490 
494  void flush();
495 
499  void close();
500 
505  void getStatistics(PcapStats& stats) const;
506 
512  bool setFilter(std::string filterAsString);
513 
514  };
515 
516 }// namespace pcpp
517 
518 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:24
PcapFileReaderDevice(const char *fileName)
Definition: PcapFileDevice.h:118
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:53
std::string getFileName() const
static bool matchPacketWithFilter(std::string filterAsString, RawPacket *rawPacket)
Definition: PcapFileDevice.h:160
Definition: PcapDevice.h:40
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:284
Definition: PcapFileDevice.h:395
virtual bool open()=0
Definition: PcapFileDevice.h:301
virtual void close()
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:327
Definition: PcapFileDevice.h:103
LinkLayerType
Definition: RawPacket.h:25
virtual void getStatistics(PcapStats &stats) const =0
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:128
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:424
virtual bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:271
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:186
Definition: PcapDevice.h:54