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 
82  static IFileReaderDevice* getReader(const char* fileName);
83  };
84 
85 
91  {
92  private:
93  LinkLayerType m_PcapLinkLayerType;
94 
95  // private copy c'tor
97  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
98 
99  public:
105  PcapFileReaderDevice(const char* fileName);
106 
111 
116 
117 
118  //overridden methods
119 
126  bool getNextPacket(RawPacket& rawPacket);
127 
133  bool open();
134 
139  void getStatistics(pcap_stat& stats);
140  };
141 
142 
148  {
149  private:
150  void* m_LightPcapNg;
151  struct bpf_program m_Bpf;
152  bool m_BpfInitialized;
153  int m_BpfLinkType;
154  std::string m_CurFilter;
155 
156  // private copy c'tor
158  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
159 
160  bool matchPacketWithFilter(const uint8_t* packetData, size_t packetLen, timeval packetTimestamp, uint16_t linkType);
161 
162  public:
168  PcapNgFileReaderDevice(const char* fileName);
169 
174 
181  std::string getOS();
182 
189  std::string getHardware();
190 
197  std::string getCaptureApplication();
198 
205  std::string getCaptureFileComment();
206 
215  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
216 
217  //overridden methods
218 
225  bool getNextPacket(RawPacket& rawPacket);
226 
232  bool open();
233 
238  void getStatistics(pcap_stat& stats);
239 
245  bool setFilter(std::string filterAsString);
246 
250  void close();
251  };
252 
253 
259  {
260  protected:
261  uint32_t m_NumOfPacketsWritten;
262  uint32_t m_NumOfPacketsNotWritten;
263 
264  IFileWriterDevice(const char* fileName);
265 
266  public:
267 
271  virtual ~IFileWriterDevice() {}
272 
273  virtual bool writePacket(RawPacket const& packet) = 0;
274 
275  virtual bool writePackets(const RawPacketVector& packets) = 0;
276 
277  using IFileDevice::open;
278  virtual bool open(bool appendMode) = 0;
279  };
280 
281 
289  {
290  private:
291  pcap_dumper_t* m_PcapDumpHandler;
292  LinkLayerType m_PcapLinkLayerType;
293  bool m_AppendMode;
294  FILE* m_File;
295 
296  // private copy c'tor
298  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
299 
300  void closeFile();
301 
302  public:
309  PcapFileWriterDevice(const char* fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
310 
315 
324  bool writePacket(RawPacket const& packet);
325 
333  bool writePackets(const RawPacketVector& packets);
334 
335  //override methods
336 
343  virtual bool open();
344 
355  bool open(bool appendMode);
356 
360  virtual void close();
361 
366  virtual void getStatistics(pcap_stat& stats);
367  };
368 
369 
378  {
379  private:
380 
381  void* m_LightPcapNg;
382 
383  // private copy c'tor
385  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
386 
387  public:
388 
394  PcapNgFileWriterDevice(const char* fileName);
395 
400 
416  bool open(const char* os, const char* hardware, const char* captureApp, const char* fileComment);
417 
426  bool writePacket(RawPacket const& packet, const char* comment);
427 
428  //overridden methods
429 
436  bool writePacket(RawPacket const& packet);
437 
445  bool writePackets(const RawPacketVector& packets);
446 
453  bool open();
454 
464  bool open(bool appendMode);
465 
469  void close();
470 
475  void getStatistics(pcap_stat& stats);
476  };
477 
478 }// namespace pcpp
479 
480 #endif
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:173
Definition: PcapFileDevice.h:288
bool getNextPacket(RawPacket &rawPacket)
Definition: RawPacket.h:220
bool setFilter(std::string filterAsString)
LinkLayerType
Definition: RawPacket.h:25
virtual void getStatistics(pcap_stat &stats)
virtual bool open()=0
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:67
Definition: PcapFileDevice.h:147
Definition: PcapDevice.h:26
void getStatistics(pcap_stat &stats)
bool writePackets(const RawPacketVector &packets)
static IFileReaderDevice * getReader(const char *fileName)
std::string getFileName()
Definition: RawPacket.h:30
LinkLayerType getLinkLayerType()
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:110
Definition: PcapFileDevice.h:49
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:399
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:271
bool writePackets(const RawPacketVector &packets)
virtual void close()
void getStatistics(pcap_stat &stats)
bool writePacket(RawPacket const &packet)
std::string getCaptureFileComment()
std::string getCaptureApplication()
Definition: PcapFileDevice.h:90
Definition: PcapFileDevice.h:258
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:20
bool writePacket(RawPacket const &packet, const char *comment)
void getStatistics(pcap_stat &stats)
IFileReaderDevice(const char *fileName)
Definition: PcapFileDevice.h:377
Definition: PointerVector.h:24
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)