PcapPlusPlus  23.09
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  explicit IFileDevice(const std::string& fileName);
31  virtual ~IFileDevice();
32 
33  public:
34 
38  std::string getFileName() const;
39 
40 
41  //override methods
42 
46  virtual void close();
47  };
48 
49 
55  {
56  protected:
57  uint32_t m_NumOfPacketsRead;
58  uint32_t m_NumOfPacketsNotParsed;
59 
65  IFileReaderDevice(const std::string& fileName);
66 
67  public:
68 
72  virtual ~IFileReaderDevice() {}
73 
77  uint64_t getFileSize() const;
78 
79  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
80 
88  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
89 
96  static IFileReaderDevice* getReader(const std::string& fileName);
97  };
98 
99 
105  {
106  private:
107  LinkLayerType m_PcapLinkLayerType;
108 
109  // private copy c'tor
111  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
112 
113  public:
119  PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
120 
125 
129  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
130 
131 
132  //overridden methods
133 
140  bool getNextPacket(RawPacket& rawPacket);
141 
147  bool open();
148 
153  void getStatistics(PcapStats& stats) const;
154  };
155 
161  {
162  private:
163  #pragma pack(1)
164  /*
165  * File format header.
166  */
167  typedef struct {
168  uint64_t identification_pattern;
169  uint32_t version_number;
170  uint32_t datalink_type;
171  } snoop_file_header_t;
172 
173  /*
174  * Packet record header.
175  */
176  typedef struct {
177  uint32_t original_length; /* original packet length */
178  uint32_t included_length; /* saved packet length */
179  uint32_t packet_record_length;/* total record length */
180  uint32_t ndrops_cumulative; /* cumulative drops */
181  uint32_t time_sec; /* timestamp */
182  uint32_t time_usec; /* microsecond timestamp */
183  } snoop_packet_header_t;
184  #pragma pack()
185 
186  LinkLayerType m_PcapLinkLayerType;
187  std::ifstream m_snoopFile;
188 
189  // private copy c'tor
191  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other);
192 
193  public:
199  SnoopFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET) {}
200 
204  virtual ~SnoopFileReaderDevice();
205 
209  LinkLayerType getLinkLayerType() const { return m_PcapLinkLayerType; }
210 
211 
212  //overridden methods
213 
220  bool getNextPacket(RawPacket& rawPacket);
221 
227  bool open();
228 
233  void getStatistics(PcapStats& stats) const;
234 
238  void close();
239  };
240 
241 
247  {
248  private:
249  void* m_LightPcapNg;
250  BpfFilterWrapper m_BpfWrapper;
251 
252  // private copy c'tor
254  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
255 
256  public:
262  PcapNgFileReaderDevice(const std::string& fileName);
263 
268 
275  std::string getOS() const;
276 
283  std::string getHardware() const;
284 
291  std::string getCaptureApplication() const;
292 
299  std::string getCaptureFileComment() const;
300 
309  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
310 
311  //overridden methods
312 
319  bool getNextPacket(RawPacket& rawPacket);
320 
326  bool open();
327 
332  void getStatistics(PcapStats& stats) const;
333 
339  bool setFilter(std::string filterAsString);
340 
344  void close();
345  };
346 
347 
353  {
354  protected:
355  uint32_t m_NumOfPacketsWritten;
356  uint32_t m_NumOfPacketsNotWritten;
357 
358  IFileWriterDevice(const std::string& fileName);
359 
360  public:
361 
365  virtual ~IFileWriterDevice() {}
366 
367  virtual bool writePacket(RawPacket const& packet) = 0;
368 
369  virtual bool writePackets(const RawPacketVector& packets) = 0;
370 
371  using IFileDevice::open;
372  virtual bool open(bool appendMode) = 0;
373  };
374 
375 
383  {
384  private:
385  pcap_dumper_t* m_PcapDumpHandler;
386  LinkLayerType m_PcapLinkLayerType;
387  bool m_AppendMode;
388  FILE* m_File;
389 
390  // private copy c'tor
392  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
393 
394  void closeFile();
395 
396  public:
403  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET);
404 
409 
418  bool writePacket(RawPacket const& packet);
419 
427  bool writePackets(const RawPacketVector& packets);
428 
429  //override methods
430 
437  virtual bool open();
438 
449  bool open(bool appendMode);
450 
454  virtual void close();
455 
459  void flush();
460 
465  virtual void getStatistics(PcapStats& stats) const;
466  };
467 
468 
477  {
478  private:
479  void* m_LightPcapNg;
480  int m_CompressionLevel;
481  BpfFilterWrapper m_BpfWrapper;
482 
483  // private copy c'tor
485  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
486 
487  public:
488 
495  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
496 
501 
517  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp, const std::string& fileComment);
518 
527  bool writePacket(RawPacket const& packet, const std::string& comment);
528 
529  //overridden methods
530 
537  bool writePacket(RawPacket const& packet);
538 
546  bool writePackets(const RawPacketVector& packets);
547 
554  bool open();
555 
565  bool open(bool appendMode);
566 
570  void flush();
571 
575  void close();
576 
581  void getStatistics(PcapStats& stats) const;
582 
588  bool setFilter(std::string filterAsString);
589 
590  };
591 
592 }// namespace pcpp
593 
594 #endif
The main namespace for the PcapPlusPlus lib.
Definition: PcapFileDevice.h:25
Definition: RawPacket.h:30
Definition: PcapFileDevice.h:54
std::string getFileName() const
Definition: PcapFileDevice.h:160
Definition: PcapFileDevice.h:246
Definition: PcapDevice.h:27
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:124
Definition: PointerVector.h:24
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:72
Definition: RawPacket.h:254
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:365
Definition: PcapFileDevice.h:476
virtual bool open()=0
Definition: PcapFileDevice.h:382
virtual void close()
Definition: PcapFilter.h:77
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:199
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:408
Definition: PcapFileDevice.h:104
LinkLayerType
Definition: RawPacket.h:25
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:119
virtual void getStatistics(PcapStats &stats) const =0
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:129
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:500
virtual bool setFilter(std::string filterAsString)
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:209
Definition: PcapFileDevice.h:352
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:267
Definition: PcapDevice.h:41