libpromeki 1.0.0-alpha
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
pcapreader.h
Go to the documentation of this file.
1
8#pragma once
9
10
11#include <promeki/config.h>
12#if PROMEKI_ENABLE_NETWORK
13#include <cstdint>
14#include <promeki/namespace.h>
15#include <promeki/buffer.h>
16#include <promeki/bufferview.h>
17#include <promeki/datetime.h>
18#include <promeki/enums_pcap.h>
19#include <promeki/error.h>
20#include <promeki/list.h>
21#include <promeki/result.h>
22#include <promeki/string.h>
23
24PROMEKI_NAMESPACE_BEGIN
25
26class IODevice;
27
46struct PcapRecord {
51 DateTime captureTime;
52
57 PcapLinkType linkType;
58
62 BufferView frame;
63
66 uint32_t originalLength = 0;
67
71 bool snapTruncated = false;
72
74 size_t capturedLength() const { return frame.size(); }
75};
76
123class PcapReader {
124 public:
128 static constexpr size_t MaxRecordLength = 256u * 1024u * 1024u;
129
132 static constexpr uint32_t MagicMicros = 0xa1b2c3d4u;
133
136 static constexpr uint32_t MagicNanos = 0xa1b23c4du;
137
140 static constexpr uint32_t PngBlockShb = 0x0a0d0d0au;
141 static constexpr uint32_t PngByteOrderMagic = 0x1a2b3c4du;
142 static constexpr uint32_t PngBlockIdb = 0x00000001u;
143 static constexpr uint32_t PngBlockSpb = 0x00000003u;
144 static constexpr uint32_t PngBlockEpb = 0x00000006u;
145
147 PcapReader() = default;
148
163 Error open(IODevice &device);
164
172 Error openFile(const String &path);
173
183 Error openBuffer(const Buffer &buf);
184
186 bool isOpen() const { return _format != PcapFileFormat::Unknown; }
187
189 PcapFileFormat format() const { return _format; }
190
192 PcapByteOrder byteOrder() const { return _byteOrder; }
193
203 PcapLinkType linkType() const;
204
211 uint32_t snapLength() const;
212
214 size_t interfaceCount() const { return _interfaces.size(); }
215
225 Result<PcapRecord> next();
226
228 void close();
229
230 private:
233 struct Interface {
234 PcapLinkType linkType = PcapLinkType::Ethernet;
235 uint32_t snapLength = 0;
239 uint8_t tsResolCode = 6;
240 };
241
242 Error parseHeader();
243 Error parseClassicHeader();
244 Error parsePcapngFirstSection();
245 Result<PcapRecord> nextClassic();
246 Result<PcapRecord> nextPcapng();
247 Error consumePcapngIdb(size_t bodyOff, size_t bodyLen);
248
249 Buffer _backing;
250 size_t _pos = 0;
251 size_t _size = 0;
252 PcapFileFormat _format = PcapFileFormat::Unknown;
253 PcapByteOrder _byteOrder = PcapByteOrder::Unknown;
254 bool _be = false;
255 bool _nanoTs = false;
256 uint32_t _snaplen = 0;
257 PcapLinkType _classicLink = PcapLinkType::Ethernet;
258 List<Interface> _interfaces;
259};
260
261PROMEKI_NAMESPACE_END
262
263#endif // PROMEKI_ENABLE_NETWORK