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 
117  static IFileReaderDevice* getReader(const std::string& fileName);
118  };
119 
124  {
125  protected:
126  IFileWriterDevice(const std::string& fileName);
127 
128  public:
130  ~IFileWriterDevice() override = default;
131 
132  virtual bool writePacket(RawPacket const& packet) = 0;
133 
134  virtual bool writePackets(const RawPacketVector& packets) = 0;
135 
136  using IFileDevice::open;
137  virtual bool open(bool appendMode) = 0;
138  };
139 
144  {
145  public:
149  explicit PcapFileReaderDevice(const std::string& fileName) : IFileReaderDevice(fileName)
150  {}
151 
153  ~PcapFileReaderDevice() override = default;
154 
155  PcapFileReaderDevice(const PcapFileReaderDevice& other) = delete;
156  PcapFileReaderDevice& operator=(const PcapFileReaderDevice& other) = delete;
157 
160  {
161  return m_PcapLinkLayerType;
162  }
163 
166  {
167  return m_Precision;
168  }
169 
171  uint32_t getSnapshotLength() const
172  {
173  return m_SnapshotLength;
174  }
175 
179  PCPP_DEPRECATED("Nanosecond precision is now natively supported by the internal parser and always returns true")
181  {
182  return true;
183  }
184 
185  // overridden methods
186 
188  bool isOpened() const override
189  {
190  return m_PcapFile.is_open();
191  }
192 
197  bool getNextPacket(RawPacket& rawPacket) override;
198 
202  bool open() override;
203 
205  void close() override;
206 
207  private:
209  LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
210  std::ifstream m_PcapFile;
211  bool m_NeedsSwap = false;
212  uint32_t m_SnapshotLength = 0;
213  std::vector<uint8_t> m_ReadBuffer;
214 
215  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
216  uint32_t& capturedLength, uint32_t& frameLength);
217  };
218 
225  {
226  public:
237  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET,
238  bool nanosecondsPrecision = false);
239 
240  PcapFileWriterDevice(const PcapFileWriterDevice& other) = delete;
241  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other) = delete;
242 
249  bool writePacket(RawPacket const& packet) override;
250 
258  bool writePackets(const RawPacketVector& packets) override;
259 
262  {
263  return m_Precision;
264  }
265 
269  PCPP_DEPRECATED("Nanosecond precision is now natively supported by the internal parser and always returns true")
271  {
272  return true;
273  }
274 
275  LinkLayerType getLinkLayerType() const
276  {
277  return m_PcapLinkLayerType;
278  }
279 
284  bool open() override;
285 
294  bool open(bool appendMode) override;
295 
297  bool isOpened() const override
298  {
299  return m_PcapFile.is_open();
300  }
301 
303  void close() override;
304 
306  void flush();
307 
308  private:
309  LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
310  bool m_NeedsSwap = false;
312  std::fstream m_PcapFile;
313 
314  struct CheckHeaderResult
315  {
316  enum class Result
317  {
318  HeaderOk,
319  HeaderError,
320  HeaderNeeded
321  };
322 
323  Result result;
324  std::string error;
325  bool needsSwap = false;
326 
327  static CheckHeaderResult fromOk(bool needsSwap)
328  {
329  return { Result::HeaderOk, "", needsSwap };
330  }
331 
332  static CheckHeaderResult fromError(const std::string& error)
333  {
334  return { Result::HeaderError, error };
335  }
336 
337  static CheckHeaderResult fromHeaderNeeded()
338  {
339  return { Result::HeaderNeeded };
340  }
341  };
342 
343  static bool writeHeader(std::fstream& pcapFile, FileTimestampPrecision precision, uint32_t snaplen,
344  LinkLayerType linkType);
345  static CheckHeaderResult checkHeader(std::fstream& pcapFile, FileTimestampPrecision requestedPrecision,
346  LinkLayerType requestedLinkType);
347  };
348 
353  {
354  private:
355  internal::LightPcapNgHandle* m_LightPcapNg;
356 
357  public:
361  PcapNgFileReaderDevice(const std::string& fileName);
362 
365  {
367  }
368 
369  PcapNgFileReaderDevice(const PcapNgFileReaderDevice& other) = delete;
370  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other) = delete;
371 
376  std::string getOS() const;
377 
382  std::string getHardware() const;
383 
388  std::string getCaptureApplication() const;
389 
394  std::string getCaptureFileComment() const;
395 
403  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
404 
405  // overridden methods
406 
411  bool getNextPacket(RawPacket& rawPacket) override;
412 
416  bool open() override;
417 
419  bool isOpened() const override
420  {
421  return m_LightPcapNg != nullptr;
422  }
423 
425  void close() override;
426 
427  private:
428  bool getNextPacketInternal(RawPacket& rawPacket, std::string* packetComment);
429  };
430 
437  {
438  private:
439  internal::LightPcapNgHandle* m_LightPcapNg;
440  int m_CompressionLevel;
441 
442  public:
449  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
450 
453  {
455  }
456 
457  PcapNgFileWriterDevice(const PcapFileWriterDevice& other) = delete;
458  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other) = delete;
459 
468  bool writePacket(RawPacket const& packet, const std::string& comment);
469 
470  // overridden methods
471 
477  bool writePacket(RawPacket const& packet) override;
478 
486  bool writePackets(const RawPacketVector& packets) override;
487 
492  bool open() override;
493 
501  bool open(bool appendMode) override;
502 
516  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp,
517  const std::string& fileComment);
518 
520  bool isOpened() const override
521  {
522  return m_LightPcapNg != nullptr;
523  }
524 
526  void flush();
527 
529  void close() override;
530 
531  private:
535  struct PcapNgMetadata
536  {
538  std::string os;
540  std::string hardware;
542  std::string captureApplication;
544  std::string comment;
545  };
546 
547  bool openWrite(PcapNgMetadata const* metadata = nullptr);
548  bool openAppend();
549  };
550 
555  {
556  private:
557 #pragma pack(1)
559  typedef struct
560  {
561  uint64_t identification_pattern;
562  uint32_t version_number;
563  uint32_t datalink_type;
564  } snoop_file_header_t;
565 
567  typedef struct
568  {
569  uint32_t original_length;
570  uint32_t included_length;
571  uint32_t packet_record_length;
572  uint32_t ndrops_cumulative;
573  uint32_t time_sec;
574  uint32_t time_usec;
575  } snoop_packet_header_t;
576 #pragma pack()
577 
578  LinkLayerType m_PcapLinkLayerType;
579  std::ifstream m_SnoopFile;
580 
581  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
582  uint32_t& capturedLength, uint32_t& frameLength);
583 
584  public:
588  SnoopFileReaderDevice(const std::string& fileName)
589  : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET)
590  {}
591 
594 
595  SnoopFileReaderDevice(const PcapFileReaderDevice& other) = delete;
596  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other) = delete;
597 
600  {
601  return m_PcapLinkLayerType;
602  }
603 
604  // overridden methods
605 
610  bool getNextPacket(RawPacket& rawPacket) override;
611 
615  bool open() override;
616 
618  bool isOpened() const override
619  {
620  return m_SnoopFile.is_open();
621  }
622 
624  void close() override;
625  };
626 } // 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)
uint64_t getFileSize() const
static IFileReaderDevice * getReader(const std::string &fileName)
~IFileReaderDevice() override=default
A destructor for this class.
IFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:124
~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:144
PcapFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:149
bool getNextPacket(RawPacket &rawPacket) override
void close() override
Close the pacp file.
static bool isNanoSecondPrecisionSupported()
Definition: PcapFileDevice.h:180
bool isOpened() const override
Definition: PcapFileDevice.h:188
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:165
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:159
~PcapFileReaderDevice() override=default
A destructor for this class.
uint32_t getSnapshotLength() const
Definition: PcapFileDevice.h:171
Definition: PcapFileDevice.h:225
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:297
PcapFileWriterDevice(const std::string &fileName, LinkLayerType linkLayerType=LINKTYPE_ETHERNET, bool nanosecondsPrecision=false)
bool writePacket(RawPacket const &packet) override
static bool isNanoSecondPrecisionSupported()
Definition: PcapFileDevice.h:270
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:261
bool writePackets(const RawPacketVector &packets) override
Definition: PcapFileDevice.h:353
std::string getOS() const
bool isOpened() const override
Definition: PcapFileDevice.h:419
std::string getCaptureApplication() const
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
bool getNextPacket(RawPacket &rawPacket) override
~PcapNgFileReaderDevice() override
A destructor for this class.
Definition: PcapFileDevice.h:364
std::string getCaptureFileComment() const
PcapNgFileReaderDevice(const std::string &fileName)
std::string getHardware() const
void close() override
Close the pacp-ng file.
Definition: PcapFileDevice.h:437
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:520
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
~PcapNgFileWriterDevice() override
A destructor for this class.
Definition: PcapFileDevice.h:452
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:555
void close() override
Close the snoop file.
bool getNextPacket(RawPacket &rawPacket) override
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:588
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:599
~SnoopFileReaderDevice() override
A destructor for this class.
bool isOpened() const override
Definition: PcapFileDevice.h:618
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