PcapPlusPlus  22.11
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 #include <fstream>
7 
8 // forward declaration for structs and typedefs defined in pcap.h
9 struct pcap_dumper;
10 typedef struct pcap_dumper pcap_dumper_t;
11 
13 
18 namespace pcpp
19 {
20 
25  class IFileDevice : public IPcapDevice
26  {
27  protected:
28  std::string m_FileName;
29 
30  IFileDevice(const std::string& fileName);
31  virtual ~IFileDevice();
32 
33  public:
34 
38  std::string getFileName() const;
39 
40 
41  //override methods
42 
47  virtual void close();
48  };
49 
50 
56  {
57  protected:
58  uint32_t m_NumOfPacketsRead;
59  uint32_t m_NumOfPacketsNotParsed;
60 
66  IFileReaderDevice(const std::string& fileName);
67 
68  public:
69 
73  virtual ~IFileReaderDevice() {}
74 
78  uint64_t getFileSize() const;
79 
80  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
81 
89  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
90 
97  static IFileReaderDevice* getReader(const std::string& fileName);
98  };
99 
100 
106  {
107  private:
108  LinkLayerType m_PcapLinkLayerType;
109 
110  // private copy c'tor
112  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
113 
114  public:
120  PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
121 
126 
130  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
131 
132 
133  //overridden methods
134 
141  bool getNextPacket(RawPacket& rawPacket);
142 
148  bool open();
149 
154  void getStatistics(PcapStats& stats) const;
155  };
156 
162  {
163  private:
164  #pragma pack(0)
165  /*
166  * File format header.
167  */
168  typedef struct {
169  uint64_t identification_pattern;
170  uint32_t version_number;
171  uint32_t datalink_type;
172  } snoop_file_header_t;
173 
174  /*
175  * Packet record header.
176  */
177  typedef struct {
178  uint32_t original_length; /* original packet length */
179  uint32_t included_length; /* saved packet length */
180  uint32_t packet_record_length;/* total record length */
181  uint32_t ndrops_cumulative; /* cumulative drops */
182  uint32_t time_sec; /* timestamp */
183  uint32_t time_usec; /* microsecond timestamp */
184  } snoop_packet_header_t;
185  #pragma pack()
186 
187  LinkLayerType m_PcapLinkLayerType;
188  std::ifstream m_snoopFile;
189 
190  // private copy c'tor
192  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other);
193 
194  public:
200  SnoopFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
201 
205  virtual ~SnoopFileReaderDevice();
206 
210  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
211 
212 
213  //overridden methods
214 
221  bool getNextPacket(RawPacket& rawPacket);
222 
228  bool open();
229 
234  void getStatistics(PcapStats& stats) const;
235 
239  void close();
240  };
241 
242 
248  {
249  private:
250  void* m_LightPcapNg;
251  BpfFilterWrapper m_BpfWrapper;
252 
253  // private copy c'tor
255  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
256 
257  public:
263  PcapNgFileReaderDevice(const std::string& fileName);
264 
269 
276  std::string getOS() const;
277 
284  std::string getHardware() const;
285 
292  std::string getCaptureApplication() const;
293 
300  std::string getCaptureFileComment() const;
301 
310  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
311 
312  //overridden methods
313 
320  bool getNextPacket(RawPacket& rawPacket);
321 
327  bool open();
328 
333  void getStatistics(PcapStats& stats) const;
334 
340  bool setFilter(std::string filterAsString);
341 
345  void close();
346  };
347 
348 
354  {
355  protected:
356  uint32_t m_NumOfPacketsWritten;
357  uint32_t m_NumOfPacketsNotWritten;
358 
359  IFileWriterDevice(const std::string& fileName);
360 
361  public:
362 
366  virtual ~IFileWriterDevice() {}
367 
368  virtual bool writePacket(RawPacket const& packet) = 0;
369 
370  virtual bool writePackets(const RawPacketVector& packets) = 0;
371 
372  using IFileDevice::open;
373  virtual bool open(bool appendMode) = 0;
374  };
375 
376 
384  {
385  private:
386  pcap_dumper_t* m_PcapDumpHandler;
387  LinkLayerType m_PcapLinkLayerType;
388  bool m_AppendMode;
389  FILE* m_File;
390 
391  // private copy c'tor
393  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
394 
395  void closeFile();
396 
397  public:
404  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
405 
410 
419  bool writePacket(RawPacket const& packet);
420 
428  bool writePackets(const RawPacketVector& packets);
429 
430  //override methods
431 
438  virtual bool open();
439 
450  bool open(bool appendMode);
451 
455  virtual void close();
456 
460  void flush();
461 
466  virtual void getStatistics(PcapStats& stats) const;
467  };
468 
469 
478  {
479  private:
480  void* m_LightPcapNg;
481  int m_CompressionLevel;
482  BpfFilterWrapper m_BpfWrapper;
483 
484  // private copy c'tor
486  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
487 
488  public:
489 
496  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
497 
502 
518  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp, const std::string& fileComment);
519 
528  bool writePacket(RawPacket const& packet, const std::string& comment);
529 
530  //overridden methods
531 
538  bool writePacket(RawPacket const& packet);
539 
547  bool writePackets(const RawPacketVector& packets);
548 
555  bool open();
556 
566  bool open(bool appendMode);
567 
571  void flush();
572 
576  void close();
577 
582  void getStatistics(PcapStats& stats) const;
583 
589  bool setFilter(std::string filterAsString);
590 
591  };
592 
593 }// namespace pcpp
594 
595 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:25
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:55
std::string getFileName() const
Definition: PcapFileDevice.h:161
Definition: PcapFileDevice.h:247
Definition: PcapDevice.h:27
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:125
Definition: PointerVector.h:24
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:73
Definition: RawPacket.h:252
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:366
Definition: PcapFileDevice.h:477
virtual bool open()=0
Definition: PcapFileDevice.h:383
virtual void close()
Definition: PcapFilter.h:77
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:200
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:409
Definition: PcapFileDevice.h:105
LinkLayerType
Definition: RawPacket.h:25
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:120
virtual void getStatistics(PcapStats &stats) const =0
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:130
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:501
virtual bool setFilter(std::string filterAsString)
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:210
Definition: PcapFileDevice.h:353
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:268
Definition: PcapDevice.h:41