-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageDealer.cpp
285 lines (235 loc) · 9.7 KB
/
MessageDealer.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//
// Created by 92798 on 2021/7/5.
//
#include "MessageDealer.h"
extern IDTransform IDTransTable[ID_AMOUNT];
extern int ID_COUNT;
DNS_HEADER *MessageDealer::getDNSHeader(char *buff) {
auto *header = new DNS_HEADER();
char *tmp_ptr = buff;
u_short t_id = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(t_id);
u_short flags = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(flags);
u_short question = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(question);
u_short answer_rr = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(flags);
u_short authority_rr = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(flags);
u_short additional_rr = ntohs(*(u_short *) tmp_ptr);
header->id = t_id;
header->question = question;
header->flags = flags;
header->answer_RR = answer_rr;
header->authority_RR = authority_rr;
header->additional_RR = additional_rr;
return header;
}
std::string MessageDealer::getHostName(char *buff, char *domain_start_ptr) {
int len = std::strlen(buff);
int c0label = -1;
char *domain = (char *) malloc((sizeof(char *) * len) + 1);
strcpy(domain, buff);
for (int i = 0; i < len; i++) {
if (domain[i] == -64) {
c0label = i;
break;
}
if (domain[i] < 33)
domain[i] = '.';
}
std::string domain_str = domain;
if (c0label != -1) {
domain_str = domain_str.substr(0, c0label);
int data_ptr = ntohs(*(u_short *) (buff + c0label)) - 49152;
char *data_start_str = domain_start_ptr + data_ptr;
domain_str.append(".");
domain_str.append(MessageDealer::getHostName(data_start_str, domain_start_ptr));
}
domain_str.erase(0, domain_str.find_first_not_of(".")).erase(domain_str.find_last_not_of(".") + 1);
return domain_str;
}
DNS_QUERY *MessageDealer::getDNSQuery(char *buff) {
char *tmp_buff = buff;
auto *query = new DNS_QUERY();
tmp_buff += 12;
std::string domain_str = MessageDealer::getHostName(tmp_buff, buff);
int len = std::strlen(tmp_buff);
tmp_buff += len;
++tmp_buff;
u_short type = ntohs(*(u_short *) tmp_buff);
tmp_buff += sizeof(type);
u_short class_ = ntohs(*(u_short *) tmp_buff);
int query_len = 12 + len + 1 + 2 + 2;
query->name = domain_str;
if (type == 1)
query->type = "IPV4";
else if (type == 28)
query->type = "IPV6";
else if (type == 12)
query->type = "PTR";
else
query->type = std::to_string(type);
query->class_ = class_;
query->headerAndQueryLength = query_len; //这里是为了读response方便,这个长度包含了头部长度以及query长度,需要receiver直接加这个length得到response主体
return query;
}
std::vector<DNS_RESPONSE> MessageDealer::getAllResponses(char *ptr, int startPoint, int num) {
std::vector<DNS_RESPONSE> result;
char *tmp_buff = ptr + startPoint;
int *length = new int;
while (num > 0) {
--num;
result.push_back(getResponse(tmp_buff, length, ptr));
if (num != 0)
tmp_buff += *length;
}
return result;
}
DNS_RESPONSE MessageDealer::getResponse(char *ptr, int *length, char *domain_start_ptr) {
auto *response = new DNS_RESPONSE();
char *tmp_ptr = ptr;
int name_ptr = ntohs(*(u_short *) tmp_ptr) - 49152; //c000;
char *name_start_str = domain_start_ptr + name_ptr;
response->name = MessageDealer::getHostName(name_start_str, domain_start_ptr);
tmp_ptr += sizeof(u_short);
u_short type = ntohs(*(u_short *) tmp_ptr);
if (type == 1)
response->type = "IPV4";
else if (type == 28)
response->type = "IPV6";
else if (type == 5)
response->type = "CNAME";
else
response->type = std::to_string(type);
tmp_ptr += sizeof(u_short);
response->class_ = ntohs(*(u_short *) tmp_ptr);
tmp_ptr += sizeof(u_short);
response->ttl = ntohl(*(u_long *) tmp_ptr);
tmp_ptr += sizeof(u_long);
u_short data_length = ntohs(*(u_short *) tmp_ptr);
response->data_length = data_length;
tmp_ptr += sizeof(u_short);
char *data = new char[1024];
memset(data, 0, sizeof(data));
memcpy(data, tmp_ptr, data_length);
std::string data_str;
if (type == 1) {
data_str = charToIpv4(data);
} else if (type == 28) {
data_str = charToIpv6(data);
} else {
data_str = MessageDealer::getHostName(tmp_ptr, domain_start_ptr);
}
response->data = data_str;
*length = sizeof(u_short) * 4 + data_length + sizeof(u_long);
return *response;
}
Message MessageDealer::messageInit(char *ptr, bool isResponse) {
auto *message = new Message();
message->setHeader(getDNSHeader(ptr));
DNS_QUERY *query = getDNSQuery(ptr);
message->setQuery(query);
if (isResponse && (query->type == "IPV4" || query->type == "IPV6")) {
int len = query->headerAndQueryLength;
message->setResponses(getAllResponses(ptr, len, message->getHeader()->answer_RR));
}
return *message;
}
char *MessageDealer::ipv4ToChar(const std::string &str) {
char *p = (char *) str.data();
char *buf = new char[1024];
inet_pton(AF_INET, p, buf);
return buf;
}
char *MessageDealer::ipv6ToChar(const std::string &str) {
char *p = (char *) str.data();
char *buf = new char[1024];
inet_pton(AF_INET6, p, buf);
return buf;
}
std::string MessageDealer::charToIpv6(char *str) {
char result_char[40];
inet_ntop(AF_INET6, str, result_char, 40);
std::string result = result_char;
return result;
}
std::string MessageDealer::charToIpv4(char *str) {
char *result_char = new char[40];
inet_ntop(AF_INET, str, result_char, 40);
std::string result = result_char;
return result;
}
unsigned short MessageDealer::getNewID(unsigned short recv_ID, sockaddr_in receive_in, BOOL processed) {
IDTransTable[ID_COUNT].oldID = recv_ID;
IDTransTable[ID_COUNT].client = receive_in;
IDTransTable[ID_COUNT].done = processed;
++ID_COUNT;
return (unsigned short) (ID_COUNT - 1);
}
void MessageDealer::printResponsesDetailed(const std::vector<DNS_RESPONSE> &responses) {
int count = 1;
for (const auto &response : responses) {
std::cout << "Response: "
<< "count: " << count
<< " name:" << response.name
<< " type:" << response.type
<< " class:" << response.class_
<< " ttl:" << response.ttl
<< " dataLength:" << response.data_length
<< " data:" << response.data << std::endl;
++count;
}
}
void MessageDealer::printQueryDetailed(DNS_QUERY *query) {
std::cout << "Query: " << "domain name:" << query->name
<< " type:" << query->type
<< " class:" << query->class_
<< " H&Qlength:"
<< query->headerAndQueryLength << std::endl;
}
void MessageDealer::printHeaderDetailed(DNS_HEADER *header) {
std::cout << "Header: " << "id:" << header->id;
printf(" flag: 0x%x", header->flags);
std::cout << " question:" << header->question << " answer:"
<< header->answer_RR << " authority:"
<< header->authority_RR << " additional:" << header->additional_RR << std::endl;
}
void MessageDealer::printDetailedInfo(Message message) {
MessageDealer::printHeaderDetailed(message.getHeader());
MessageDealer::printQueryDetailed(message.getQuery());
MessageDealer::printResponsesDetailed(message.getResponses());
}
void MessageDealer::printQuerySimple(DNS_QUERY *query) {
std::cout << "QuerySimpleInfo: "
<< "domain name:" << query->name
<< " type:" << query->type << std::endl;
}
void MessageDealer::printResponsesSimple(const std::vector<DNS_RESPONSE> &responses) {
int count = 1;
if (responses.empty()) {
std::cout << "No address return" << std::endl;
return;
}
for (const auto &response : responses) {
if (response.type == "IPV4" || response.type == "IPV6")
std::cout << "Response: " << " data: " << response.data << std::endl;
++count;
}
}
bool MessageDealer::isIntercept(Message message) {
int flag = message.getHeader()->flags;
return flag % 16 == 3;
}
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}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3})|(:(:[0-9A-Fa-f]{1,4}){0,5}:(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}))$)");
std::regex V4(R"(^(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])(\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){3}$)");
if (std::regex_match(ip, V4)) {
return true;
} else if (std::regex_match(ip, V6)) {
return true;
}
return false;
}