Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
DnsPackage.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
19
20#pragma once
21
22#include <cstdint>
23#include <memory>
24#include <string>
25#include <vector>
26#include <set>
27#include <map>
28
29namespace sese::net::dns {
30
33public:
35 struct Flags {
36 bool qr = false;
37 uint8_t opcode = 0;
38 bool aa = false;
39 bool tc = false;
40 bool rd = false;
41 bool ra = false;
42 uint8_t z = false;
43 uint8_t rcode = false;
44
45 [[nodiscard]] uint16_t encode() const;
46
47 void decode(uint16_t flags);
48 };
49
50 class Index;
51
53 struct Question {
54 std::string name;
55 uint16_t type;
56 uint16_t class_;
57 };
58
60 struct Answer {
61 std::string name;
62 uint16_t type;
63 uint16_t class_;
64 uint32_t ttl;
65 uint16_t data_length;
66 std::unique_ptr<uint8_t[]> data{};
67 };
68
73
74private:
75 struct DnsHeader {
76 uint16_t id;
77 uint16_t flags;
78 } header{};
79
80 std::vector<Question> questions;
81 std::vector<Answer> answers;
82 std::vector<Authority> authorities;
83 std::vector<Additional> additionals;
84
85 DnsPackage() = default;
86
93 static std::string decodeWords(const uint8_t *buffer, size_t length, size_t &offset); // NOLINT
94
95 static bool decodeAnswers(std::vector<Answer> &answers, size_t expect_size, const uint8_t *buffer, size_t length, size_t &pos);
96
97 static std::string encodeWords(const std::string &fullname);
98
99 static bool encodeQuestions(const std::vector<Question> &questions, void *buffer, size_t &length, Index &index, size_t offset);
100
101 static bool encodeAnswers(const std::vector<Answer> &answers, void *buffer, size_t &length, Index &index, size_t offset);
102
103public:
105 class Index {
106 friend class DnsPackage;
107 using CompressMapping = std::set<uint16_t>;
109 uint16_t index;
110 std::string name;
111 uint16_t pos;
112 };
113
114 std::vector<CompressIndex> compress_index;
115 std::map<std::string, CompressMapping> compress_mapping;
116
117 Index(
118 std::vector<Question> &questions,
119 std::vector<Answer> &answers,
120 std::vector<Authority> &authorities,
121 std::vector<Additional> &additionals
122 );
123
124 [[nodiscard]] std::vector<CompressIndex *> getSortedIndexes(const std::set<uint16_t> &indexes);
125
126 static void updatePos(std::vector<CompressIndex *> &indexes, uint16_t offset);
127
128 void clearPos() const;
129
130 std::string encodeWords(const std::string &fullname, const std::set<uint16_t> &indexes, uint16_t base_offset);
131
132 public:
133 Index() = default;
134 };
135
136 using Ptr = std::shared_ptr<DnsPackage>;
137
139 static Ptr new_();
140
145 static Ptr decode(const uint8_t *buffer, size_t length);
146
150
156 bool encode(void *buffer, size_t &length, Index &index) const;
157
158 [[nodiscard]] std::vector<Question> &getQuestions() { return questions; }
159 [[nodiscard]] std::vector<Answer> &getAnswers() { return answers; }
160 [[nodiscard]] std::vector<Authority> &getAuthorities() { return authorities; }
161 [[nodiscard]] std::vector<Additional> &getAdditionals() { return additionals; }
162 [[nodiscard]] auto getId() const { return header.id; }
163 [[nodiscard]] auto getFlags() const { return header.flags; }
164
165 void setId(uint16_t id) { header.id = id; }
166 void setFlags(uint16_t flags) { header.flags = flags; }
167};
168
169
170} // namespace sese::net::dns