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 
214  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
215  uint32_t& capturedLength, uint32_t& frameLength);
216  };
217 
224  {
225  public:
236  PcapFileWriterDevice(const std::string& fileName, LinkLayerType linkLayerType = LINKTYPE_ETHERNET,
237  bool nanosecondsPrecision = false);
238 
239  PcapFileWriterDevice(const PcapFileWriterDevice& other) = delete;
240  PcapFileWriterDevice& operator=(const PcapFileWriterDevice& other) = delete;
241 
248  bool writePacket(RawPacket const& packet) override;
249 
257  bool writePackets(const RawPacketVector& packets) override;
258 
261  {
262  return m_Precision;
263  }
264 
268  PCPP_DEPRECATED("Nanosecond precision is now natively supported by the internal parser and always returns true")
270  {
271  return true;
272  }
273 
274  LinkLayerType getLinkLayerType() const
275  {
276  return m_PcapLinkLayerType;
277  }
278 
283  bool open() override;
284 
293  bool open(bool appendMode) override;
294 
296  bool isOpened() const override
297  {
298  return m_PcapFile.is_open();
299  }
300 
302  void close() override;
303 
305  void flush();
306 
307  private:
308  LinkLayerType m_PcapLinkLayerType = LINKTYPE_ETHERNET;
309  bool m_NeedsSwap = false;
311  std::fstream m_PcapFile;
312 
313  struct CheckHeaderResult
314  {
315  enum class Result
316  {
317  HeaderOk,
318  HeaderError,
319  HeaderNeeded
320  };
321 
322  Result result;
323  std::string error;
324  bool needsSwap = false;
325 
326  static CheckHeaderResult fromOk(bool needsSwap)
327  {
328  return { Result::HeaderOk, "", needsSwap };
329  }
330 
331  static CheckHeaderResult fromError(const std::string& error)
332  {
333  return { Result::HeaderError, error };
334  }
335 
336  static CheckHeaderResult fromHeaderNeeded()
337  {
338  return { Result::HeaderNeeded };
339  }
340  };
341 
342  static bool writeHeader(std::fstream& pcapFile, FileTimestampPrecision precision, uint32_t snaplen,
343  LinkLayerType linkType);
344  static CheckHeaderResult checkHeader(std::fstream& pcapFile, FileTimestampPrecision requestedPrecision,
345  LinkLayerType requestedLinkType);
346  };
347 
352  {
353  private:
354  internal::LightPcapNgHandle* m_LightPcapNg;
355 
356  public:
360  PcapNgFileReaderDevice(const std::string& fileName);
361 
364  {
366  }
367 
368  PcapNgFileReaderDevice(const PcapNgFileReaderDevice& other) = delete;
369  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice& other) = delete;
370 
375  std::string getOS() const;
376 
381  std::string getHardware() const;
382 
387  std::string getCaptureApplication() const;
388 
393  std::string getCaptureFileComment() const;
394 
402  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
403 
404  // overridden methods
405 
410  bool getNextPacket(RawPacket& rawPacket) override;
411 
415  bool open() override;
416 
418  bool isOpened() const override
419  {
420  return m_LightPcapNg != nullptr;
421  }
422 
424  void close() override;
425 
426  private:
427  bool getNextPacketInternal(RawPacket& rawPacket, std::string* packetComment);
428  };
429 
436  {
437  private:
438  internal::LightPcapNgHandle* m_LightPcapNg;
439  int m_CompressionLevel;
440 
441  public:
448  PcapNgFileWriterDevice(const std::string& fileName, int compressionLevel = 0);
449 
452  {
454  }
455 
456  PcapNgFileWriterDevice(const PcapFileWriterDevice& other) = delete;
457  PcapNgFileWriterDevice& operator=(const PcapNgFileWriterDevice& other) = delete;
458 
467  bool writePacket(RawPacket const& packet, const std::string& comment);
468 
469  // overridden methods
470 
476  bool writePacket(RawPacket const& packet) override;
477 
485  bool writePackets(const RawPacketVector& packets) override;
486 
491  bool open() override;
492 
500  bool open(bool appendMode) override;
501 
515  bool open(const std::string& os, const std::string& hardware, const std::string& captureApp,
516  const std::string& fileComment);
517 
519  bool isOpened() const override
520  {
521  return m_LightPcapNg != nullptr;
522  }
523 
525  void flush();
526 
528  void close() override;
529 
530  private:
534  struct PcapNgMetadata
535  {
537  std::string os;
539  std::string hardware;
541  std::string captureApplication;
543  std::string comment;
544  };
545 
546  bool openWrite(PcapNgMetadata const* metadata = nullptr);
547  bool openAppend();
548  };
549 
554  {
555  private:
556 #pragma pack(1)
558  typedef struct
559  {
560  uint64_t identification_pattern;
561  uint32_t version_number;
562  uint32_t datalink_type;
563  } snoop_file_header_t;
564 
566  typedef struct
567  {
568  uint32_t original_length;
569  uint32_t included_length;
570  uint32_t packet_record_length;
571  uint32_t ndrops_cumulative;
572  uint32_t time_sec;
573  uint32_t time_usec;
574  } snoop_packet_header_t;
575 #pragma pack()
576 
577  LinkLayerType m_PcapLinkLayerType;
578  std::ifstream m_SnoopFile;
579 
580  bool readNextPacket(timespec& packetTimestamp, uint8_t* packetData, uint32_t packetDataLen,
581  uint32_t& capturedLength, uint32_t& frameLength);
582 
583  public:
587  SnoopFileReaderDevice(const std::string& fileName)
588  : IFileReaderDevice(fileName), m_PcapLinkLayerType(LINKTYPE_ETHERNET)
589  {}
590 
593 
594  SnoopFileReaderDevice(const PcapFileReaderDevice& other) = delete;
595  SnoopFileReaderDevice& operator=(const PcapFileReaderDevice& other) = delete;
596 
599  {
600  return m_PcapLinkLayerType;
601  }
602 
603  // overridden methods
604 
609  bool getNextPacket(RawPacket& rawPacket) override;
610 
614  bool open() override;
615 
617  bool isOpened() const override
618  {
619  return m_SnoopFile.is_open();
620  }
621 
623  void close() override;
624  };
625 } // 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:224
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:296
PcapFileWriterDevice(const std::string &fileName, LinkLayerType linkLayerType=LINKTYPE_ETHERNET, bool nanosecondsPrecision=false)
bool writePacket(RawPacket const &packet) override
static bool isNanoSecondPrecisionSupported()
Definition: PcapFileDevice.h:269
FileTimestampPrecision getTimestampPrecision() const
Definition: PcapFileDevice.h:260
bool writePackets(const RawPacketVector &packets) override
Definition: PcapFileDevice.h:352
std::string getOS() const
bool isOpened() const override
Definition: PcapFileDevice.h:418
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:363
std::string getCaptureFileComment() const
PcapNgFileReaderDevice(const std::string &fileName)
std::string getHardware() const
void close() override
Close the pacp-ng file.
Definition: PcapFileDevice.h:436
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:519
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:451
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:261
Definition: PcapFileDevice.h:554
void close() override
Close the snoop file.
bool getNextPacket(RawPacket &rawPacket) override
SnoopFileReaderDevice(const std::string &fileName)
Definition: PcapFileDevice.h:587
LinkLayerType getLinkLayerType() const
Definition: PcapFileDevice.h:598
~SnoopFileReaderDevice() override
A destructor for this class.
bool isOpened() const override
Definition: PcapFileDevice.h:617
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