11#pragma once
22
33#include < span>
4- #include < views >
4+ #include < ranges >
55#include < optional>
66#include < readers/rangeReader/rangeReader.hpp>
77#include < readers/rangeReader/generator.hpp>
1111namespace ipxp
1212{
1313
14- class DNSSectionReader ;
14+ /*
15+ struct DNSSectionReaderFactory;
1516
16- struct DNSSectionReaderFactory {
17- DNSSectionReader* self;
18- std::size_t itemCount;
19- std::span<const std::byte> fullDNSPayload;
17+ class DNSSectionReader : public RangeReader<DNSSectionReaderFactory> {
18+ public:
19+ DNSSectionReader(std::span<const std::byte> section,
20+ std::span<const std::byte> fullDNSPayload, const std::size_t itemCount)
21+ : RangeReader(section, DNSSectionReaderFactory{this, itemCount, fullDNSPayload.data()}) {}
22+ };*/
23+
24+ class DNSSectionReader : public RangeReader <DNSSectionReader> {
25+ public:
26+
2027
21- auto operator ()(std::span<const std::byte> section) const {
22- return Generator::generate ([section, self = self, itemCount, fullDNSPayload]() mutable
23- -> std::optional<DNSRecord>& {
28+ auto init () noexcept
29+ {
30+ return Generator::generate ([this ]() mutable
31+ -> const std::optional<DNSRecord>& {
2432 static auto res = std::make_optional<DNSRecord>();
25- if (itemCount == 0 ) {
26- self-> setSuccess ();
33+ if (m_itemCount == 0 ) {
34+ setSuccess ();
2735 return std::nullopt ;
2836 }
29- itemCount --;
37+ m_itemCount --;
3038
3139 std::optional<DNSName> name = DNSName::createFrom (
32- section, fullDNSPayload );
40+ section, m_fullDNSPayload );
3341 if (!name.has_value ()) {
3442 return std::nullopt ;
3543 }
@@ -38,25 +46,27 @@ struct DNSSectionReaderFactory {
3846 }
3947 res->name = std::move (*name);
4048
41- res->type = ntohs (*reinterpret_cast <const uint16_t *>(
42- section.data () + name->length ()));
49+ res->type = static_cast <DNSQueryType>(
50+ ntohs (*reinterpret_cast <const uint16_t *>(
51+ section.data () + name->length ())));
4352
4453 res->recordClass = ntohs (
45- *reinterpret_cast <const uint16_t *>(section.data () + name->length () + sizeof (type)));
54+ *reinterpret_cast <const uint16_t *>(
55+ section.data () + name->length () + sizeof (res->type )));
4656
4757 res->timeToLive = ntohl (*reinterpret_cast <const uint32_t *>(
4858 section.data () + name->length () + 2 * sizeof (uint16_t )));
4959
50- res-> rawDataLength = ntohs (*reinterpret_cast <const uint16_t *>(
51- section.data () + name->length () + 2 * sizeof (uint16_t ) + sizeof (ttl )));
60+ const uint16_t rawDataLength = ntohs (*reinterpret_cast <const uint16_t *>(
61+ section.data () + name->length () + 2 * sizeof (uint16_t ) + sizeof (uint32_t )));
5262 if (name->length () + 3 * sizeof (uint16_t ) + sizeof (uint32_t ) + rawDataLength
5363 > section.size ()) {
5464 return std::nullopt ;
5565 }
5666
5767 std::span<const std::byte> rawData = section.subspan (
5868 name->length () + 3 * sizeof (uint16_t ) + sizeof (uint32_t ), rawDataLength);
59- res->dnsPayload (rawData, fullDNSPayload, type);
69+ res->payload = DNSRecordPayload (rawData, m_fullDNSPayload, res-> type );
6070
6171 section = section.subspan (
6272 name->length () + 3 * sizeof (uint16_t ) + sizeof (uint32_t ) + rawDataLength);
@@ -68,13 +78,22 @@ struct DNSSectionReaderFactory {
6878 return *v;
6979 });
7080 }
71- };
7281
73- class DNSSectionReader : public RangeReader <DNSSectionReaderFactory> {
74- public:
75- DNSSectionReader (std::span<const std::byte> section,
76- std::span<const std::byte> fullDNSPayload, const std::size_t itemCount)
77- : RangeReader(section, DNSSectionReaderFactory{this , itemCount, fullDNSPayload.data ()}) {}
82+ DNSSectionReader (
83+ const std::size_t itemCount,
84+ std::span<const std::byte> fullDNSPayload,
85+ std::span<const std::byte> section)
86+ : m_itemCount(itemCount)
87+ , m_fullDNSPayload(fullDNSPayload)
88+ , section(section)
89+ , m_callback(init()) {}
90+ private:
91+ std::size_t m_itemCount{0 };
92+ std::span<const std::byte> m_fullDNSPayload;
93+ std::span<const std::byte> section;
94+ decltype (std::declval<DNSSectionReader>().init()) m_callback;
7895};
7996
97+
98+
8099} // namespace ipxp
0 commit comments