-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.cpp
447 lines (361 loc) · 11.5 KB
/
resolver.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include <time.h>
#include "client.h"
#include "resolver.h"
#include "dns_message.h"
#include <netdb.h>
#include "recursive_resolver.rs"
#include <sys/types.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include<iostream>
#include<fstream>
#include<cstring>
#include<sstream>
#include <typeinfo>
#include "key_expand.h"
#include "encoding.h"
#include "decoding.h"
#include <unistd.h>
#define PORT 8089
using namespace std;
void aes_enc() {
int extendedlength = 0;
string myText;
ifstream File;
string filepath = "encryption.aes";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail()) {
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
fstream newfile;
newfile.open("input.txt",ios::in);
if (newfile.is_open()) {
string tp;
while(getline(newfile, tp)) {
int messlength=tp.length();
int extendedlength;
if((messlength%16)!=0)
{
extendedlength=messlength+(16-(messlength%16));
}
else
{
extendedlength=messlength;
}
unsigned char* encryptedtext=new unsigned char[extendedlength];
for(int i=0;i<extendedlength;i++)
{
if(i<messlength)
encryptedtext[i]=tp[i];
else
encryptedtext[i]=0;
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
istringstream tempkey(k);
unsigned char key[16];
unsigned int x;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x;
key[i] = x;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for(int i=0;i<extendedlength;i+=16)
{
unsigned char* temp=new unsigned char[16];
for(int j=0;j<16;j++)
{
temp[j]=encryptedtext[i+j];
}
encryption(temp , extendedkeys);
for(int j=0;j<16;j++)
{
encryptedtext[i+j]=temp[j];
}
}
ofstream fout;
ifstream fin;
fin.open("encryption.aes");
fout.open ("encryption.aes",ios::app);
if(fin.is_open())
fout<<encryptedtext<<"\n";
fin.close();
fout.close();
}
newfile.close();
}
}
void aes_dec() {
int extendedlength=0;
string myText;
string tp;
ifstream File;
string filepath = "outputtext.txt";
File.open(filepath.c_str(), std::ifstream::out | std::ifstream::trunc );
if (!File.is_open() || File.fail())
{
File.close();
printf("\nError : failed to erase file content !");
}
File.close();
ifstream MyReadFile;
MyReadFile.open("encryption.aes", ios::in | ios::binary);
if(MyReadFile.is_open())
{
while( getline (MyReadFile, myText) )
{
cout.flush();
char * x;
x=&myText[0];
int messlength=strlen(x);
char * msg = new char[myText.size()+1];
strcpy(msg, myText.c_str());
int n = strlen((const char*)msg);
unsigned char * decryptedtext = new unsigned char[n];
for (int i = 0; i < n; i++) {
decryptedtext[i] = (unsigned char)msg[i];
}
string k;
ifstream infile;
infile.open("key.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
else cout << "Unable to open file";
istringstream tempkey(k);
unsigned char key[16];
unsigned int x1;
for(int i=0;i<16;i++)
{
tempkey>>hex>>x1;
key[i] = x1;
}
unsigned char extendedkeys[176];
Key_extenxion(key,extendedkeys);
for (int i = 0; i < messlength; i += 16)
{
unsigned char * temp=new unsigned char[16];
for(int j=0;j<16;j++)
temp[j]=decryptedtext[i+j];
decryption(temp , extendedkeys);
for(int j=0;j<16;j++)
decryptedtext[i+j]=temp[j];
}
for(int i=0;i<messlength;i++)
{
if(decryptedtext[i]==0 && decryptedtext[i-1]==0 )
break;
}
cout<<endl;
ofstream fout;
ifstream fin;
fin.open("outputtext.txt");
fout.open ("outputtext.txt",ios::app);
if(fin.is_open())
fout<<decryptedtext<<"\n";
fin.close();
fout.close();
}
}
else
{
cout<<"Can not open input file\n ";
}
MyReadFile.close();
}
void resolve()
{
}
extern "C" void get_resolved_query(string qname);
extern "C" void get_unresolved_query(string qname);
// int get_resolved_ns(string qname)
// {
// }
// int get_unresolved_ns(string qname)
// {
// }
int lookup(string qname, unsigned short qtype, string server)
{
// let socket = UdpSocket::bind(("0.0.0.0", 43210))?;
int s = socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(53);
dest.sin_addr.s_addr = inet_addr(server.c_str());
if(inet_pton(AF_INET, server.c_str(), &dest.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return 0;
}
if (connect(s, (struct sockaddr *)&dest, sizeof(dest)) < 0)
{
printf("\nConnection Failed \n");
return 0;
}
cout << "Connection successful" << endl;
// let mut packet = DnsPacket::new();
// packet.header.id = 6666;
// packet.header.questions = 1;
// packet.header.recursion_desired = true;
// packet
// .questions
// .push(DnsQuestion::new(qname.to_string(), qtype));
// let mut req_buffer = BytePacketBuffer::new();
// packet.write(&mut req_buffer)?;
unsigned char buf[65536];
unsigned char buf_recv[65536] = {0};
DNS_HEADER *header = NULL;
header = (DNS_HEADER*)&buf;
header->id = 6666;
header->q_count = 1;
header->rd = 1;
header->response = false;
header->tc = 0;
header->aa = 0;
header->opcode = 0;
header->qr = 0;
header->rcode = 0;
header->z = 0;
header->ra = 0;
header->ans_count = 0;
header->ns_count = 0;
header->rr_count = 0;
// QER* q = NULL;
// q = (QER*)&buf[sizeof(DNS_HEADER)];
// unsigned char *val = new unsigned char[qname.length()+1];
// strcpy((char *)val, qname.c_str());
// q->name = val;
QUESTION* question = NULL;
question = (QUESTION*)&buf[sizeof(DNS_HEADER)];
unsigned char *val = new unsigned char[qname.length()+1];
strcpy((char *)val, qname.c_str());
question->name = val;
question->qtype = qtype;
// q->ques = question;
// question->qtype = qtype;
// question->qclass = htons(1);
// unsigned char *answer = (unsigned char*)&buf[sizeof(DNS_HEADER) + sizeof(QER)];
// RESOURCE_DATA* rd = NULL;
// rd = (RESOURCE_DATA*)&buf[sizeof(DNS_HEADER) + sizeof(QER) + (strlen((const char*)answer) + 1)];
RESOURCE_RECORD* answers = NULL;
answers = (RESOURCE_RECORD*)&buf[sizeof(DNS_HEADER) + sizeof(QUESTION)];
RESOURCE_RECORD* authorities = NULL;
authorities = (RESOURCE_RECORD*)&buf[sizeof(DNS_HEADER) + sizeof(QUESTION) + sizeof(RESOURCE_RECORD)];
RESOURCE_RECORD* resources = NULL;
resources = (RESOURCE_RECORD*)&buf[sizeof(DNS_HEADER) + sizeof(QUESTION) + 2 * sizeof(RESOURCE_RECORD)];
// string hello = "Hello from client";
// send(s, hello.c_str(), hello.length(), 0);
sendto(s, (char*)buf, sizeof(DNS_HEADER) + sizeof(QUESTION) + 3 * sizeof(RESOURCE_RECORD), MSG_CONFIRM, (const struct sockaddr *) &dest,
sizeof(dest));
cout << "sent" << endl;
int len;
int valread = recvfrom(s, (char*)buf_recv, sizeof(DNS_HEADER) + sizeof(QUESTION) + 3 * sizeof(RESOURCE_RECORD), MSG_WAITALL, (struct sockaddr *) &dest,
NULL);
cout << "Received" << endl;
DNS_HEADER *head = (DNS_HEADER*)&buf_recv;
QUESTION* quesn = (QUESTION*)&buf_recv[sizeof(DNS_HEADER)];
RESOURCE_RECORD* ans = (RESOURCE_RECORD*)&buf_recv[sizeof(DNS_HEADER) + sizeof(QUESTION)];
RESOURCE_RECORD* auth = (RESOURCE_RECORD*)&buf_recv[sizeof(DNS_HEADER) + sizeof(QUESTION) + sizeof(RESOURCE_RECORD)];
RESOURCE_RECORD* rs = (RESOURCE_RECORD*)&buf_recv[sizeof(DNS_HEADER) + sizeof(QUESTION) + 2 * sizeof(RESOURCE_RECORD)];
cout << "ANSWER" << ans->name << endl;
// socket.send_to(&req_buffer.buf[0..req_buffer.pos], server)?;
// let mut res_buffer = BytePacketBuffer::new();
// socket.recv_from(&mut res_buffer.buf)?;
// DnsPacket::from_buffer(&mut res_buffer)
return 0;
}
int recursive_lookup (string qname, unsigned short qtype, string server)
{
int s = socket(AF_INET,SOCK_DGRAM,0);
sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_port = htons(53);
dest.sin_addr.s_addr = inet_addr(server.c_str());
if(inet_pton(AF_INET, server.c_str(), &dest.sin_addr)<=0)
{
return 0;
}
if (connect(s, (struct sockaddr *)&dest, sizeof(dest)) < 0)
{
return 0;
}
int response = lookup(qname, qtype, server);
if(response != NULL)
{
return true;
}
else {
}
auto new_ns_name = 0;
// int recursive_response = recursive_lookup(new_ns_name, qtype);
}
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
unsigned char buf[65536] = {0};
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) {
perror("accept");
exit(EXIT_FAILURE);
}
valread = read(new_socket, buf, 65536);
cout << "Received" << endl;
DNS_HEADER *header = (DNS_HEADER*)&buf;
unsigned char *reader = &buf[sizeof(DNS_HEADER)];
QUESTION *ques = (QUESTION*)&buf[sizeof(DNS_HEADER) + (strlen((const char*)reader) + 1)];
unsigned char *response = (unsigned char*) malloc (1000000 * sizeof(unsigned char));
aes_dec();
string k;
ifstream infile;
infile.open("outputtext.txt");
if (infile.is_open())
{
getline(infile, k);
infile.close();
}
cout << "Doing lookup!" << endl;
int l = lookup("3www6google3com", 1, "199.9.14.201");
cout << "Done lookup" << endl;
return 0;
}