PcapPlusPlus  26.07
PcapNgFileReaderDevice.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "RawPacket.h"
4 #include "Packet.h"
5 #include "IFileDevice.h"
6 #include "PcapCommon.h"
7 #include <fstream>
8 #include <vector>
9 #include <string>
10 #include <memory>
11 
13 
14 namespace pcpp
15 {
17 enum class PcapNgBlockType : uint32_t
18 {
20  SectionHeader = 0x0A0D0D0A,
22  InterfaceDescription = 0x00000001,
24  Packet = 0x00000002,
26  SimplePacket = 0x00000003,
28  NameResolution = 0x00000004,
30  InterfaceStatistics = 0x00000005,
32  EnhancedPacket = 0x00000006,
34  Custom = 0x00000BAD,
36  CustomReserved = 0x40000BAD
37 };
38 
39 #pragma pack(push, 1)
42 {
44  uint32_t blockType;
46  uint32_t blockTotalLength;
47 };
48 
51 {
55  uint32_t byteOrderMagic;
57  uint16_t majorVersion;
59  uint16_t minorVersion;
61  int64_t sectionLength;
62  // Options follow
63 };
64 
67 {
71  uint16_t linkType;
73  uint16_t reserved;
75  uint32_t snapLen;
76  // Options follow
77 };
78 
81 {
85  uint32_t interfaceId;
87  uint32_t timestampHigh;
89  uint32_t timestampLow;
91  uint32_t capturedLen;
93  uint32_t originalLen;
94  // Packet data and options follow
95 };
96 
99 {
103  uint32_t originalLen;
104  // Packet data follows
105 };
106 
109 {
111  uint16_t optionCode;
113  uint16_t optionLength;
114 };
115 #pragma pack(pop)
116 
119 {
120 public:
123  explicit PcapNgFileReaderDevice(const std::string& fileName);
124 
127 
130  bool open() override;
131 
133  void close() override;
134 
138  bool getNextPacket(RawPacket& rawPacket) override;
139 
142  bool isOpened() const override
143  {
144  return m_File.is_open();
145  }
146 
149  std::string getOS() const;
150 
153  std::string getHardware() const;
154 
157  std::string getCaptureApplication() const;
158 
161  std::string getCaptureFileComment() const;
162 
167  bool getNextPacket(RawPacket& rawPacket, std::string& packetComment);
168 
170  PcapNgFileReaderDevice& operator=(const PcapNgFileReaderDevice&) = delete;
171 
172 private:
174  std::ifstream m_File;
176  struct SectionInfo
177  {
179  uint32_t byteOrderMagic;
181  uint16_t majorVersion;
183  uint16_t minorVersion;
185  uint16_t linkType;
187  uint32_t snapLen;
189  uint8_t timestampResolution;
191  std::string os;
193  std::string hardware;
195  std::string captureApplication;
197  std::string fileComment;
198  };
199 
201  std::vector<SectionInfo> m_Sections;
203  size_t m_CurrentSection;
205  std::vector<uint8_t> m_ReadBuffer;
206 
210  bool readBlockHeader(PcapNgBlockHeader& blockHeader);
211 
216  bool readSectionHeaderBlock(const PcapNgSectionHeaderBlock& shb, SectionInfo& sectionInfo);
217 
222  bool readInterfaceDescriptionBlock(const PcapNgInterfaceDescriptionBlock& idb, SectionInfo& sectionInfo);
223 
230  bool readEnhancedPacketBlock(const PcapNgEnhancedPacketBlock& epb, const SectionInfo& sectionInfo,
231  RawPacket& rawPacket, std::string* packetComment = nullptr);
232 
238  bool readSimplePacketBlock(const PcapNgSimplePacketBlock& spb, const SectionInfo& sectionInfo,
239  RawPacket& rawPacket);
240 
246  size_t readOptions(const uint8_t* data, size_t length, SectionInfo& sectionInfo);
247 
250  static bool isLittleEndian();
251 
255  static uint16_t swap16(uint16_t value);
256 
260  static uint32_t swap32(uint32_t value);
261 
265  static uint64_t swap64(uint64_t value);
266 };
267 } // namespace pcpp
Definition: PcapFileDevice.h:89
Definition: Packet.h:48
PcapNG file reader device.
Definition: PcapNgFileReaderDevice.h:119
bool open() override
Open the file.
std::string getOS() const
Get OS information from file metadata.
bool isOpened() const override
Get file position.
Definition: PcapNgFileReaderDevice.h:142
std::string getCaptureApplication() const
Get capture application information from file metadata.
bool getNextPacket(RawPacket &rawPacket, std::string &packetComment)
Get next packet with comment.
bool getNextPacket(RawPacket &rawPacket) override
Get next packet from file.
~PcapNgFileReaderDevice() override
Destructor.
std::string getCaptureFileComment() const
Get file comment.
PcapNgFileReaderDevice(const std::string &fileName)
Constructor.
std::string getHardware() const
Get hardware information from file metadata.
void close() override
Close the file.
Definition: RawPacket.h:290
The main namespace for the PcapPlusPlus lib.
Definition: AssertionUtils.h:19
PcapNgBlockType
PcapNG block types.
Definition: PcapNgFileReaderDevice.h:18
@ NameResolution
Name Resolution Block (NRB)
@ CustomReserved
Custom Block that rewrites existing data.
@ InterfaceDescription
Interface Description Block (IDB)
@ Custom
Custom Block.
@ InterfaceStatistics
Interface Statistics Block (ISB)
@ SimplePacket
Simple Packet Block (SPB)
@ SectionHeader
Section Header Block (SHB)
@ EnhancedPacket
Enhanced Packet Block (EPB)
Common structure for all PcapNG blocks.
Definition: PcapNgFileReaderDevice.h:42
uint32_t blockType
Block type.
Definition: PcapNgFileReaderDevice.h:44
uint32_t blockTotalLength
Total block length (including this header)
Definition: PcapNgFileReaderDevice.h:46
Enhanced Packet Block (EPB)
Definition: PcapNgFileReaderDevice.h:81
uint32_t timestampLow
Timestamp (low part)
Definition: PcapNgFileReaderDevice.h:89
uint32_t capturedLen
Captured packet length.
Definition: PcapNgFileReaderDevice.h:91
PcapNgBlockHeader blockHeader
Block header.
Definition: PcapNgFileReaderDevice.h:83
uint32_t timestampHigh
Timestamp (high part)
Definition: PcapNgFileReaderDevice.h:87
uint32_t interfaceId
Interface ID.
Definition: PcapNgFileReaderDevice.h:85
uint32_t originalLen
Original packet length.
Definition: PcapNgFileReaderDevice.h:93
Interface Description Block (IDB)
Definition: PcapNgFileReaderDevice.h:67
uint16_t reserved
Reserved field.
Definition: PcapNgFileReaderDevice.h:73
uint32_t snapLen
Snap length.
Definition: PcapNgFileReaderDevice.h:75
uint16_t linkType
Link type.
Definition: PcapNgFileReaderDevice.h:71
PcapNgBlockHeader blockHeader
Block header.
Definition: PcapNgFileReaderDevice.h:69
Block option header.
Definition: PcapNgFileReaderDevice.h:109
uint16_t optionLength
Option length.
Definition: PcapNgFileReaderDevice.h:113
uint16_t optionCode
Option code.
Definition: PcapNgFileReaderDevice.h:111
Section Header Block (SHB)
Definition: PcapNgFileReaderDevice.h:51
int64_t sectionLength
Length of section (0xFFFFFFFFFFFFFFFF means unspecified)
Definition: PcapNgFileReaderDevice.h:61
PcapNgBlockHeader blockHeader
Block header.
Definition: PcapNgFileReaderDevice.h:53
uint16_t majorVersion
Major version number.
Definition: PcapNgFileReaderDevice.h:57
uint32_t byteOrderMagic
Byte order magic: 0x1A2B3C4D.
Definition: PcapNgFileReaderDevice.h:55
uint16_t minorVersion
Minor version number.
Definition: PcapNgFileReaderDevice.h:59
Simple Packet Block (SPB)
Definition: PcapNgFileReaderDevice.h:99
uint32_t originalLen
Original packet length.
Definition: PcapNgFileReaderDevice.h:103
PcapNgBlockHeader blockHeader
Block header.
Definition: PcapNgFileReaderDevice.h:101