-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.cpp
308 lines (265 loc) · 6.5 KB
/
dns.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
KE Wang
822002009
*/
#pragma comment (lib,"Iphlpapi.lib")
#include "stdafx.h"
#include "dns.h"
CRITICAL_SECTION statistic_lock;
statistic stat;
bool is_integer(string s)
{
for(unsigned int i = 0; i < s.size(); i++)
if(s[i] > '9' || s[i] < '0')
return false;
return true;
}
vector<string> split(string& s, char seperator)
{
stringstream ss(s);
string token;
vector<string> result;
while(!ss.eof())
{
std::getline(ss,token,seperator);
result.push_back(token);
}
return result;
}
bool DNS::is_Ip_addr(string question)
{
for(unsigned int i = 0; i < question.size(); i++)
{
if(('0' <= question[i] && question[i] <= '9') || (question[i] == '.'))
continue;
else return false;
}
return true;
}
bool DNS::is_valiad_IP(string ip)
{
if(inet_addr(ip.c_str()) == INADDR_NONE)
return false;
}
vector<string> DNS::getDNSServer(void)
{
// MSDN sample code
FIXED_INFO *FixedInfo;
ULONG ulOutBufLen;
DWORD dwRetVal;
IP_ADDR_STRING * pIPAddr;
vector<string> hosts;
ulOutBufLen = sizeof(FIXED_INFO);
FixedInfo = (FIXED_INFO *) GlobalAlloc( GPTR, sizeof( FIXED_INFO ) );
ulOutBufLen = sizeof( FIXED_INFO );
if(ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &ulOutBufLen)) {
GlobalFree( FixedInfo );
FixedInfo = (FIXED_INFO *)GlobalAlloc( GPTR, ulOutBufLen );
}
if ( dwRetVal = GetNetworkParams( FixedInfo, &ulOutBufLen ) ) {
printf( "Call to GetNetworkParams failed. Return Value: %08x\n", dwRetVal );
return hosts;
}
else {
//printf( "Host Name: %s\n", FixedInfo->HostName );
//printf( "Domain Name: %s\n", FixedInfo->DomainName );
//printf( "DNS Servers:\n" );
//printf( "\t%s\n", FixedInfo->DnsServerList.IpAddress.String);
hosts.push_back(FixedInfo->DnsServerList.IpAddress.String);
pIPAddr = FixedInfo->DnsServerList.Next;
while ( pIPAddr ) {
hosts.push_back(pIPAddr ->IpAddress.String);
//printf( "\t%s\n", pIPAddr ->IpAddress.String);
pIPAddr = pIPAddr ->Next;
}
return hosts;
}
GlobalFree (FixedInfo);
}
void DNS::makeDNSquestion(char* question, vector<string>& tokens)
{
char* pos = question;
for(auto e : tokens)
{
*pos++ = e.size();
memcpy(pos,e.c_str(),e.size());
pos += e.size();
}
*pos = '\0';
}
string make_domain(const char* buf, int offset)
{
unsigned char* start = (unsigned char*)buf + offset;
unsigned char* current = start;
string ret;
while(*current != 0)
{
if((*current & 0xc0) == 0xc0)
{
ret += make_domain(buf, *(current+1));
return ret;
}
else
{
int len = *current;
for(int i=1; i <= len;i++)
{
ret += *(current + i);
}
ret += '.';
current += (len + 1);
}
}
return ret;
}
string DNS::make_result_string(const char* buf, int offset)
{
fixedDNSheader * fdh = (fixedDNSheader*) buf;
unsigned short Id = ntohs(fdh->ID);
unsigned short flags = ntohs(fdh->flags);
unsigned short answer_count = ntohs(fdh->answers);
unsigned short authority_count = ntohs(fdh->name_server_records);
unsigned short addition_count = ntohs(fdh->add_records);
if((flags & RCODE_MASK) == DNS_SERVERFAIL)
{
return "Authoritative DNS server not found\n";
}
else if((flags & RCODE_MASK) == DNS_ERROR)
{
return "";
}
else if((flags & RCODE_MASK) != DNS_OK)
{
return "Other errors.\n";
}
unsigned char* current = (unsigned char*)buf + offset;
int offset_data = 0;
while(*(current + offset_data) != '\0')
offset_data++;
if(offset_data %2 != 0)
offset_data++;
offset_data += sizeof(fixedRR) + 2;
return make_domain(buf,offset_data + offset);
}
int DNS::send_DNS_request(string hostname)
{
unsigned short query_type;
if(is_Ip_addr(hostname))
{
if(is_valiad_IP(hostname))
{
query_type = DNS_PTR;
}
else
{
printf("Invalid IP address\n");
return -1;
}
}
else
{
query_type = DNS_A;
}
unique_ptr<char> buf(new char[1024]);
vector<string> tokens;
tokens = split(hostname,'.');
if(query_type == DNS_PTR)
{
vector<string> tmp;
for(int i = tokens.size() - 1; i>=0; i--)
tmp.push_back(tokens[i]);
tmp.push_back("in-addr");
tmp.push_back("arpa");
tokens.clear();
tokens = tmp;
}
int pkt_size = 0;
int question_size = 0;
for(unsigned int i = 0; i < tokens.size(); i++)
question_size += tokens[i].size() + 1;
question_size += 1 + sizeof(queryHeader);
pkt_size += sizeof(fixedDNSheader) + question_size;
fixedDNSheader* dns_header = (fixedDNSheader*) buf.get();
queryHeader* query_header = (queryHeader*)(buf.get() + pkt_size - sizeof(queryHeader));
dns_header->ID = htons(++seq_num);
dns_header->flags = htons(DNS_STDQUERY | DNS_RD);
dns_header->questions = htons(1);
dns_header->add_records = 0;
dns_header->answers = 0;
dns_header->name_server_records = 0;
query_header->dns_class = htons(DNS_INET);
query_header->type = htons(query_type);
makeDNSquestion((char*)dns_header + sizeof(fixedDNSheader), tokens);
string server_ip = server_ips[rand()%server_ips.size()];
if(!s.sock_send(buf.get(),pkt_size,(char*)server_ip.c_str(),"53"))
{
printf("socket send error.\n");
return -1;
}
return seq_num;
}
pair<int,string> DNS::get_DNS_answer()
{
if(!s.sock_recv())
return make_pair(-1,"");
fixedDNSheader * fdh = (fixedDNSheader*) s.get_recv_buf();
unsigned short Id = ntohs(fdh->ID);
unsigned short flags = ntohs(fdh->flags);
unsigned short answer_count = ntohs(fdh->answers);
unsigned short authority_count = ntohs(fdh->name_server_records);
unsigned short addition_count = ntohs(fdh->add_records);
if((flags & RCODE_MASK) == DNS_SERVERFAIL)
{
return make_pair(Id,"Authoritative DNS server not found");
}
else if((flags & RCODE_MASK) == DNS_ERROR)
{
return make_pair(Id,"<no DNS entry>");
}
else if((flags & RCODE_MASK) != DNS_OK)
{
return make_pair(Id,"Other errors");
}
unsigned char* current = (unsigned char*)s.get_recv_buf() + sizeof(fixedDNSheader);
int offset_data = 0;
/*parse query*/
while(*(current + offset_data) != '\0')
{
if((*current & 0xc0) == 0xc0)
{
offset_data++;
break;
}
offset_data++;
}
offset_data++;
offset_data += sizeof(queryHeader);
/*parse name*/
while(*(current + offset_data) != '\0')
{
if((*(current + offset_data) & 0xc0) == 0xc0)
{
offset_data++;
break;
}
offset_data++;
}
offset_data++;
offset_data += sizeof(fixedRR) + 2;
return make_pair(Id,make_domain(s.get_recv_buf(),sizeof(fixedDNSheader) + offset_data));
}
class parameter
{
public:
queue<string>* questions;
queue<string>* answers;
CRITICAL_SECTION* cs;
};
void init_stat()
{
stat.local_dns_timeout = 0;
stat.no_auth_server = 0;
stat.no_dns_record = 0;
stat.sucess = 0;
stat.total_queries = 0;
}