-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdns.cpp
More file actions
200 lines (174 loc) · 5.34 KB
/
dns.cpp
File metadata and controls
200 lines (174 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <bitset>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <stdexcept>
#include <netinet/in.h>
#include "dns.hpp"
namespace dns
{
std::uint16_t Flags::GetBits(const Flags::Bits bits) const
{
std::bitset<16> bitset{bytes};
switch (bits)
{
case Bits::QR:
return bitset[15];
case Bits::OPCODE:
{
std::bitset<4> result{};
result[0] = bitset[14];
result[1] = bitset[13];
result[2] = bitset[12];
result[3] = bitset[11];
return result.to_ulong();
}
case Bits::AA:
return bitset[10];
case Bits::TC:
return bitset[9];
case Bits::RD:
return bitset[8];
case Bits::RA:
return bitset[7];
case Bits::Z:
return bitset[6];
case Bits::AD:
return bitset[5];
case Bits::CD:
return bitset[4];
case Bits::RCODE:
{
std::bitset<4> result{};
result[0] = bitset[3];
result[1] = bitset[2];
result[2] = bitset[1];
result[3] = bitset[0];
return result.to_ulong();
}
}
}
void Flags::SetBits(const Flags::Bits bits, const std::uint16_t value)
{
std::bitset<16> result{bytes};
switch (bits)
{
case Bits::QR:
result[15] = value;
break;
case Bits::OPCODE:
{
std::bitset<4> bitset{value};
result[14] = bitset[0];
result[13] = bitset[1];
result[12] = bitset[2];
result[11] = bitset[3];
break;
}
case Bits::AA:
result[10] = value;
break;
case Bits::TC:
result[9] = value;
break;
case Bits::RD:
result[8] = value;
break;
case Bits::RA:
result[7] = value;
break;
case Bits::Z:
result[6] = value;
break;
case Bits::AD:
result[5] = value;
break;
case Bits::CD:
result[4] = value;
break;
case Bits::RCODE:
{
std::bitset<4> bitset{value};
result[3] = bitset[0];
result[2] = bitset[1];
result[1] = bitset[2];
result[0] = bitset[3];
break;
}
}
bytes = result.to_ulong();
}
Query::Query(const std::vector<std::byte>& bytes)
{
ParseHeader(bytes);
ParseQuestion(bytes);
}
void Query::ParseHeader(const std::vector<std::byte>& bytes)
{
if (bytes.size() < 12)
{
throw std::runtime_error("insuffucient DNS query length");
}
std::memcpy(&header.transactionId, bytes.data(), 2);
header.transactionId = ntohs(header.transactionId);
std::memcpy(&header.flags, bytes.data() + 2, 2);
header.flags.bytes = ntohs(header.flags.bytes);
if (header.flags.GetBits(Flags::Bits::QR) != 0)
{
throw std::runtime_error("not a DNS query");
}
std::memcpy(&header.numberOfQuestions, bytes.data() + 4, 2);
header.numberOfQuestions = ntohs(header.numberOfQuestions);
if (header.numberOfQuestions > 1)
{
throw std::runtime_error("only one question is supported per query");
}
std::memcpy(&header.numberOfAnswers, bytes.data() + 6, 2);
header.numberOfAnswers = ntohs(header.numberOfAnswers);
std::memcpy(&header.numberOfAuthorityRRs, bytes.data() + 8, 2);
header.numberOfAuthorityRRs = ntohs(header.numberOfAuthorityRRs);
std::memcpy(&header.numberOfAuthorityRRs, bytes.data() + 10, 2);
header.numberOfAuthorityRRs = ntohs(header.numberOfAuthorityRRs);
}
void Query::ParseQuestion(const std::vector<std::byte>& bytes)
{
if (header.numberOfQuestions > 0 && bytes.size() < (12 + 1 + 4))
{
throw std::runtime_error("insufficient DNS question length");
}
// TODO: добавьте десериализацию секции question из массива
// байт bytes. В первых 12 элементах массива находится
// заголовок header, question находится сразу после него.
// В этом методе необходимо заполнить поле this->question.
}
std::vector<std::byte> Response::Serialize() const
{
std::vector<std::byte> bytes{};
SerializeHeader(bytes);
SerializeQuestion(bytes);
return bytes;
}
void Response::SerializeHeader(std::vector<std::byte>& bytes) const
{
bytes.resize(sizeof(header));
std::uint16_t transactionId = htons(header.transactionId);
std::memcpy(&bytes[0], &transactionId, 2);
std::uint16_t flags = htons(header.flags.bytes);
std::memcpy(&bytes[2], &flags, 2);
std::uint16_t numberOfQuestions = htons(header.numberOfQuestions);
std::memcpy(&bytes[4], &numberOfQuestions, 2);
std::uint16_t numberOfAnswers = htons(header.numberOfAnswers);
std::memcpy(&bytes[6], &numberOfAnswers, 2);
std::uint16_t numberOfAuthorityRRs = htons(header.numberOfAuthorityRRs);
std::memcpy(&bytes[8], &numberOfAuthorityRRs, 2);
std::uint16_t numberOfAdditionalRRs = htons(header.numberOfAdditionalRRs);
std::memcpy(&bytes[10], &numberOfAdditionalRRs, 2);
}
void Response::SerializeQuestion(
[[maybe_unused]] std::vector<std::byte>& bytes) const
{
// TODO: добавьте сериализацию поля this->question в массив
// байтов bytes. Bytes уже содержит сериализованный заголовок
// header, question необходимо добавить в конец массива.
}
} // namespace dns