Skip to content

Commit 94247bd

Browse files
author
Damir Zainullin
committed
++
1 parent 6d33067 commit 94247bd

File tree

3 files changed

+71
-40
lines changed

3 files changed

+71
-40
lines changed

process-plugin-api/process/common/dnsParser/dnsParser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <arpa/inet.h>
1717

1818
#include "dnsSection.hpp"
19+
#include "dnsSectionReader.hpp"
1920

2021
namespace ipxp {
2122

@@ -35,7 +36,7 @@ std::optional<std::size_t> parseDNSOverTCPLength(std::span<const std::byte> payl
3536
return dnsDataLength;
3637
}
3738

38-
constexpr static
39+
static
3940
std::optional<std::size_t>
4041
parseSection(
4142
std::span<const std::byte> payload,
Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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>
@@ -11,25 +11,33 @@
1111
namespace 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

process-plugin-api/process/common/readers/rangeReader/rangeReader.hpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,60 @@
33
namespace ipxp
44
{
55

6-
template<typename parsingCallbackFactory>
6+
template<typename Derived>
77
class RangeReader {
88
enum class ParsingState {
99
SUCCESS,
1010
FAILURE
1111
};
1212

1313
public:
14-
RangeReader(std::span<const std::byte> data,
15-
parsingCallbackFactory& callbackFactory) noexcept
16-
: m_callback(callbackFactory(data))
14+
RangeReader() noexcept
15+
//: m_callback(callback->init())
1716
{
1817
}
1918

20-
constexpr auto begin() const noexcept {
21-
return m_reader.begin();
19+
auto getCallback() const noexcept
20+
{
21+
return static_cast<const Derived*>(this)->m_callback;
2222
}
2323

24-
constexpr auto end() const noexcept {
25-
return m_reader.end();
24+
constexpr auto begin() const noexcept
25+
{
26+
return getCallback().begin();
2627
}
2728

28-
constexpr auto begin() noexcept {
29-
return m_reader.begin();
29+
constexpr auto end() const noexcept
30+
{
31+
return getCallback().end();
3032
}
3133

32-
constexpr auto end() noexcept {
33-
return m_reader.end();
34+
/*constexpr auto begin() noexcept
35+
{
36+
return m_callback.begin();
3437
}
3538
36-
constexpr bool parsedSuccessfully() const noexcept {
39+
constexpr auto end() noexcept
40+
{
41+
return m_callback.end();
42+
}*/
43+
44+
constexpr bool parsedSuccessfully() const noexcept
45+
{
3746
return m_state == ParsingState::SUCCESS;
3847
}
3948

4049
protected:
50+
4151

4252
constexpr void setSuccess() noexcept {
4353
m_state = ParsingState::SUCCESS;
4454
}
4555

56+
//decltype(std::declval<Derived>().init()) m_callback;
57+
4658
private:
4759
ParsingState m_state{ParsingState::FAILURE};
48-
decltype(makeReader(std::span<const std::byte>{})) m_reader;
4960
};
5061

5162

0 commit comments

Comments
 (0)