PcapPlusPlus  Next
PcapFileDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Device.h"
4 #include "PcapDevice.h"
5 #include "RawPacket.h"
6 #include <fstream>
7 
9 
12 namespace pcpp
13 {
14  namespace internal
15  {
18  struct LightPcapNgHandle;
19  } // namespace internal
20 
24  enum class FileTimestampPrecision : int8_t
25  {
27  Unknown = -1,
29  Microseconds = 0,
31  Nanoseconds = 1
32  };
33 
37  {
38  protected:
39  std::string m_FileName;
40  BpfFilterWrapper m_BpfWrapper;
41 
42  explicit IFileDevice(const std::string& fileName);
43 
44  bool doUpdateFilter(std::string const* filterAsString) override;
45 
46  public:
48  std::string getFileName() const;
49 
50  // override methods
51 
60  void getStatistics(PcapStats& stats) const override;
61 
62  protected:
65  void reportPacketProcessed(uint64_t numPackets = 1)
66  {
67  m_NumOfPacketsProcessed += numPackets;
68  }
69 
72  void reportPacketDropped(uint64_t numPackets = 1)
73  {
74  m_NumOfPacketsDropped += numPackets;
75  }
76 
79 
80  private:
81  uint64_t m_NumOfPacketsProcessed = 0;
82  uint64_t m_NumOfPacketsDropped = 0;
83  };
84 
89  {
90  protected:
94  IFileReaderDevice(const std::string& fileName);
95 
96  public:
98  ~IFileReaderDevice() override = default;
99 
101  uint64_t getFileSize() const;
102 
103  virtual bool getNextPacket(RawPacket& rawPacket) = 0;
104 
110  int getNextPackets(RawPacketVector& packetVec, int numOfPacketsToRead = -1);
111 
119  PCPP_DEPRECATED("Prefer `tryCreateReader` due to selection of reader based on file content.")
120  static IFileReaderDevice* getReader(const std::string& fileName);
121 
131  static std::unique_ptr<IFileReaderDevice> createReader(const std::string& fileName);
132 
142  static std::unique_ptr<IFileReaderDevice> tryCreateReader(const std::string& fileName);
143  };
144 
149  {
150  protected:
151  IFileWriterDevice(const std::string& fileName);
152 
153  public:
155  ~IFileWriterDevice() override = default;
156 
157  virtual bool writePacket(RawPacket const& packet) = 0;
158 
159  virtual bool writePackets(const RawPacketVector& packets) = 0;
160 
161  using IFileDevice::open;
162  virtual bool open(bool appendMode) = 0;
163  };
164 
169  {
170  public:
174  explicit PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName)
175  {}
176 
178  ~PcapFileReaderDevice() override = default;
179 
180  PcapFileReaderDevice(const PcapFileReaderDevice& other) = delete;
181  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other) = delete;
182 
185  {
186  return m_PcapLinkLayerType;
187  }
188 
191  {
192  return m_Precision;
193  }
194 
196  uint32_t getSnapshotLength() const
197  {
198  return m_SnapshotLength;
199  }
200 
204  PCPP_DEPRECATED("Nanosecond precision is now natively supported by the internal parser and always returns true")
205  static bool isNanoSecondPrecisionSupported()
206  {
207  return true;
208  }
209 
210  // overridden methods
211 
213  bool isOpened() const override
214  {
215  return m_PcapFile.is_open();
216  }
217 
222  bool getNextPacket(RawPacket& rawPacket) override;
223 
227  bool open() override;
228 
230  void close() override;
231 
232  private:
234  LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
235  std::ifstream m_PcapFile;
236  bool m_NeedsSwap = false;
237  uint32_t m_SnapshotLength = 0;
238  std::vector<uint8_t> m_ReadBuffer;
239 
240  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
241  uint32_t& capturedLength, uint32_t& frameLength);
242  };
243 
250  {
251  public:
262  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET,
263  bool nanosecondsPrecision = false);
264 
265  PcapFileWriterDevice(const PcapFileWriterDevice& other) = delete;
266  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other) = delete;
267 
274  bool writePacket(RawPacket const& packet) override;
275 
283  bool writePackets(const RawPacketVector& packets) override;
284 
287  {
288  return m_Precision;
289  }
290 
294  PCPP_DEPRECATED("Nanosecond precision is now natively supported by the internal parser and always returns true")
295  static bool isNanoSecondPrecisionSupported()
296  {
297  return true;
298  }
299 
300  LinkLayerType getLinkLayerType() const
301  {
302  return m_PcapLinkLayerType;
303  }
304 
309  bool open() override;
310 
319  bool open(bool appendMode) override;
320 
322  bool isOpened() const override
323  {
324  return m_PcapFile.is_open();
325  }
326 
328  void close() override;
329 
331  void flush();
332 
333  private:
334  static bool writeHeader(std::ostream& outStream, FileTimestampPrecision precision, uint32_t snaplen,
335  LinkLayerType linkType);
336 
337  LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
338  bool m_NeedsSwap = false;
340  std::fstream m_PcapFile;
341  };
342 
347  {
348  private:
349  internal::LightPcapNgHandle* m_LightPcapNg;
350 
351  public:
354  static bool isZstdSupported();
355 
359  PcapNgFileReaderDevice(const std::string& fileName);
360 
363  {
365  }
366 
367  PcapNgFileReaderDevice(const PcapNgFileReaderDevice& other) = delete;
368  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other) = delete;
369 
374  std::string getOS() const;
375 
380  std::string getHardware() const;
381 
386  std::string getCaptureApplication() const;
387 
392  std::string getCaptureFileComment() const;
393 
401  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
402 
403  // overridden methods
404 
409  bool getNextPacket(RawPacket& rawPacket) override;
410 
414  bool open() override;
415 
417  bool isOpened() const override
418  {
419  return m_LightPcapNg != nullptr;
420  }
421 
423  void close() override;
424 
425  private:
426  bool getNextPacketInternal(RawPacket& rawPacket, std::string* packetComment);
427  };
428 
435  {
436  private:
437  internal::LightPcapNgHandle* m_LightPcapNg;
438  int m_CompressionLevel;
439 
440  public:
443  static bool isZstdSupported();
444 
451  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
452 
455  {
457  }
458 
459  PcapNgFileWriterDevice(const PcapFileWriterDevice& other) = delete;
460  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other) = delete;
461 
470  bool writePacket(RawPacket const& packet, const std::string& comment);
471 
472  // overridden methods
473 
479  bool writePacket(RawPacket const& packet) override;
480 
488  bool writePackets(const RawPacketVector& packets) override;
489 
494  bool open() override;
495 
503  bool open(bool appendMode) override;
504 
518  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp,
519  const std::string& fileComment);
520 
522  bool isOpened() const override
523  {
524  return m_LightPcapNg != nullptr;
525  }
526 
528  void flush();
529 
531  void close() override;
532 
533  private:
537  struct PcapNgMetadata
538  {
540  std::string os;
542  std::string hardware;
544  std::string captureApplication;
546  std::string comment;
547  };
548 
549  bool openWrite(PcapNgMetadata const* metadata = nullptr);
550  bool openAppend();
551  };
552 
557  {
558  private:
559 #pragma pack(1)
561  typedef struct
562  {
563  uint64_t identification_pattern;
564  uint32_t version_number;
565  uint32_t datalink_type;
566  } snoop_file_header_t;
567 
569  typedef struct
570  {
571  uint32_t original_length;
572  uint32_t included_length;
573  uint32_t packet_record_length;
574  uint32_t ndrops_cumulative;
575  uint32_t time_sec;
576  uint32_t time_usec;
577  } snoop_packet_header_t;
578 #pragma pack()
579 
580  LinkLayerType m_PcapLinkLayerType;
581  std::ifstream m_SnoopFile;
582  std::vector<uint8_t> m_ReadBuffer;
583 
584  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
585  uint32_t& capturedLength, uint32_t& frameLength);
586 
587  public:
591  SnoopFileReaderDevice(const std::string& fileName)
592  : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET)
593  {}
594 
597 
598  SnoopFileReaderDevice(const PcapFileReaderDevice& other) = delete;
599  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other) = delete;
600 
603  {
604  return m_PcapLinkLayerType;
605  }
606 
607  // overridden methods
608 
613  bool getNextPacket(RawPacket& rawPacket) override;
614 
618  bool open() override;
619 
621  bool isOpened() const override
622  {
623  return m_SnoopFile.is_open();
624  }
625 
627  void close() override;
628  };
629 } // namespace pcpp
Definition: PcapFilter.h:80
virtual bool open()=0
Definition: PcapFileDevice.h:37
bool doUpdateFilter(std::string const *filterAsString) override
Updates the filter on the device with a BPF string.
std::string getFileName() const
void reportPacketProcessed(uint64_t numPackets=1)
Report that packets were processed (read or written, depending on the device type).
Definition: PcapFileDevice.h:65
void reportPacketDropped(uint64_t numPackets=1)
Report that packets were dropped (not read or not written, depending on the device type).
Definition: PcapFileDevice.h:72
void getStatistics(PcapStats &stats) const override
Get the statistics for this device.
void resetStatisticCounters()
Reset the internal statistic counters to zero.
Definition: PcapFileDevice.h:89
int getNextPackets(RawPacketVector &packetVec, int numOfPacketsToRead=-1)
static std::unique_ptr< IFileReaderDevice > createReader(const std::string &fileName)
Creates an instance of the reader best fit to read the file.
uint64_t getFileSize() const
static IFileReaderDevice * getReader(const std::string &fileName)
~IFileReaderDevice() override=default
A destructor for this class.
IFileReaderDevice(const std::string &fileName)
static std::unique_ptr< IFileReaderDevice > tryCreateReader(const std::string &fileName)
Tries to create an instance of the reader best fit to read the file.
Definition: PcapFileDevice.h:149
~IFileWriterDevice() override=default
A destructor for this class.
Definition: Device.h:44
An interface for providing Pcap-based device statistics.
Definition: PcapDevice.h:25
Definition: PcapFileDevice.h:169
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:174
bool getNextPacket(RawPacket &rawPacket) override
void close() override
Close the pacp file.
bool isOpened() const override
Definition: PcapFileDevice.h:213
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:190
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:184
~PcapFileReaderDevice() override=default
A destructor for this class.
uint32_t getSnapshotLength() const
Definition: PcapFileDevice.h:196
Definition: PcapFileDevice.h:250
bool open(bool appendMode) override
void flush()
Flush packets to disk.
void close() override
Flush and close the pacp file.
bool isOpened() const override
Definition: PcapFileDevice.h:322
PcapFileWriterDevice(const std::string &fileName, LinkLayerType linkLayerType=LINKTYPE_ETHERNET, bool nanosecondsPrecision=false)
bool writePacket(RawPacket const &packet) override
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:286
bool writePackets(const RawPacketVector &packets) override
Definition: PcapFileDevice.h:347
std::string getOS() const
bool isOpened() const override
Definition: PcapFileDevice.h:417
std::string getCaptureApplication() const
static bool isZstdSupported()
A static method that checks if the device was built with zstd compression support.
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
bool getNextPacket(RawPacket &rawPacket) override
~PcapNgFileReaderDevice() override
A destructor for this class.
Definition: PcapFileDevice.h:362
std::string getCaptureFileComment() const
PcapNgFileReaderDevice(const std::string &fileName)
std::string getHardware() const
void close() override
Close the pacp-ng file.
Definition: PcapFileDevice.h:435
bool open(const std::string &os, const std::string &hardware, const std::string &captureApp, const std::string &fileComment)
bool isOpened() const override
Definition: PcapFileDevice.h:522
PcapNgFileWriterDevice(const std::string &fileName, int compressionLevel=0)
bool writePacket(RawPacket const &packet) override
void close() override
Flush and close the pcap-ng file.
bool writePackets(const RawPacketVector &packets) override
static bool isZstdSupported()
A static method that checks if the device was built with zstd compression support.
~PcapNgFileWriterDevice() override
A destructor for this class.
Definition: PcapFileDevice.h:454
bool open(bool appendMode) override
bool writePacket(RawPacket const &packet, const std::string &comment)
void flush()
Flush packets to the pcap-ng file.
Definition: PointerVector.h:50
Definition: RawPacket.h:290
Definition: PcapFileDevice.h:557
void close() override
Close the snoop file.
bool getNextPacket(RawPacket &rawPacket) override
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:591
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:602
~SnoopFileReaderDevice() override
A destructor for this class.
bool isOpened() const override
Definition: PcapFileDevice.h:621
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
FileTimestampPrecision
Definition: PcapFileDevice.h:25
@ Microseconds
Precision is in microseconds.
@ Unknown
Precision is unknown or not set/determined.
@ Nanoseconds
Precision is in nanoseconds.
LinkLayerType
An enum describing all known link layer type. Taken from: http://www.tcpdump.org/linktypes....
Definition: RawPacket.h:22
@ LINKTYPE_ETHERNET
IEEE 802.3 Ethernet.
Definition: RawPacket.h:26
Definition: PcapDevice.h:14