-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.cpp
532 lines (404 loc) · 12 KB
/
proxy.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<assert.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<sys/time.h>
#include <iostream>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
#define MAXFD 10 //Size of fds array
// used to store actual data of routing table
vector<string> servers;
vector<string> clients;
vector<string> ports;
// proxy cache
vector<string> sites;
vector<string> ips;
// used in cross-communication
int to_send = 0;
// status for circuit switch
bool busy = false;
// find if website is cached in proxy
string getIpFromCache(string site) {
for (int i = 0; i < sites.size(); i++) {
if (sites[i] == site)
return ips[i];
}
return "";
}
string getServerFromClientPort(string port) {
for (int i = 0; i < ports.size(); i++) {
if (ports[i] == port)
return servers[i];
}
return "";
}
void displayVectors() {
for (int i = 0; i < sites.size(); i++) {
cout << sites[i] << " = " << ips[i] << endl;
}
}
string ip(string domain)
{
string word;
string found; //If the website is found in the DNS Server
ifstream obj("cache.txt", ios::in);
string temp = "www." + domain; //Adding "www." at start of the domain name
temp += ".com"; //Adding ".com" at end of the domain name
while (!obj.eof())
{
obj >> word; //Reading word by word
if (word == temp)
{
obj >> found;
//cout << "IP address of '"<<temp<<"' = " << found << endl;
return found;
}
}
obj.close();
return "";
}
void updateCache(string domain, string ip) {
ofstream out("cache.txt", ios::app);
if (out) {
out << "www." << domain << ".com " << ip << endl;
out.close();
}
}
void parseRoutingTable(string rtable) {
servers.clear();
clients.clear();
ports.clear();
// removing header lines
rtable.erase(0, 34);
// finding number of lines in table
int count = 0;
for (int i = 0; i < rtable.size(); i++) {
if (rtable[i] == 10)
count++;
}
// extracting info in each line, storing in vectors
istringstream ss(rtable);
string c, p, s;
for (int i = 0; i < count; i++) {
ss >> s;
ss >> c;
ss >> p;
servers.push_back(s);
clients.push_back(c);
ports.push_back(p);
}
}
// sends one message to a server/port then closes connection
void sendMsgToServer(int port, string msg) {
int sockfd = socket(AF_INET,SOCK_STREAM,0);
assert(sockfd != -1 );
//Set Address Information
struct sockaddr_in saddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");
//Link to server
int res = connect(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
assert(res != -1);
send(sockfd, msg.c_str(), msg.size(), 0);
close(sockfd);
}
void fds_add(int fds[],int fd) //Add a file descriptor to the fds array
{
int i=0;
for(;i<MAXFD;++i)
{
if(fds[i]==-1)
{
fds[i]=fd;
break;
}
}
}
string charPtrToString(char* toConvert)
{
string str;
for (int i = 0; toConvert[i]; i++)
str += toConvert[i];
return str;
}
// returns routing table as a string
string getRoutingTable(string s1, string s2, string s4) {
string s = "ROUTING TABLE:\n";
s += "Server\tClient\tPort\n";
s += s1 + s2 + s4 + "\n";
//cout << s;
return s;
}
int main()
{
string s_name = "S3 (proxy dns)";
int s_port = 20003;
string s1_str, s2_str, s4_str;
int s_fd[3] = {0};
int s_ports[3] = {20001, 20002, 20004};
int s_count = 0;
bool status = false;
int count = 0;
int sockfd=socket(AF_INET,SOCK_STREAM,0);
assert(sockfd!=-1);
//printf("sockfd=%d\n",sockfd);
struct sockaddr_in saddr,caddr;
memset(&saddr,0,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(s_port);
saddr.sin_addr.s_addr=inet_addr("192.168.100.21");
int res=bind(sockfd,(struct sockaddr*)&saddr,sizeof(saddr));
assert(res!=-1);
//Create listening queue
listen(sockfd,5);
//Define fdset collection
fd_set fdset;
//Define fds array
int fds[MAXFD];
int i=0;
for(;i<MAXFD;++i)
fds[i]=-1;
fds_add(fds,sockfd);
string client;
while(1)
{
FD_ZERO(&fdset);
int maxfd=-1;
int i=0;
for(;i<MAXFD;i++)
{
if(fds[i]==-1)
continue;
FD_SET(fds[i],&fdset);
if(fds[i]>maxfd)
maxfd=fds[i];
}
struct timeval tv={5,0};
int n=select(maxfd+1,&fdset,NULL,NULL,&tv);
if(n==-1) //fail
perror("select error");
else if(n==0){}
//printf("time out\n");
else
{
for(i=0;i<MAXFD;++i)
{
if(fds[i]==-1)
continue;
if(FD_ISSET(fds[i],&fdset))
{
// new connections
if(fds[i]==sockfd)
{
struct sockaddr_in caddr;
int len=sizeof(caddr);
int c=accept(sockfd,(struct sockaddr *)&caddr,(socklen_t*)&len);
if(c<0)
{
continue;
}
fds_add(fds,c);
s_fd[count] = c;
count++;
}
//Receive data recv when an existing client sends data
else
{
char buff[500]={0};
int res = recv(fds[i],buff,sizeof(buff),0);
if(res<=0)
{
close(fds[i]);
fds[i]=-1;
}
else
{
char flag1 = buff[0];
char flag2 = buff[1];
// a new client has connected
if (flag1 == '0')
{
string temp = charPtrToString(buff);
string temp2 = temp;
memset(buff,0,sizeof(buff));
temp.erase(0, 2);
int pos = temp.find('+');
string client_conn = temp.substr(0, pos);
cout << client_conn << endl; // display a client has connected
temp.erase(0, pos + 1);
if (flag2 == '1') { // server 1
s1_str += temp;
}
else if (flag2 == '2') { // server 2
s2_str += temp;
}
else if (flag2 == '4') { // server 4
s4_str += temp;
}
string s = getRoutingTable(s1_str, s2_str, s4_str);
cout<<"=================================================="<<endl;
cout << s << endl;
cout<<"=================================================="<<endl;
string all_info = "1";
all_info += client_conn + "+" + s;
//Send (flagged) message to all servers about client connected to any server + routing table
send(4, all_info.c_str(), all_info.size(), 0); // to s1
send(5, all_info.c_str(), all_info.size(), 0); // to s2
send(6, all_info.c_str(), all_info.size(), 0); // to s4
}
// recieving list of clients of s1
/* else if (flag1 == '1')
{
string str = charPtrToString(buff);
memset(buff,0,sizeof(buff));
str.erase(0, 2);
s1_str = str;
cout << getRoutingTable(s1_str, s2_str, s4_str);
}
// recieving list of clients of s2
else if (flag1 == '2')
{
cout << "into 2" << endl;
//recv(fds[i],buff,sizeof(buff),0);
string str = charPtrToString(buff);
memset(buff,0,sizeof(buff));
str.erase(0, 2);
s2_str = str;
cout << getRoutingTable(s1_str, s2_str, s4_str);
}
// recieving list of clients of s4
else if (flag1 == '4')
{
//recv(fds[i],buff,sizeof(buff),0);
string str = charPtrToString(buff);
memset(buff,0,sizeof(buff));
str.erase(0, 2);
s4_str = str;
cout << getRoutingTable(s1_str, s2_str, s4_str);
} */
// server requesting routing table (for client)
else if (flag1 == 5) {
string rtable = getRoutingTable(s1_str, s2_str, s4_str);
string rt = "9";
rt.append(rtable);
send(fds[i], rt.c_str(), rt.size(), 0);
}
// server forwarding client message
else if (flag1 == '6') {
busy = true;
bool status = false;
while (1) {
if (status == true) {
memset(buff ,0,sizeof(buff));
recv(fds[i], buff, sizeof(buff), 0);
}
bool breakLoop = false;
if (buff[0] == '7')
breakLoop = true;
status = true;
cout << "Server has sent: " << buff << endl;
string msg = charPtrToString(buff);
memset(buff,0,sizeof(buff));
// removing flag info at start of buff/string
msg.erase(0, 2);
// extracting port of recieving client
int pos = msg.find('+');
string port = msg.substr(0, pos);
// removing port from msg string
msg.erase(0, pos);
// removing leftover '+' at start
msg.erase(0, 1);
string data = msg;
cout << "Data = " << data << endl;
// extracting server/clients/ports from routing table
string rtable = getRoutingTable(s1_str, s2_str, s4_str);
parseRoutingTable(rtable);
string server = getServerFromClientPort(port);
cout << "Server = " << server << endl;
string t = "7";
data = t + "+" + port + "+" + data;
if (server == "S1")
to_send = 4;
else if (server == "S2")
to_send = 5;
else if (server == "S4")
to_send = 6;
else
cout << "Server not found for that client port" << endl;
// sending to relevant server
send(to_send, data.c_str(), data.size(), 0);
char buff2[200];
// recieving return message from relevant server
recv(to_send, buff2, sizeof(buff2), 0);
cout << "Message recieved back: " << buff2 << endl;
//send msg back to original server
msg = "8";
msg = msg + charPtrToString(buff2);
memset(buff2,0,sizeof(buff2));
cout << fds[i] << endl;
send(fds[i], msg.c_str(), sizeof(msg), 0);
msg.clear();
if (breakLoop == true)
break;
}
busy = false; // communication over, link is open
}
// client -> server (requesting IP of site)
else if (flag1 == '9') {
string route;
string msg = charPtrToString(buff);
msg.erase(0, 2); // removing flag
int pos = msg.find('+');
string requesting_server = msg.substr(0, pos);
msg.erase(0, pos+1);
string domain = msg;
cout << "Requesting server = " << requesting_server << endl;
cout << "Domain requested = " << domain << endl;
string cache_IP = ip(domain);
cout << cache_IP << endl;
// displayVectors();
if (cache_IP.empty()) {
route += requesting_server + " -> S3 (proxy) -> S4 -> ||DNS Server|| -> S4 -> S3 (proxy) -> " + requesting_server;
domain = "2" + domain;
send(6, domain.c_str(), domain.size(), 0); // sending site to s4
char ip[30];
recv(6, ip, sizeof(ip), 0); // recieving ip from s4
string ip_str = charPtrToString(ip);
cout << "IP = " << ip_str << endl;
string ip_to_send = "3+" + route + "+" + ip_str;
send(fds[i], ip_to_send.c_str(), ip_to_send.size(), 0); // sending route + ip back to requesting server
domain.erase(0, 1);
//sites.push_back(domain);
//ips.push_back(ip_str);
updateCache(domain, ip);
}
else {
route = requesting_server + " -> S3 (proxy) -> " + requesting_server + " -> ";
string ip_to_send = "3+" + route + "+" + cache_IP;
send(fds[i], ip_to_send.c_str(), ip_to_send.size(), 0);
}
}
// server requesting routing table
if (flag2 == '5')
{
string rtable = getRoutingTable(s1_str, s2_str, s4_str);
string rt = "0";
rt.append(rtable);
send(fds[i], rt.c_str(), rt.size(), 0);
}
}
}
}
}
}
}
}