PcapPlusPlus  Next
PcapFileDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "PcapDevice.h"
4 #include "RawPacket.h"
5 #include <fstream>
6 
7 // forward declaration for structs and typedefs defined in pcap.h
8 struct pcap_dumper;
9 typedef struct pcap_dumper pcap_dumper_t;
10 
12 
17 namespace pcpp
18 {
24  enum class FileTimestampPrecision : int8_t
25  {
27  Unknown = -1,
29  Microseconds = 0,
31  Nanoseconds = 1
32  };
33 
38  class IFileDevice : public IPcapDevice
39  {
40  protected:
41  std::string m_FileName;
42 
43  explicit IFileDevice(const std::string& fileName);
44  virtual ~IFileDevice();
45 
46  public:
50  std::string getFileName() const;
51 
52  // override methods
53 
57  void close() override;
58  };
59 
65  {
66  protected:
67  uint32_t m_NumOfPacketsRead;
68  uint32_t m_NumOfPacketsNotParsed;
69 
75  IFileReaderDevice(const std::string& fileName);
76 
77  public:
82  {}
83 
87  uint64_t getFileSize() const;
88 
89  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
90 
98  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
99 
107  static IFileReaderDevice* getReader(const std::string& fileName);
108  };
109 
116  {
117  private:
118  FileTimestampPrecision m_Precision;
119  LinkLayerType m_PcapLinkLayerType;
120 
121  // private copy c'tor
123  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other);
124 
125  public:
131  PcapFileReaderDevice(const std::string& fileName)
132  : IFileReaderDevice(fileName), m_Precision(FileTimestampPrecision::Unknown),
133  m_PcapLinkLayerType(LINKTYPE_ETHERNET)
134  {}
135 
140  {}
141 
146  {
147  return m_PcapLinkLayerType;
148  }
149 
156  {
157  return m_Precision;
158  }
159 
165 
166  // overridden methods
167 
174  bool getNextPacket(RawPacket& rawPacket);
175 
181  bool open();
182 
188  void getStatistics(PcapStats& stats) const;
189  };
190 
197  {
198  private:
199 #pragma pack(1)
200  /*
201  * File format header.
202  */
203  typedef struct
204  {
205  uint64_t identification_pattern;
206  uint32_t version_number;
207  uint32_t datalink_type;
208  } snoop_file_header_t;
209 
210  /*
211  * Packet record header.
212  */
213  typedef struct
214  {
215  uint32_t original_length; /* original packet length */
216  uint32_t included_length; /* saved packet length */
217  uint32_t packet_record_length; /* total record length */
218  uint32_t ndrops_cumulative; /* cumulative drops */
219  uint32_t time_sec; /* timestamp */
220  uint32_t time_usec; /* microsecond timestamp */
221  } snoop_packet_header_t;
222 #pragma pack()
223 
224  LinkLayerType m_PcapLinkLayerType;
225  std::ifstream m_snoopFile;
226 
227  // private copy c'tor
229  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other);
230 
231  public:
237  SnoopFileReaderDevice(const std::string& fileName)
238  : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET)
239  {}
240 
245 
250  {
251  return m_PcapLinkLayerType;
252  }
253 
254  // overridden methods
255 
262  bool getNextPacket(RawPacket& rawPacket);
263 
269  bool open();
270 
276  void getStatistics(PcapStats& stats) const;
277 
281  void close();
282  };
283 
290  {
291  private:
292  void* m_LightPcapNg;
293  BpfFilterWrapper m_BpfWrapper;
294 
295  // private copy c'tor
297  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other);
298 
299  public:
305  PcapNgFileReaderDevice(const std::string& fileName);
306 
311  {
312  close();
313  }
314 
321  std::string getOS() const;
322 
329  std::string getHardware() const;
330 
337  std::string getCaptureApplication() const;
338 
345  std::string getCaptureFileComment() const;
346 
356  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
357 
358  // overridden methods
359 
366  bool getNextPacket(RawPacket& rawPacket);
367 
373  bool open();
374 
379  void getStatistics(PcapStats& stats) const;
380 
387  bool setFilter(std::string filterAsString);
388 
392  void close();
393  };
394 
400  {
401  protected:
402  uint32_t m_NumOfPacketsWritten;
403  uint32_t m_NumOfPacketsNotWritten;
404 
405  IFileWriterDevice(const std::string& fileName);
406 
407  public:
412  {}
413 
414  virtual bool writePacket(RawPacket const& packet) = 0;
415 
416  virtual bool writePackets(const RawPacketVector& packets) = 0;
417 
418  using IFileDevice::open;
419  virtual bool open(bool appendMode) = 0;
420  };
421 
429  {
430  private:
431  pcap_dumper_t* m_PcapDumpHandler;
432  LinkLayerType m_PcapLinkLayerType;
433  bool m_AppendMode;
434  FileTimestampPrecision m_Precision;
435  FILE* m_File;
436 
437  // private copy c'tor
439  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other);
440 
441  void closeFile();
442 
443  public:
454  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET,
455  bool nanosecondsPrecision = false);
456 
461  {
463  }
464 
473  bool writePacket(RawPacket const& packet) override;
474 
483  bool writePackets(const RawPacketVector& packets) override;
484 
489  {
490  return m_Precision;
491  }
492 
498 
499  // override methods
500 
507  bool open() override;
508 
519  bool open(bool appendMode) override;
520 
524  void close() override;
525 
529  void flush();
530 
535  void getStatistics(PcapStats& stats) const override;
536  };
537 
546  {
547  private:
548  void* m_LightPcapNg;
549  int m_CompressionLevel;
550  BpfFilterWrapper m_BpfWrapper;
551 
552  // private copy c'tor
554  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other);
555 
556  public:
565  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
566 
571  {
572  close();
573  }
574 
590  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp,
591  const std::string& fileComment);
592 
603  bool writePacket(RawPacket const& packet, const std::string& comment);
604 
605  // overridden methods
606 
614  bool writePacket(RawPacket const& packet);
615 
624  bool writePackets(const RawPacketVector& packets);
625 
632  bool open();
633 
643  bool open(bool appendMode);
644 
648  void flush();
649 
653  void close();
654 
659  void getStatistics(PcapStats& stats) const;
660 
667  bool setFilter(std::string filterAsString);
668  };
669 
670 } // namespace pcpp
Definition: PcapFilter.h:92
virtual bool open()=0
Definition: PcapFileDevice.h:39
std::string getFileName() const
void close() override
Definition: PcapFileDevice.h:65
int getNextPackets(RawPacketVector &packetVec, int numOfPacketsToRead=-1)
uint64_t getFileSize() const
static IFileReaderDevice * getReader(const std::string &fileName)
IFileReaderDevice(const std::string &fileName)
virtual ~IFileReaderDevice()
Definition: PcapFileDevice.h:81
Definition: PcapFileDevice.h:400
virtual ~IFileWriterDevice()
Definition: PcapFileDevice.h:411
Definition: PcapDevice.h:114
Definition: PcapFileDevice.h:116
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:131
static bool isNanoSecondPrecisionSupported()
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:155
virtual ~PcapFileReaderDevice()
Definition: PcapFileDevice.h:139
void getStatistics(PcapStats &stats) const
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:145
bool getNextPacket(RawPacket &rawPacket)
Definition: PcapFileDevice.h:429
bool open(bool appendMode) override
void getStatistics(PcapStats &stats) const override
PcapFileWriterDevice(const std::string &fileName, LinkLayerType linkLayerType=LINKTYPE_ETHERNET, bool nanosecondsPrecision=false)
bool writePacket(RawPacket const &packet) override
~PcapFileWriterDevice()
Definition: PcapFileDevice.h:460
static bool isNanoSecondPrecisionSupported()
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:488
bool writePackets(const RawPacketVector &packets) override
Definition: PcapFileDevice.h:290
std::string getOS() const
virtual ~PcapNgFileReaderDevice()
Definition: PcapFileDevice.h:310
void getStatistics(PcapStats &stats) const
std::string getCaptureApplication() const
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
std::string getCaptureFileComment() const
bool getNextPacket(RawPacket &rawPacket)
PcapNgFileReaderDevice(const std::string &fileName)
std::string getHardware() const
bool setFilter(std::string filterAsString)
Definition: PcapFileDevice.h:546
bool writePackets(const RawPacketVector &packets)
bool writePacket(RawPacket const &packet)
bool setFilter(std::string filterAsString)
void getStatistics(PcapStats &stats) const
bool open(const std::string &os, const std::string &hardware, const std::string &captureApp, const std::string &fileComment)
bool open(bool appendMode)
PcapNgFileWriterDevice(const std::string &fileName, int compressionLevel=0)
virtual ~PcapNgFileWriterDevice()
Definition: PcapFileDevice.h:570
bool writePacket(RawPacket const &packet, const std::string &comment)
Definition: PointerVector.h:58
Definition: RawPacket.h:269
Definition: PcapFileDevice.h:197
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:237
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:249
void getStatistics(PcapStats &stats) const
bool getNextPacket(RawPacket &rawPacket)
The main namespace for the PcapPlusPlus lib.
FileTimestampPrecision
Definition: PcapFileDevice.h:25
@ Microseconds
Precision is in microseconds.
@ Unknown
Precision is unknown or not set/determined.
@ Nanoseconds
Precision is in nanoseconds.
LinkLayerType
Definition: RawPacket.h:25
@ LINKTYPE_ETHERNET
Definition: RawPacket.h:29
Definition: PcapDevice.h:128