Slick Sim 0.10.1
Exchange simulator that shadows real venues for risk-free strategy testing
Loading...
Searching...
No Matches
pcap_reader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <zlib.h>
4#include <zstd.h>
5
6#ifdef _WIN32
7#include <share.h>
8#endif
9
10#include <algorithm>
11#include <bit>
12#include <cerrno>
13#include <charconv>
14#include <cstdint>
15#include <cstdio>
16#include <cstring>
17#include <format>
18#include <new>
19#include <stdexcept>
20#include <string>
21#include <string_view>
22#include <system_error>
23#include <vector>
24
25namespace slick::sim::md_feed {
26
29struct PcapPacket {
30 uint64_t ts_ns = 0;
31 uint32_t dst_addr = 0;
32 uint16_t dst_port = 0;
33 const uint8_t *data = nullptr;
34 size_t size = 0;
35};
36
39[[nodiscard]] inline uint32_t parse_ipv4(const std::string &text) {
40 uint32_t address = 0;
41 const char *p = text.data();
42 const char *end = p + text.size();
43 for (int i = 0; i < 4; ++i) {
44 unsigned octet = 0;
45 const auto [next, ec] = std::from_chars(p, end, octet);
46 if (ec != std::errc() || octet > 255 || (i < 3 ? next == end || *next != '.' : next != end)) {
47 throw std::runtime_error(std::format("'{}' is not an IPv4 address", text));
48 }
49 address = (address << 8) | octet;
50 p = next + 1;
51 }
52 return address;
53}
54
55namespace detail {
56
58inline bool has_suffix(std::string_view path, std::string_view suffix) noexcept {
59 if (path.size() < suffix.size()) {
60 return false;
61 }
62 path.remove_prefix(path.size() - suffix.size());
63 for (size_t i = 0; i < suffix.size(); ++i) {
64 const char c = path[i];
65 if ((c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c) != suffix[i]) {
66 return false;
67 }
68 }
69 return true;
70}
71
73inline std::string errno_text(int error) { return std::generic_category().message(error); }
74
76inline std::FILE *open_shared(const std::string &path) {
77#ifdef _WIN32
78 return ::_fsopen(path.c_str(), "rb", _SH_DENYNO);
79#else
80 return std::fopen(path.c_str(), "rb");
81#endif
82}
83
84} // namespace detail
85
86namespace detail {
87
91public:
93 explicit CaptureStream(const std::string &path) {
94 file_ = open_shared(path);
95 if (file_ == nullptr) {
96 throw std::runtime_error(std::format("{}: {}", path, errno_text(errno)));
97 }
98 if (has_suffix(path, ".zst")) {
99 codec_ = Codec::ZSTD;
100 zstd_ = ZSTD_createDCtx();
101 } else if (has_suffix(path, ".gz")) {
102 codec_ = Codec::GZIP;
103 gzip_ready_ = inflateInit2(&gzip_, 15 + 16) == Z_OK; // 16: a gzip wrapper, not zlib's
104 }
105 if ((codec_ == Codec::ZSTD && zstd_ == nullptr) || (codec_ == Codec::GZIP && !gzip_ready_)) {
106 std::fclose(file_);
107 throw std::bad_alloc();
108 }
109 if (codec_ != Codec::NONE) {
110 in_.resize(IN_BYTES);
111 }
112 }
113
115 if (zstd_ != nullptr) {
116 ZSTD_freeDCtx(zstd_);
117 }
118 if (gzip_ready_) {
119 inflateEnd(&gzip_);
120 }
121 std::fclose(file_);
122 }
123
124 CaptureStream(const CaptureStream &) = delete;
126
129 size_t read(uint8_t *dst, size_t size) {
130 if (!error_.empty()) {
131 return 0;
132 }
133 switch (codec_) {
134 case Codec::ZSTD: return read_zstd(dst, size);
135 case Codec::GZIP: return read_gzip(dst, size);
136 case Codec::NONE: break;
137 }
138 return read_file(dst, size);
139 }
140
142 [[nodiscard]] const std::string &error() const noexcept { return error_; }
143
144private:
145 enum class Codec : uint8_t { NONE, ZSTD, GZIP };
146 static constexpr size_t IN_BYTES = 1u << 18;
147
148 size_t read_file(uint8_t *dst, size_t size) {
149 const size_t got = std::fread(dst, 1, size, file_);
150 if (got < size && std::ferror(file_)) {
151 error_ = std::format("read failed: {}", errno_text(errno));
152 }
153 return got;
154 }
155
157 bool refill() {
158 if (in_pos_ < in_len_) {
159 return true;
160 }
161 in_pos_ = 0;
162 in_len_ = read_file(in_.data(), in_.size());
163 return in_len_ > 0;
164 }
165
166 size_t read_zstd(uint8_t *dst, size_t size) {
167 ZSTD_outBuffer out{dst, size, 0};
168 while (out.pos < out.size) {
169 const bool input = refill();
170 if (!error_.empty() || (!input && !open_)) {
171 break; // the end, and every frame was whole
172 }
173 // Called without input too: a full output may have left decoded bytes to flush.
174 ZSTD_inBuffer in{in_.data(), in_len_, in_pos_};
175 const size_t out_before = out.pos;
176 const size_t in_before = in_pos_;
177 const size_t hint = ZSTD_decompressStream(zstd_, &out, &in);
178 in_pos_ = in.pos;
179 if (ZSTD_isError(hint)) {
180 error_ = std::format("zstd: {}; the file is corrupt", ZSTD_getErrorName(hint));
181 break;
182 }
183 open_ = hint != 0;
184 if (out.pos == out_before && in_pos_ == in_before) {
185 // Without input, a frame cut short. With it, a decoder that stopped moving,
186 // which would otherwise spin here.
187 error_ = input ? "zstd stopped making progress; the file is corrupt"
188 : "the zstd stream ends inside a frame; the file is truncated";
189 break;
190 }
191 }
192 return out.pos;
193 }
194
195 size_t read_gzip(uint8_t *dst, size_t size) {
196 gzip_.next_out = dst;
197 gzip_.avail_out = static_cast<uInt>(size);
198 while (gzip_.avail_out > 0) {
199 const bool input = refill();
200 if (!error_.empty() || (!input && !open_)) {
201 break; // the end, and every member was whole
202 }
203 if (input && !open_) {
204 // Another member follows, as `cat a.gz b.gz` makes.
205 if (members_++ > 0) {
206 inflateReset(&gzip_);
207 }
208 open_ = true;
209 }
210 gzip_.next_in = in_.data() + in_pos_;
211 gzip_.avail_in = static_cast<uInt>(in_len_ - in_pos_);
212 const uInt out_before = gzip_.avail_out;
213 const uInt in_before = gzip_.avail_in;
214 const int rc = inflate(&gzip_, Z_NO_FLUSH);
215 in_pos_ = in_len_ - gzip_.avail_in;
216 if (rc == Z_STREAM_END) {
217 open_ = false;
218 } else if (rc != Z_OK && rc != Z_BUF_ERROR) {
219 error_ = std::format("gzip: {}; the file is corrupt", gzip_.msg != nullptr ? gzip_.msg : zError(rc));
220 break;
221 } else if (gzip_.avail_out == out_before && gzip_.avail_in == in_before) {
222 // As for zstd: cut short without input, stalled with it.
223 error_ = input ? "gzip stopped making progress; the file is corrupt"
224 : "the gzip stream ends inside a member; the file is truncated";
225 break;
226 }
227 }
228 return size - gzip_.avail_out;
229 }
230
231 std::FILE *file_ = nullptr;
232 Codec codec_ = Codec::NONE;
233 ZSTD_DCtx *zstd_ = nullptr;
234 z_stream gzip_{};
235 bool gzip_ready_ = false;
236 uint64_t members_ = 0;
237 bool open_ = false;
238 std::vector<uint8_t> in_;
239 size_t in_pos_ = 0;
240 size_t in_len_ = 0;
241 std::string error_;
242};
243
244} // namespace detail
245
259public:
261 explicit PcapReader(const std::string &path) : path_(path), stream_(path) {
262 buffer_.resize(BUFFER_BYTES);
263 if (!fill(4)) {
264 fail(stream_.error().empty() ? "not a pcap or pcapng capture" : stream_.error());
265 }
266 uint32_t magic = 0;
267 std::memcpy(&magic, data(), sizeof(magic));
268 if (magic == PCAPNG_SECTION) {
269 pcapng_ = true;
270 // Read up to the first interface, so a link type that can't be read fails here.
271 Block block;
272 while (interfaces_.empty()) {
273 if (!next_block(block)) {
274 fail(error_.empty() ? "a pcapng capture with no interface" : error_);
275 }
276 if (block.type == PCAPNG_ENHANCED || block.type == PCAPNG_SIMPLE || block.type == PCAPNG_OBSOLETE) {
277 fail("a pcapng packet comes before any interface is described");
278 }
279 }
280 return;
281 }
282 open_pcap(magic);
283 }
284
285 PcapReader(const PcapReader &) = delete;
286 PcapReader &operator=(const PcapReader &) = delete;
287
290 bool next(PcapPacket &out) {
291 if (!error_.empty()) {
292 return false;
293 }
294 return pcapng_ ? next_pcapng(out) : next_pcap(out);
295 }
296
299 [[nodiscard]] const std::string &error() const { return error_; }
300 [[nodiscard]] const std::string &path() const { return path_; }
303 [[nodiscard]] uint64_t truncated() const noexcept { return truncated_; }
304
305private:
306 static constexpr size_t BUFFER_BYTES = 1u << 20;
309 static constexpr uint32_t MAX_RECORD_BYTES = 16u << 20;
310
311 static constexpr uint32_t PCAP_MICRO = 0xa1b2c3d4;
312 static constexpr uint32_t PCAP_NANO = 0xa1b23c4d;
313 static constexpr uint32_t PCAPNG_SECTION = 0x0a0d0d0a; // the same in either byte order
314 static constexpr uint32_t PCAPNG_BYTE_ORDER = 0x1a2b3c4d;
315 static constexpr uint32_t PCAPNG_INTERFACE = 1;
316 static constexpr uint32_t PCAPNG_OBSOLETE = 2; // the old Packet Block
317 static constexpr uint32_t PCAPNG_SIMPLE = 3;
318 static constexpr uint32_t PCAPNG_ENHANCED = 6;
319
320 static constexpr uint16_t LINK_ETHERNET = 1;
321
324 static constexpr bool readable(uint32_t link_type) noexcept {
325 return link_type == LINK_ETHERNET || link_type == 101 || link_type == 12 || link_type == 14 ||
326 link_type == 228;
327 }
328
330 struct Interface {
331 uint16_t link_type = 0;
332 uint32_t snaplen = 0;
333 bool binary = false;
334 uint8_t exponent = 6;
335 int64_t offset_s = 0;
336
337 [[nodiscard]] uint64_t to_ns(uint64_t ts) const noexcept {
338 uint64_t ns = 0;
339 if (binary) {
340 const uint8_t e = exponent;
341 const uint64_t seconds = ts >> e;
342 const uint64_t fraction = ts & ((uint64_t(1) << e) - 1);
343 uint64_t scaled = (fraction & 0xffffffff) * 1'000'000'000ull;
344 if (e > 32) {
345 // fraction * 1e9 can pass 64 bits, so by halves: every bit counts until the last shift.
346 scaled = ((fraction >> 32) * 1'000'000'000ull + (scaled >> 32)) >> (e - 32);
347 } else {
348 scaled >>= e;
349 }
350 ns = seconds * 1'000'000'000ull + scaled;
351 } else if (exponent <= 9) {
352 ns = ts * pow10(9 - exponent);
353 } else {
354 ns = ts / pow10(exponent - 9);
355 }
356 if (offset_s < 0) {
357 // Negated in unsigned arithmetic, so even INT64_MIN has a magnitude.
358 const uint64_t back = (uint64_t(0) - static_cast<uint64_t>(offset_s)) * 1'000'000'000ull;
359 return ns > back ? ns - back : 0; // before the epoch: 0, which no replay window reaches
360 }
361 return ns + static_cast<uint64_t>(offset_s) * 1'000'000'000ull;
362 }
363
364 static constexpr uint64_t pow10(unsigned n) noexcept {
365 uint64_t v = 1;
366 while (n-- > 0) {
367 v *= 10;
368 }
369 return v;
370 }
371 };
372
374 struct Block {
375 uint32_t type = 0;
376 const uint8_t *body = nullptr;
377 size_t size = 0;
378 };
379
380 [[noreturn]] void fail(const std::string &why) const {
381 throw std::runtime_error(std::format("{}: {}", path_, why));
382 }
383
384 void open_pcap(uint32_t magic) {
385 if (magic == std::byteswap(PCAP_MICRO) || magic == std::byteswap(PCAP_NANO)) {
386 swapped_ = true;
387 magic = std::byteswap(magic);
388 }
389 if (magic != PCAP_MICRO && magic != PCAP_NANO) {
390 fail(stream_.error().empty() ? "not a pcap or pcapng capture" : stream_.error());
391 }
392 nano_ = magic == PCAP_NANO;
393 if (!fill(24)) {
394 fail(stream_.error().empty() ? "the pcap header is cut short" : stream_.error());
395 }
396 if (const uint16_t major = u16(data() + 4); major != 2) {
397 fail(std::format("pcap version {} is not read", major));
398 }
399 link_type_ = u32(data() + 20) & 0xffff; // the upper bits can carry FCS details
400 if (!readable(link_type_)) {
401 fail(std::format("unsupported link type {}", link_type_));
402 }
403 consume(24);
404 }
405
406 bool next_pcap(PcapPacket &out) {
407 for (;;) {
408 if (!fill(16)) {
409 return end_of_stream();
410 }
411 const uint32_t caplen = u32(data() + 8);
412 if (caplen > MAX_RECORD_BYTES) {
413 error_ = std::format("a record claims {} captured bytes; the file is corrupt", caplen);
414 return false;
415 }
416 if (!fill(16 + size_t(caplen))) {
417 return end_of_stream();
418 }
419 const uint8_t *record = data();
420 consume(16 + size_t(caplen));
421 const uint64_t fraction = u32(record + 4);
422 const uint64_t ts_ns = uint64_t(u32(record)) * 1'000'000'000ull + (nano_ ? fraction : fraction * 1000);
423 if (parse(link_type_, record + 16, caplen, ts_ns, out)) {
424 return true;
425 }
426 }
427 }
428
429 bool next_pcapng(PcapPacket &out) {
430 Block block;
431 while (next_block(block)) {
432 uint32_t if_id = 0; // not `interface`, which windows.h defines as a macro
433 uint64_t ts = 0;
434 size_t caplen = 0;
435 const uint8_t *frame = nullptr;
436 if (block.type == PCAPNG_ENHANCED || block.type == PCAPNG_OBSOLETE) {
437 if (block.size < 20) {
438 return malformed("packet");
439 }
440 if_id = block.type == PCAPNG_ENHANCED ? u32(block.body) : u16(block.body);
441 ts = (uint64_t(u32(block.body + 4)) << 32) | u32(block.body + 8);
442 caplen = u32(block.body + 12);
443 frame = block.body + 20;
444 if (caplen > block.size - 20) {
445 return malformed("packet");
446 }
447 } else if (block.type == PCAPNG_SIMPLE) {
448 if (block.size < 4) {
449 return malformed("simple packet");
450 }
451 // No captured length: the original one, cut to the block (and the snaplen below).
452 caplen = std::min<size_t>(u32(block.body), block.size - 4);
453 frame = block.body + 4;
454 } else {
455 continue; // statistics, name resolution, custom blocks
456 }
457 if (if_id >= interfaces_.size()) {
458 error_ = std::format("a packet names interface {}, which is not described", if_id);
459 return false;
460 }
461 const Interface &described = interfaces_[if_id];
462 if (block.type == PCAPNG_SIMPLE && described.snaplen != 0) {
463 caplen = std::min<size_t>(caplen, described.snaplen);
464 }
465 // A simple packet has no timestamp, so libpcap's 0 stands, which no replay window reaches.
466 const uint64_t ts_ns = block.type == PCAPNG_SIMPLE ? 0 : described.to_ns(ts);
467 if (parse(described.link_type, frame, caplen, ts_ns, out)) {
468 return true;
469 }
470 }
471 return false;
472 }
473
476 bool next_block(Block &block) {
477 if (!fill(12)) {
478 return end_of_stream();
479 }
480 uint32_t type = 0;
481 std::memcpy(&type, data(), sizeof(type));
482 if (type == PCAPNG_SECTION) {
483 uint32_t order = 0;
484 std::memcpy(&order, data() + 8, sizeof(order));
485 if (order != PCAPNG_BYTE_ORDER && order != std::byteswap(PCAPNG_BYTE_ORDER)) {
486 error_ = "a pcapng section without its byte-order magic; the file is corrupt";
487 return false;
488 }
489 swapped_ = order != PCAPNG_BYTE_ORDER;
490 interfaces_.clear(); // a new section describes its own
491 }
492 type = u32(data());
493 const uint32_t length = u32(data() + 4);
494 if (length < 12 || length % 4 != 0 || length > MAX_RECORD_BYTES) {
495 error_ = std::format("a pcapng block claims {} bytes; the file is corrupt", length);
496 return false;
497 }
498 if (!fill(length)) {
499 return end_of_stream();
500 }
501 // The length is at both ends; a block that disagrees with itself is where the damage is.
502 if (const uint32_t trailing = u32(data() + length - 4); trailing != length) {
503 error_ = std::format("a pcapng block of type {} says {} bytes at its start, {} at its end; the file is corrupt",
504 type, length, trailing);
505 return false;
506 }
507 block = {type, data() + 8, length - 12};
508 consume(length);
509 if (type == PCAPNG_SECTION) {
510 if (block.size < 16) {
511 return malformed("section header");
512 }
513 if (const uint16_t major = u16(block.body + 4); major != 1) {
514 error_ = std::format("pcapng version {} is not read", major);
515 return false;
516 }
517 } else if (type == PCAPNG_INTERFACE) {
518 return describe_interface(block);
519 }
520 return true;
521 }
522
523 bool describe_interface(const Block &block) {
524 if (block.size < 8) {
525 return malformed("interface description");
526 }
527 Interface described;
528 described.link_type = u16(block.body);
529 described.snaplen = u32(block.body + 4);
530 const uint8_t *option = block.body + 8;
531 const uint8_t *end = block.body + block.size;
532 while (end - option >= 4) {
533 const uint16_t code = u16(option);
534 const uint16_t length = u16(option + 2);
535 option += 4;
536 if (code == 0 || size_t(end - option) < length) { // opt_endofopt, or cut short
537 break;
538 }
539 if (code == 9 && length >= 1) { // if_tsresol
540 described.binary = (option[0] & 0x80) != 0;
541 described.exponent = static_cast<uint8_t>(option[0] & 0x7f);
542 } else if (code == 14 && length >= 8) { // if_tsoffset
543 described.offset_s = static_cast<int64_t>(u64(option));
544 }
545 option += std::min<size_t>((size_t(length) + 3) & ~size_t(3), size_t(end - option));
546 }
547 const size_t index = interfaces_.size();
548 interfaces_.push_back(described);
549 if (!readable(described.link_type)) {
550 error_ = std::format("unsupported link type {} on interface {}", described.link_type, index);
551 return false;
552 }
553 if (described.exponent > (described.binary ? 63 : 19)) {
554 error_ = std::format("interface {} has a timestamp resolution this reader can't use", index);
555 return false;
556 }
557 return true;
558 }
559
560 bool malformed(const char *what) {
561 error_ = std::format("a pcapng {} block is malformed; the file is corrupt", what);
562 return false;
563 }
564
567 bool end_of_stream() {
568 if (error_.empty()) {
569 if (!stream_.error().empty()) {
570 error_ = stream_.error();
571 } else if (end_ > begin_) {
572 error_ = std::format("the capture ends {} bytes into a record; the file is truncated", end_ - begin_);
573 }
574 }
575 return false;
576 }
577
578 bool cut_short() noexcept {
579 ++truncated_;
580 return false;
581 }
582
583 bool parse(uint32_t link_type, const uint8_t *frame, size_t caplen, uint64_t ts_ns, PcapPacket &out) {
584 const uint8_t *p = frame;
585 size_t left = caplen;
586 if (link_type == LINK_ETHERNET) {
587 if (left < 14) {
588 return cut_short();
589 }
590 uint16_t type = static_cast<uint16_t>((p[12] << 8) | p[13]);
591 p += 14;
592 left -= 14;
593 while ((type == 0x8100 || type == 0x88a8) && left >= 4) {
594 type = static_cast<uint16_t>((p[2] << 8) | p[3]);
595 p += 4;
596 left -= 4;
597 }
598 if (type == 0x8100 || type == 0x88a8) {
599 return cut_short(); // a VLAN tag the capture cut off
600 }
601 if (type != 0x0800) {
602 return false;
603 }
604 }
605 if (left < 20) {
606 return cut_short();
607 }
608 if ((p[0] >> 4) != 4) {
609 return false;
610 }
611 const size_t ihl = static_cast<size_t>(p[0] & 0x0f) * 4;
612 const size_t total = static_cast<size_t>((p[2] << 8) | p[3]);
613 const uint16_t frag = static_cast<uint16_t>(((p[6] << 8) | p[7]) & 0x3fff); // MF flag + offset
614 // Only whole datagrams: a fragment has no UDP header to read, or no end.
615 if (p[9] != 17 || frag != 0) {
616 return false;
617 }
618 if (ihl < 20 || left < ihl + 8 || total < ihl + 8) {
619 return cut_short();
620 }
621 const uint8_t *udp = p + ihl;
622 const size_t udp_len = static_cast<size_t>((udp[4] << 8) | udp[5]);
623 if (udp_len < 8) {
624 return cut_short();
625 }
626 // A capture can cut a frame short; trust no more than was recorded.
627 size_t payload = udp_len - 8;
628 if (payload > left - ihl - 8) {
629 return cut_short();
630 }
631 out.ts_ns = ts_ns;
632 out.dst_addr = (uint32_t(p[16]) << 24) | (uint32_t(p[17]) << 16) | (uint32_t(p[18]) << 8) | p[19];
633 out.dst_port = static_cast<uint16_t>((udp[2] << 8) | udp[3]);
634 out.data = udp + 8;
635 out.size = payload;
636 return true;
637 }
638
641 bool fill(size_t n) {
642 if (end_ - begin_ >= n) {
643 return true;
644 }
645 if (begin_ > 0) {
646 std::memmove(buffer_.data(), buffer_.data() + begin_, end_ - begin_);
647 end_ -= begin_;
648 begin_ = 0;
649 }
650 if (n > buffer_.size()) {
651 buffer_.resize(std::bit_ceil(n)); // a record larger than the buffer
652 }
653 while (end_ < n) {
654 const size_t got = stream_.read(buffer_.data() + end_, buffer_.size() - end_);
655 if (got == 0) {
656 return false;
657 }
658 end_ += got;
659 }
660 return true;
661 }
662
663 [[nodiscard]] const uint8_t *data() const noexcept { return buffer_.data() + begin_; }
664 void consume(size_t n) noexcept { begin_ += n; }
665
666 template <typename T>
667 [[nodiscard]] T load(const uint8_t *p) const noexcept {
668 T v;
669 std::memcpy(&v, p, sizeof(v));
670 return swapped_ ? std::byteswap(v) : v;
671 }
672 [[nodiscard]] uint16_t u16(const uint8_t *p) const noexcept { return load<uint16_t>(p); }
673 [[nodiscard]] uint32_t u32(const uint8_t *p) const noexcept { return load<uint32_t>(p); }
674 [[nodiscard]] uint64_t u64(const uint8_t *p) const noexcept { return load<uint64_t>(p); }
675
676 std::string path_;
677 detail::CaptureStream stream_;
678 std::vector<uint8_t> buffer_;
679 size_t begin_ = 0;
680 size_t end_ = 0;
681 bool pcapng_ = false;
682 bool swapped_ = false;
683 bool nano_ = false;
684 uint32_t link_type_ = 0;
685 std::vector<Interface> interfaces_;
686 std::string error_;
687 uint64_t truncated_ = 0;
688};
689
690} // namespace slick::sim::md_feed
PcapReader & operator=(const PcapReader &)=delete
const std::string & error() const
Empty after a clean end of file; otherwise what stopped the read, for example a capture truncated mid...
const std::string & path() const
PcapReader(const PcapReader &)=delete
bool next(PcapPacket &out)
Reads the next UDP datagram.
uint64_t truncated() const noexcept
Frames skipped so far for being cut short or malformed - not those that are simply not UDP (IGMP,...
PcapReader(const std::string &path)
Opens path. Throws std::runtime_error when it cannot be opened or read as a capture.
const std::string & error() const noexcept
Empty unless reading or decompressing failed: a truncated or corrupt archive.
size_t read(uint8_t *dst, size_t size)
Fills dst with up to size bytes, short only at the end of the stream or on an error,...
CaptureStream(const std::string &path)
Throws std::runtime_error when the file cannot be opened.
CaptureStream(const CaptureStream &)=delete
CaptureStream & operator=(const CaptureStream &)=delete
std::FILE * open_shared(const std::string &path)
Opens for reading, shared: two readers may hold one capture (fopen_s would lock it).
bool has_suffix(std::string_view path, std::string_view suffix) noexcept
path ends in the lowercase suffix, in any case: a.PCAP.ZST is zstd too.
std::string errno_text(int error)
errno's text, without strerror's thread-safety warning on MSVC.
uint32_t parse_ipv4(const std::string &text)
Packs a dotted quad such as "224.0.31.1" into host byte order, the form PcapPacket::dst_addr uses.
One UDP datagram read from a capture.
uint32_t dst_addr
IPv4 destination, host byte order.
uint64_t ts_ns
Capture time, nanoseconds since the epoch.
const uint8_t * data
UDP payload.