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 
85  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
86 
93  static IFileReaderDevice* getReader(const char* fileName);
94  };
95 
96 
102  {
103  private:
104  LinkLayerType m_PcapLinkLayerType;
105 
106  // private copy c'tor
108  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
109 
110  public:
116  PcapFileReaderDevice(const char* fileName);
117 
122 
127 
128 
129  //overridden methods
130 
137  bool getNextPacket(RawPacket& rawPacket);
138 
144  bool open();
145 
150  void getStatistics(pcap_stat& stats);
151  };
152 
153 
159  {
160  private:
161  void* m_LightPcapNg;
162  struct bpf_program m_Bpf;
163  bool m_BpfInitialized;
164  int m_BpfLinkType;
165  std::string m_CurFilter;
166 
167  // private copy c'tor
169  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
170 
171  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timeval packetTimestamp, uint16_t linkType);
172 
173  public:
179  PcapNgFileReaderDevice(const char* fileName);
180 
185 
192  std::string getOS();
193 
200  std::string getHardware();
201 
208  std::string getCaptureApplication();
209 
216  std::string getCaptureFileComment();
217 
226  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
227 
228  //overridden methods
229 
236  bool getNextPacket(RawPacket& rawPacket);
237 
243  bool open();
244 
249  void getStatistics(pcap_stat& stats);
250 
256  bool setFilter(std::string filterAsString);
257 
261  void close();
262  };
263 
264 
270  {
271  protected:
272  uint32_t m_NumOfPacketsWritten;
273  uint32_t m_NumOfPacketsNotWritten;
274 
275  IFileWriterDevice(const char* fileName);
276 
277  public:
278 
282  virtual ~IFileWriterDevice() {}
283 
284  virtual bool writePacket(RawPacket const& packet) = 0;
285 
286  virtual bool writePackets(const RawPacketVector& packets) = 0;
287 
288  using IFileDevice::open;
289  virtual bool open(bool appendMode) = 0;
290  };
291 
292 
300  {
301  private:
302  pcap_dumper_t* m_PcapDumpHandler;
303  LinkLayerType m_PcapLinkLayerType;
304  bool m_AppendMode;
305  FILE* m_File;
306 
307  // private copy c'tor
309  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
310 
311  void closeFile();
312 
313  public:
320  PcapFileWriterDevice(const char* fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
321 
326 
335  bool writePacket(RawPacket const& packet);
336 
344  bool writePackets(const RawPacketVector& packets);
345 
346  //override methods
347 
354  virtual bool open();
355 
366  bool open(bool appendMode);
367 
371  virtual void close();
372 
377  virtual void getStatistics(pcap_stat& stats);
378  };
379 
380 
389  {
390  private:
391 
392  void* m_LightPcapNg;
393 
394  // private copy c'tor
396  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
397 
398  public:
399 
405  PcapNgFileWriterDevice(const char* fileName);
406 
411 
427  bool open(const char* os, const char* hardware, const char* captureApp, const char* fileComment);
428 
437  bool writePacket(RawPacket const& packet, const char* comment);
438 
439  //overridden methods
440 
447  bool writePacket(RawPacket const& packet);
448 
456  bool writePackets(const RawPacketVector& packets);
457 
464  bool open();
465 
475  bool open(bool appendMode);
476 
480  void close();
481 
486  void getStatistics(pcap_stat& stats);
487  };
488 
489 }// namespace pcpp
490 
491 #endif
Definition: PcapFileDevice.h:299
int getNextPackets(RawPacketVector &packetVec, int numOfPacketsToRead=-1)
static IFileReaderDevice * getReader(const char *fileName)
std::string getCaptureApplication()
void getStatistics(pcap_stat &stats)
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:101
Definition: RawPacket.h:220
Definition: PcapDevice.h:38
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:282
bool writePackets(const RawPacketVector &packets)
The main namespace for the PcapPlusPlus lib.
LinkLayerType
Definition: RawPacket.h:25
bool writePacket(RawPacket const &packet)
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:121
Definition: PcapFileDevice.h:269
void getStatistics(pcap_stat &stats)
Definition: PcapFileDevice.h:49
Definition: PointerVector.h:24
bool getNextPacket(RawPacket &rawPacket)
Definition: PcapFileDevice.h:388
Definition: PcapFileDevice.h:158
std::string getCaptureFileComment()
IFileReaderDevice(const char *fileName)
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:67
void getStatistics(pcap_stat &stats)
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:410
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:184
virtual void getStatistics(pcap_stat &stats)
bool writePackets(const RawPacketVector &packets)
LinkLayerType getLinkLayerType()
virtual bool open()=0
std::string getFileName()
virtual void close()
bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:20
bool writePacket(RawPacket const &packet, const char *comment)