Skip to content

Commit 2e60280

Browse files
Zainullin DamirZainullin Damir
authored andcommitted
++
1 parent 09bdca9 commit 2e60280

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1764
-1459
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1+
/**
2+
* @file
3+
* @brief Provides DNS header structure.
4+
* @author Damir Zainullin <[email protected]>
5+
* @date 2025
6+
*
7+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
8+
*/
9+
110
#pragma once
211

12+
#include "dnsHeaderFlags.hpp"
13+
314
#include <cstdint>
415

5-
#include "dnsHeaderFlags.hpp"
16+
namespace ipxp {
617

7-
namespace ipxp
8-
{
9-
1018
/**
11-
* @brief DNS header structure
19+
* @brief DNS header
1220
*/
1321
struct DNSHeader {
14-
uint16_t id; /**< DNS packet ID */
15-
DNSHeaderFlags flags; /**< DNS packet flags */
16-
uint16_t questionRecordCount; /**< Number of questions in the packet */
17-
uint16_t answerRecordCount; /**< Number of answers in the packet */
18-
uint16_t authorityRecordCount; /**< Number of authority records in the packet */
19-
uint16_t additionalRecordCount; /**< Number of additional records in the packet */
22+
uint16_t id; /**< DNS packet ID */
23+
DNSHeaderFlags flags; /**< DNS packet flags */
24+
uint16_t questionRecordCount; /**< Number of questions in the packet */
25+
uint16_t answerRecordCount; /**< Number of answers in the packet */
26+
uint16_t authorityRecordCount; /**< Number of authority records in the packet */
27+
uint16_t additionalRecordCount; /**< Number of additional records in the packet */
2028
};
2129

22-
2330
} // namespace ipxp

src/plugins/process/common/dnsParser/dnsHeaderFlags.hpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1+
/**
2+
* @file
3+
* @brief Provides DNS header flags structure.
4+
* @author Damir Zainullin <[email protected]>
5+
*
6+
* @date 2025
7+
*
8+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
9+
*/
10+
111
#pragma once
212

313
#include <cstdint>
414

5-
namespace ipxp
6-
{
7-
15+
namespace ipxp {
16+
817
/**
918
* @brief DNS header flags structure
1019
*/
1120
struct DNSHeaderFlags {
1221
#if RTE_BYTE_ORDER == RTE_BIG_ENDIAN
13-
uint16_t queryResponse : 1; /**< Query or response bit*/
14-
uint16_t operationCode : 4; /**< Operation code */
15-
uint16_t authorityAnswer : 1; /**< Authority answer */
16-
uint16_t truncation : 1; /**< Truncation bit*/
17-
uint16_t recursionDesired : 1; /**< Recursion desired */
18-
uint16_t recursionAvailable : 1; /**< Recursion available */
19-
uint16_t reserved : 3; /**< Reserved */
20-
uint16_t responseCode : 4; /**< Response code */
22+
uint16_t queryResponse : 1; /**< Query or response bit*/
23+
uint16_t operationCode : 4; /**< Operation code */
24+
uint16_t authorityAnswer : 1; /**< Authority answer */
25+
uint16_t truncation : 1; /**< Truncation bit*/
26+
uint16_t recursionDesired : 1; /**< Recursion desired */
27+
uint16_t recursionAvailable : 1; /**< Recursion available */
28+
uint16_t reserved : 3; /**< Reserved */
29+
uint16_t responseCode : 4; /**< Response code */
2130
#else
22-
uint16_t responseCode : 4;
23-
uint16_t reserved : 3;
24-
uint16_t recursionAvailable : 1;
25-
uint16_t recursionDesired : 1;
26-
uint16_t truncation : 1;
27-
uint16_t authorityAnswer : 1;
28-
uint16_t operationCode : 4;
29-
uint16_t queryResponse : 1;
31+
uint16_t responseCode : 4;
32+
uint16_t reserved : 3;
33+
uint16_t recursionAvailable : 1;
34+
uint16_t recursionDesired : 1;
35+
uint16_t truncation : 1;
36+
uint16_t authorityAnswer : 1;
37+
uint16_t operationCode : 4;
38+
uint16_t queryResponse : 1;
3039
#endif
3140
} __attribute__((packed));
3241

Lines changed: 72 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
/**
2+
* @file
3+
* @brief Provides DNS name structure and parsing functionality.
4+
* @author Damir Zainullin <[email protected]>
5+
*/
6+
17
#include "dnsName.hpp"
28

3-
#include <numeric>
4-
#include <span>
5-
#include <string>
6-
#include <optional>
79
#include <algorithm>
810
#include <cstddef>
911
#include <cstdint>
12+
#include <numeric>
13+
#include <optional>
14+
#include <span>
15+
#include <string>
16+
1017
#include <arpa/inet.h>
1118
#include <boost/container/static_vector.hpp>
1219
#include <boost/static_string.hpp>
13-
1420
#include <utils/stringViewUtils.hpp>
1521
#include <utils/toHostByteOrder.hpp>
1622

17-
namespace ipxp
18-
{
23+
namespace ipxp {
1924

2025
constexpr static bool isPointer(const std::byte byte) noexcept
2126
{
@@ -31,79 +36,78 @@ constexpr static uint16_t getPointerOffset(const std::byte* pointer) noexcept
3136

3237
constexpr static std::size_t calculateElementsLength(auto&& container) noexcept
3338
{
34-
return std::accumulate(
35-
container.begin(), container.end(), std::size_t{0},
36-
[](std::size_t sum, auto&& element) {
37-
return sum + element.size();
38-
});
39+
return std::accumulate(
40+
container.begin(),
41+
container.end(),
42+
std::size_t {0},
43+
[](std::size_t sum, auto&& element) { return sum + element.size(); });
3944
}
4045

4146
std::optional<DNSName> DNSName::createFrom(
42-
std::span<const std::byte> payload,
43-
std::span<const std::byte> fullDNSpayload) noexcept
47+
std::span<const std::byte> payload,
48+
std::span<const std::byte> fullDNSpayload) noexcept
4449
{
45-
auto dnsName = std::make_optional<DNSName>();
46-
while (!payload.empty()) {
47-
if (isPointer(*payload.data())) {
48-
if (payload.size() < sizeof(uint16_t)) {
49-
return std::nullopt;
50-
}
51-
const uint16_t pointerOffset = getPointerOffset(payload.data());
52-
if (pointerOffset >= fullDNSpayload.size()) {
53-
return std::nullopt;
54-
}
55-
56-
const std::size_t lengthBytesCount =
57-
dnsName->m_labels.empty() ? 0 : dnsName->m_labels.size();
58-
dnsName->m_length = calculateElementsLength(
59-
dnsName->m_labels) + lengthBytesCount + sizeof(pointerOffset);
60-
61-
payload = fullDNSpayload.subspan(pointerOffset);
62-
continue;
63-
}
64-
65-
const auto labelLength = static_cast<uint8_t>(*payload.data());
66-
if (labelLength + sizeof(uint8_t) > payload.size()) {
67-
return std::nullopt;
68-
}
69-
if (labelLength == 0) {
70-
return dnsName;
71-
}
72-
73-
if (dnsName->m_labels.size() == dnsName->m_labels.capacity()) {
74-
return std::nullopt;
75-
}
76-
77-
dnsName->m_labels.push_back(toStringView(
78-
payload.subspan(sizeof(uint8_t), labelLength)));
79-
80-
payload = payload.subspan(labelLength + sizeof(uint8_t));
81-
}
82-
83-
return std::nullopt;
50+
auto dnsName = std::make_optional<DNSName>();
51+
while (!payload.empty()) {
52+
if (isPointer(*payload.data())) {
53+
if (payload.size() < sizeof(uint16_t)) {
54+
return std::nullopt;
55+
}
56+
const uint16_t pointerOffset = getPointerOffset(payload.data());
57+
if (pointerOffset >= fullDNSpayload.size()) {
58+
return std::nullopt;
59+
}
60+
61+
const std::size_t lengthBytesCount
62+
= dnsName->m_labels.empty() ? 0 : dnsName->m_labels.size();
63+
dnsName->m_length = calculateElementsLength(dnsName->m_labels) + lengthBytesCount
64+
+ sizeof(pointerOffset);
65+
66+
payload = fullDNSpayload.subspan(pointerOffset);
67+
continue;
68+
}
69+
70+
const auto labelLength = static_cast<uint8_t>(*payload.data());
71+
if (labelLength + sizeof(uint8_t) > payload.size()) {
72+
return std::nullopt;
73+
}
74+
if (labelLength == 0) {
75+
return dnsName;
76+
}
77+
78+
if (dnsName->m_labels.size() == dnsName->m_labels.capacity()) {
79+
return std::nullopt;
80+
}
81+
82+
dnsName->m_labels.push_back(toStringView(payload.subspan(sizeof(uint8_t), labelLength)));
83+
84+
payload = payload.subspan(labelLength + sizeof(uint8_t));
85+
}
86+
87+
return std::nullopt;
8488
}
8589

8690
bool DNSName::operator==(const DNSName& other) const noexcept
8791
{
88-
return std::equal(
89-
m_labels.begin(), m_labels.end(),
90-
other.m_labels.begin(), other.m_labels.end());
92+
return std::equal(
93+
m_labels.begin(),
94+
m_labels.end(),
95+
other.m_labels.begin(),
96+
other.m_labels.end());
9197
}
9298

9399
std::string DNSName::toString(const char delimiter) const noexcept
94100
{
95-
std::string res;
96-
std::ranges::for_each(m_labels, [&res, delimiter](std::string_view label) {
97-
res += label;
98-
res += delimiter;
99-
});
100-
101-
if (!res.empty()) {
102-
res.pop_back();
103-
}
104-
return res;
101+
std::string res;
102+
std::ranges::for_each(m_labels, [&res, delimiter](std::string_view label) {
103+
res += label;
104+
res += delimiter;
105+
});
106+
107+
if (!res.empty()) {
108+
res.pop_back();
109+
}
110+
return res;
105111
}
106112

107-
108-
109113
} // namespace ipxp
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
1+
/**
2+
* @file
3+
* @brief Provides DNS name structure and related functionalities.
4+
* @author Damir Zainullin <[email protected]>
5+
* @date 2025
6+
*
7+
* @copyright Copyright (c) 2025 CESNET, z.s.p.o.
8+
*/
9+
110
#pragma once
211

12+
#include <cstddef>
13+
#include <cstdint>
314
#include <optional>
415
#include <span>
5-
#include <cstdint>
6-
#include <cstddef>
7-
#include <arpa/inet.h>
816
#include <string>
17+
18+
#include <arpa/inet.h>
919
#include <boost/container/static_vector.hpp>
1020

11-
namespace ipxp
12-
{
21+
namespace ipxp {
1322

1423
/**
1524
* @brief Class represents DNS name
1625
*/
1726
class DNSName {
1827
public:
19-
20-
static
21-
std::optional<DNSName> createFrom(
22-
std::span<const std::byte> payload,
23-
std::span<const std::byte> fullDNSpayload) noexcept;
24-
25-
/**
26-
* @brief Converts DNS name to string
27-
* @param delimiter Delimiter to use between labels
28-
* @return Concatenated labels with delimiter
29-
*/
30-
std::string toString(const char delimiter = '.') const noexcept;
31-
32-
/**
33-
* @brief Get length of the DNS name
34-
* @return Length of DNS name excluding length of data pointed by DNS pointer
35-
* i.e after that count of bytes there is DNS question type field
36-
*/
37-
std::size_t length() const noexcept;
38-
39-
bool operator==(const DNSName& other) const noexcept;
28+
static std::optional<DNSName> createFrom(
29+
std::span<const std::byte> payload,
30+
std::span<const std::byte> fullDNSpayload) noexcept;
31+
32+
/**
33+
* @brief Converts DNS name to string
34+
* @param delimiter Delimiter to use between labels
35+
* @return Concatenated labels with delimiter
36+
*/
37+
std::string toString(const char delimiter = '.') const noexcept;
38+
39+
/**
40+
* @brief Get length of the DNS name
41+
* @return Length of DNS name excluding length of data pointed by DNS pointer
42+
* i.e after that count of bytes there is DNS question type field
43+
*/
44+
std::size_t length() const noexcept;
45+
46+
bool operator==(const DNSName& other) const noexcept;
4047

4148
private:
4249
constexpr static uint16_t MAX_LABEL_COUNT = 15;
4350

44-
boost::container::static_vector<std::string_view, MAX_LABEL_COUNT> m_labels;
45-
size_t m_length{0};
46-
bool m_isPointer {false};
51+
boost::container::static_vector<std::string_view, MAX_LABEL_COUNT> m_labels;
52+
size_t m_length {0};
53+
bool m_isPointer {false};
4754
};
4855

49-
5056
} // namespace ipxp

0 commit comments

Comments
 (0)