Botcraft 1.21.4
Loading...
Searching...
No Matches
DNSResourceRecord.hpp
Go to the documentation of this file.
1#pragma once
2
4
5using namespace ProtocolCraft;
6
7namespace Botcraft
8{
10 {
11 public:
12 const std::vector<std::string>& GetNameLabels() const
13 {
14 return name_labels;
15 }
16
17 const unsigned short GetTypeCode() const
18 {
19 return type_code;
20 }
21
22 const unsigned short GetClassCode() const
23 {
24 return class_code;
25 }
26
27 const unsigned int GetTTL() const
28 {
29 return ttl;
30 }
31
32 const unsigned short GetRDLength() const
33 {
34 return rd_length;
35 }
36
37 const std::vector<unsigned char>& GetRData() const
38 {
39 return rdata;
40 }
41
42
43 void SetNameLabels(const std::vector<std::string>& name_labels_)
44 {
45 name_labels = name_labels_;
46 }
47
48 void SetTypeCode(const unsigned short type_code_)
49 {
50 type_code = type_code_;
51 }
52
53 void SetClassCode(const unsigned short class_code_)
54 {
55 class_code = class_code_;
56 }
57
58 void SetTTL(const unsigned int ttl_)
59 {
60 ttl = ttl_;
61 }
62
63 void SetRDLength(const unsigned short rd_length_)
64 {
65 rd_length = rd_length_;
66 }
67
68 void SetRData(const std::vector<unsigned char>& rdata_)
69 {
70 rdata = rdata_;
71 }
72
73 protected:
74 virtual void ReadImpl(ReadIterator& iter, size_t& length) override
75 {
76 name_labels = std::vector<std::string>();
77 bool done = false;
78
79 while (!done)
80 {
81 unsigned char first_byte = ReadData<unsigned char>(iter, length);
82 // Check if this is a pointer
83 if (first_byte >> 6 == 0x03)
84 {
85 unsigned char second_byte = ReadData<unsigned char>(iter, length);
86 // we actually don't need the value pointed in this application
87 name_labels.push_back("unresolved pointer");
88 done = true;
89 }
90 else if (first_byte == 0x00)
91 {
92 done = true;
93 }
94 else
95 {
96 name_labels.push_back(ReadRawString(iter, length, first_byte));
97 }
98 }
99
100 type_code = ReadData<unsigned short>(iter, length);
101 class_code = ReadData<unsigned short>(iter, length);
102 ttl = ReadData<unsigned int>(iter, length);
103 rd_length = ReadData<unsigned short>(iter, length);
104 rdata = ReadByteArray(iter, length, rd_length);
105 }
106
107 virtual void WriteImpl(WriteContainer& container) const override
108 {
109 // We don't support compression for write (it shouldn't been called in this program anyway)
110 for (int i = 0; i < name_labels.size(); ++i)
111 {
112 WriteData<unsigned char>(static_cast<unsigned char>(name_labels[i].size()), container);
113 WriteRawString(name_labels[i], container);
114 }
115 WriteData<unsigned char>(0, container);
116
117 WriteData<unsigned short>(type_code, container);
118 WriteData<unsigned short>(class_code, container);
119 WriteData<unsigned int>(ttl, container);
120 WriteData<unsigned short>(rd_length, container);
121 WriteByteArray(rdata, container);
122 }
123
124 virtual Json::Value SerializeImpl() const override
125 {
126 Json::Value output;
127
128 std::string name = "";
129 for (int i = 0; i < name_labels.size(); ++i)
130 {
131 name += name_labels[i] + ".";
132 }
133 output["identification"] = name;
134 output["type_code"] = type_code;
135 output["class_code"] = class_code;
136 output["ttl"] = ttl;
137 output["rd_length"] = rd_length;
138 output["rdata"] = rdata;
139
140 return output;
141 }
142
143 private:
144 std::vector<std::string> name_labels;
145 unsigned short type_code;
146 unsigned short class_code;
147 unsigned int ttl;
148 unsigned short rd_length;
149 std::vector<unsigned char> rdata;
150 };
151}
void SetNameLabels(const std::vector< std::string > &name_labels_)
virtual void WriteImpl(WriteContainer &container) const override
const unsigned short GetTypeCode() const
const unsigned int GetTTL() const
const std::vector< std::string > & GetNameLabels() const
void SetTTL(const unsigned int ttl_)
void SetTypeCode(const unsigned short type_code_)
const std::vector< unsigned char > & GetRData() const
const unsigned short GetRDLength() const
const unsigned short GetClassCode() const
virtual void ReadImpl(ReadIterator &iter, size_t &length) override
std::vector< unsigned char > rdata
virtual Json::Value SerializeImpl() const override
void SetRData(const std::vector< unsigned char > &rdata_)
void SetRDLength(const unsigned short rd_length_)
std::vector< std::string > name_labels
void SetClassCode(const unsigned short class_code_)
Main class, basically a JsonVariant with extra utility functions it doesn't inherit JsonVariant direc...
Definition Json.hpp:45
std::string ReadRawString(ReadIterator &iter, size_t &length, const size_t size)
std::vector< unsigned char > ReadByteArray(ReadIterator &iter, size_t &length, const size_t desired_length)
void WriteRawString(const std::string &s, WriteContainer &container)
std::vector< unsigned char > WriteContainer
std::vector< unsigned char >::const_iterator ReadIterator
void WriteByteArray(const std::vector< unsigned char > &my_array, WriteContainer &container)