PcapPlusPlus
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();
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();
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);
115 
120 
125 
126 
127  //overridden methods
128 
135  bool getNextPacket(RawPacket& rawPacket);
136 
142  bool open();
143 
148  void getStatistics(pcap_stat& stats);
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, timeval packetTimestamp, uint16_t linkType);
170 
171  public:
177  PcapNgFileReaderDevice(const char* fileName);
178 
183 
190  std::string getOS();
191 
198  std::string getHardware();
199 
206  std::string getCaptureApplication();
207 
214  std::string getCaptureFileComment();
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);
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 
375  virtual void getStatistics(pcap_stat& stats);
376  };
377 
378 
387  {
388  private:
389  void* m_LightPcapNg;
390  struct bpf_program m_Bpf;
391  bool m_BpfInitialized;
392  int m_BpfLinkType;
393  std::string m_CurFilter;
394 
395  // private copy c'tor
397  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
398 
399  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timeval packetTimestamp, uint16_t linkType);
400 
401  public:
402 
408  PcapNgFileWriterDevice(const char* fileName);
409 
414 
430  bool open(const char* os, const char* hardware, const char* captureApp, const char* fileComment);
431 
440  bool writePacket(RawPacket const& packet, const char* comment);
441 
442  //overridden methods
443 
450  bool writePacket(RawPacket const& packet);
451 
459  bool writePackets(const RawPacketVector& packets);
460 
467  bool open();
468 
478  bool open(bool appendMode);
479 
483  void close();
484 
489  void getStatistics(pcap_stat& stats);
490 
496  bool setFilter(std::string filterAsString);
497 
498  };
499 
500 }// namespace pcpp
501 
502 #endif
Definition: PcapFileDevice.h:49
std::string getFileName()
bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:386
bool writePackets(const RawPacketVector &packets)
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:280
Definition: PcapFileDevice.h:156
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:413
bool writePackets(const RawPacketVector &packets)
bool writePacket(RawPacket const &packet)
static IFileReaderDevice * getReader(const char *fileName)
Definition: PointerVector.h:24
bool writePacket(RawPacket const &packet, const char *comment)
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:67
int getNextPackets(RawPacketVector &packetVec, int numOfPacketsToRead=-1)
std::string getCaptureApplication()
IFileReaderDevice(const char *fileName)
Definition: RawPacket.h:219
Definition: PcapFileDevice.h:297
Definition: PcapFileDevice.h:99
Definition: PcapFileDevice.h:267
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:119
void getStatistics(pcap_stat &stats)
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:182
Definition: RawPacket.h:29
virtual void getStatistics(pcap_stat &stats)
virtual bool open()=0
void getStatistics(pcap_stat &stats)
virtual void close()
Definition: PcapFileDevice.h:20
LinkLayerType getLinkLayerType()
std::string getCaptureFileComment()
The main namespace for the PcapPlusPlus lib.
LinkLayerType
Definition: RawPacket.h:24
bool getNextPacket(RawPacket &rawPacket)
Definition: PcapDevice.h:32
bool setFilter(std::string filterAsString)
void getStatistics(pcap_stat &stats)