-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.cpp
602 lines (569 loc) · 16.2 KB
/
peer.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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include <dirent.h>
#include <math.h>
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sstream>
#include <openssl/sha.h>
#include "Definations.h"
using namespace std;
string command_string = "";
string fileUploadPath = "";
string fileUploadPathGroup = "";
string fileDownloadPath = "";
string fileDownloadName = "";
map<int, file_data_base> files_downloaded;
map<int, string> fileBitVectors; //cointains fileID and bit vector
map<int, vector<peer>> currentSeederList; //cointains fileID and information on peer
bool flag_if_login = false;
bool LOGIN_ID = "";
bool flag_peer_seeder = false;
int listenSocket;
int trackerSocket;
const int MAX_CONNECTIONS = 5;
struct sockaddr_in trackerAddress, peerAddress;
void share_piece(string ip, int port, int acc, string filePath, int startPiece, int endPiece)
{
ifstream fp(filePath, ios::in | ios::binary);
struct stat sb;
unsigned long long total_size = stat(filePath.c_str(), &sb);
total_size = (total_size == 0 ? sb.st_size : 0ll);
int chunks = (int)::ceil(total_size / (PIECE_SIZE));
//No of 512 KB chunks in file are chunks
//Total file size in (bytes) is total_size
size_t CHUNK_SIZE = PIECE_SIZE;
if (total_size < CHUNK_SIZE)
CHUNK_SIZE = total_size;
if (startPiece != 0)
total_size = total_size - startPiece * PIECE_SIZE;
char *buff = new char[CHUNK_SIZE];
int rer = 0;
fp.seekg((startPiece)*PIECE_SIZE, ios::beg);
while (fp.tellg() <= endPiece * PIECE_SIZE && fp.read(buff, CHUNK_SIZE))
{
total_size -= CHUNK_SIZE;
int z = send(acc, buff, CHUNK_SIZE, 0);
//Sending z bytes through the socket
rer += z;
if (total_size == 0)
break;
if (total_size < CHUNK_SIZE)
CHUNK_SIZE = total_size;
}
cout << fp.tellg() << endl;
fp.close();
cout << "Sent " << rer << " bytes to " << ip << ":" << port << endl;
int sta = close(acc);
if (sta == 0)
cout << "Connection closed with " << ip << ":" << port << endl;
}
void recieve_piece(int seedSocket, string filePath, int fileid, int pieceLocation)
{
ofstream fp(filePath, ios::out | ios::binary | ios::app);
fp.seekp(PIECE_SIZE * pieceLocation, ios::beg);
int i = 0;
int n, currPiece = pieceLocation;
string prevBitVec = fileBitVectors[fileid];
do
{
char *buff = new char[PIECE_SIZE];
memset(buff, 0, PIECE_SIZE);
n = read(seedSocket, buff, PIECE_SIZE);
fp.write(buff, n);
i += n;
if (n != 0 && currPiece < prevBitVec.length())
prevBitVec[currPiece++] = '1';
// Wrote n bytes
} while (n > 0);
cout << "Wrote " << i << " bytes\n";
fileBitVectors[fileid] = prevBitVec;
fp.close();
}
//when you are calling checkforconnections while downlaoding the format should be
// " d <FILEID> <PIECE RANGE START> <PIECE RANGE END> "
void checkForConnections()
{
listenSocket = socket(AF_INET, SOCK_STREAM, 0);
int reuseAddress = 1;
int listenSocketOptions = setsockopt(listenSocket, SOL_SOCKET, SO_REUSEADDR, &reuseAddress, sizeof(reuseAddress));
setsockopt(listenSocket, SOL_SOCKET, SO_REUSEPORT, &reuseAddress, sizeof(reuseAddress));
if (listenSocket < 0 || listenSocketOptions < 0)
{
cout << "Socket creation error \n";
return;
}
int bindStatus = ::bind(listenSocket, (struct sockaddr *)&peerAddress, sizeof(struct sockaddr_in));
if (bindStatus < 0)
{
cout << ("Bind Failed \n");
return;
}
if (listen(listenSocket, 3) < 0)
{
cout << ("Listen Failed \n");
return;
}
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(peerAddress.sin_addr), ip, INET_ADDRSTRLEN);
int port = ntohs(peerAddress.sin_port);
printf("Listen started on %s:%d\n", ip, port);
socklen_t addr_size = sizeof(struct sockaddr_in);
while (flag_peer_seeder)
{
memset(ip, 0, INET_ADDRSTRLEN);
struct sockaddr_in clientAddress;
int acc = accept(listenSocket, (struct sockaddr *)&clientAddress, &addr_size);
inet_ntop(AF_INET, &(clientAddress.sin_addr), ip, INET_ADDRSTRLEN);
port = ntohs(clientAddress.sin_port);
string fullAddress = string(ip) + ":" + to_string(port);
printf("connection established with peer IP : %s and PORT : %d\n", ip, port);
char tmp[4096] = {0};
recv(acc, tmp, sizeof(tmp), 0);
string req = string(tmp);
if (req[0] == 'd')
{
stringstream x(req);
string t;
vector<string> argsFromPeer;
while (getline(x, t, ' '))
argsFromPeer.push_back(t);
printf("Peer %s:%d requested for %s with piece range from %s-%s\n", ip, port, files_downloaded[stoi(argsFromPeer[1])].path.c_str(), argsFromPeer[2].c_str(), argsFromPeer[3].c_str());
thread sendDataToPeer(share_piece, string(ip), port, acc, files_downloaded[stoi(argsFromPeer[1])].path, stoi(argsFromPeer[2]), stoi(argsFromPeer[3]));
sendDataToPeer.detach();
}
else
cout << "Invalid command\n";
}
cout << "Listen stopped\n";
}
bool getFrequency(string x)
{
int no1s = 0;
for (int i = 0; i < x.length(); i++)
{
if (x[i] == '1')
no1s++;
}
if (no1s == x.length())
return false;
return true;
}
void dowload_start(int fileid, string fileName, string filePath)
{
vector<peer> peerlist = currentSeederList[fileid];
int reuseAddress = 1;
int maxConns = (MAX_CONNECTIONS < peerlist.size() ? MAX_CONNECTIONS : peerlist.size());
ifstream fp(fileName, ios::in | ios::binary);
struct stat sb;
unsigned long long total_size = stat(fileName.c_str(), &sb);
total_size = (total_size == 0 ? sb.st_size : 0ll);
int chunks = (int)::ceil(total_size / (PIECE_SIZE)) + 1;
fp.close();
cout << "CHUNKS " << chunks << endl;
size_t CHUNK_SIZE = PIECE_SIZE;
if (total_size < CHUNK_SIZE)
CHUNK_SIZE = total_size;
int startPiece = 0, endPiece = chunks / maxConns;
string bv(chunks, '0');
fileBitVectors[fileid] = bv;
ofstream output(filePath, ios::out | ios::binary | ios::app);
for (int i = 0; i < maxConns; i++)
{
struct sockaddr_in seedAddress;
seedAddress.sin_family = AF_INET;
seedAddress.sin_port = htons(peerlist[i].port);
seedAddress.sin_addr.s_addr = inet_addr(peerlist[i].ip.c_str());
int seedSocket = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(seedSocket, SOL_SOCKET, SO_REUSEADDR, &reuseAddress, sizeof(reuseAddress));
setsockopt(seedSocket, SOL_SOCKET, SO_REUSEPORT, &reuseAddress, sizeof(reuseAddress));
if (seedSocket < 0)
{
cout << "Unable to download. Socket creation error \n";
return;
}
int trackerConnectStatus = connect(seedSocket, (struct sockaddr *)&seedAddress, sizeof(seedAddress));
if (trackerConnectStatus < 0)
{
cout << "Tracker connection Failed with seeder IP " << seedAddress.sin_addr.s_addr << " and port " << seedAddress.sin_port << "\n";
return;
}
//d <FILEID> <PIECE RANGE START> <PIECE RANGE END>
string sss = "d " + to_string(fileid) + " " + to_string(startPiece) + " " + to_string(endPiece);
cout << "Send DL request to seed " << peerlist[i].ip << ":" << peerlist[i].port << " for file ID " << fileid << endl;
send(seedSocket, sss.c_str(), sss.length(), 0);
thread writeToFile(recieve_piece, seedSocket, filePath, fileid, startPiece);
writeToFile.join();
startPiece = endPiece + 1;
endPiece += (chunks / maxConns);
if (startPiece >= chunks)
i = maxConns;
if (endPiece > chunks)
endPiece = chunks;
if (i == maxConns - 2)
endPiece = chunks;
}
int y = 1;
while (getFrequency(fileBitVectors[fileid]))
;
cout << "Downloading of file " + fileName + " completed\n";
string ss = "o " + to_string(fileid);
char *buffer = new char[4096];
memset(buffer, 0, 4096);
send(trackerSocket, ss.c_str(), ss.length(), 0);
read(trackerSocket, buffer, 4096);
cout << string(buffer) << endl;
int totPiece = 0;
ifstream ifs(fileName, ios::binary);
string totalHash = "";
char *piece = new char[PIECE_SIZE], hash[20];
while (ifs.read((char *)piece, PIECE_SIZE) || ifs.gcount())
{
//SHA1(piece, strlen((char *)piece), hash);
totalHash += string((char *)hash);
totPiece++;
memset(piece, 0, PIECE_SIZE);
}
file_data_base f(fileid, fileName, fileName, fileUploadPathGroup, totPiece, totalHash, set<peer>());
files_downloaded[fileid] = f;
if (!flag_peer_seeder)
{
flag_peer_seeder = true;
thread startListenOnPeer(checkForConnections);
startListenOnPeer.detach();
}
}
int extractCommand()
{
string s, t;
getline(cin, s);
if (s == "" || s == "\n")
return 0;
stringstream x(s);
vector<string> cmds;
while (getline(x, t, ' '))
{
cmds.push_back(t);
}
if (cmds[0] == "signup")
{
if (cmds.size() < 3)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "a " + cmds[1] + " " + cmds[2];
}
else if (cmds[0] == "login")
{
if (flag_if_login)
{
cout << "Already logged in.\n";
return 0;
}
if (cmds.size() < 3)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "b " + cmds[1] + " " + cmds[2];
return 10;
}
else if (cmds[0] == "create_group")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 2)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "c " + cmds[1];
}
else if (cmds[0] == "join_group")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 2)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "d " + cmds[1];
}
else if (cmds[0] == "leave_group")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 2)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "e " + cmds[1];
}
else if (cmds[0] == "list_request")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 2)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "f " + cmds[1];
}
else if (cmds[0] == "accept_request")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 3)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "g " + cmds[1] + " " + cmds[2];
}
else if (cmds[0] == "list_group")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
command_string = "h";
}
else if (cmds[0] == "list_file")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 2)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "i " + cmds[1];
}
else if (cmds[0] == "upload_file")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 3)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
char path[4096] = {0};
string filePath = "";
getcwd(path, 4096);
if (cmds[1][0] != '~')
{
filePath = (string(path) + (cmds[1][0] == '/' ? "" : "/") + cmds[1]);
}
if (FILE *file = fopen(filePath.c_str(), "r"))
{
fclose(file);
command_string = "j " + filePath + " " + cmds[2];
//cout << command_string << endl;
fileUploadPath = filePath;
fileUploadPathGroup = cmds[2];
return 20;
}
else
{
cout << "File not found\n";
return 0;
}
}
else if (cmds[0] == "download_file")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 4)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "k " + cmds[1] + " " + cmds[2] + " " + cmds[3];
fileUploadPathGroup = cmds[1];
fileDownloadName = cmds[2];
fileDownloadPath = cmds[3];
return 30;
}
else if (cmds[0] == "logout")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first) but quitting anyway\n";
}
command_string = "l";
return 100;
}
else if (cmds[0] == "show_download")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
command_string = "m";
}
else if (cmds[0] == "stop_share")
{
if (!flag_if_login)
{
cout << "mere bhai log in toh kar pehle! (Traslation: Please log in first)\n";
return 0;
}
if (cmds.size() < 3)
{
cout << "Not enough parameters, You probably forgot to pass some parameter \n";
return 0;
}
command_string = "n " + cmds[1] + " " + cmds[2];
}
else
{
cout << "Invalid command\n";
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
if (argc < 4)
{
cout << "Parameters not provided.Exiting...\n";
return -1;
}
int reuseAddress = 1;
ifstream trackInfo(argv[3]);
string ix, px;
trackInfo >> ix >> px;
trackInfo.close();
trackerAddress.sin_family = AF_INET;
trackerAddress.sin_port = htons(stoi(px));
trackerAddress.sin_addr.s_addr = inet_addr(ix.c_str());
peerAddress.sin_family = AF_INET;
peerAddress.sin_port = htons(stoi(argv[2]));
peerAddress.sin_addr.s_addr = inet_addr(argv[1]);
char buffer[4096] = {0};
trackerSocket = socket(AF_INET, SOCK_STREAM, 0);
int listenSocketOptions = setsockopt(trackerSocket, SOL_SOCKET, SO_REUSEADDR, &reuseAddress, sizeof(reuseAddress));
setsockopt(trackerSocket, SOL_SOCKET, SO_REUSEPORT, &reuseAddress, sizeof(reuseAddress));
if (trackerSocket < 0 || listenSocketOptions < 0)
{
cout << "Socket creation error \n";
return -1;
}
int trackerConnectStatus = connect(trackerSocket, (struct sockaddr *)&trackerAddress, sizeof(trackerAddress));
if (trackerConnectStatus < 0)
{
cout << ("Tracker connection Failed \n");
return -1;
}
int valread = read(trackerSocket, buffer, 4096);
cout << string(buffer) << endl;
string syncActual = "sync " + string(argv[1]) + " " + string(argv[2]);
send(trackerSocket, syncActual.c_str(), syncActual.length(), 0);
while (true)
{
memset(buffer, 0, 4096);
int cmdFlag = extractCommand();
if (cmdFlag == 0 || command_string == "")
continue;
send(trackerSocket, command_string.c_str(), (command_string).length(), 0);
command_string = "";
valread = read(trackerSocket, buffer, 4096);
cout << string(buffer) << endl;
if (cmdFlag == 100)
{
flag_peer_seeder = false;
flag_if_login = false;
break;
}
else if (cmdFlag == 10 && string(buffer).substr(0, 3) == "Log")
{
flag_if_login = true;
//flag_peer_seeder = false;
}
else if (cmdFlag == 20 && isdigit(string(buffer)[0]))
{
string fileDetails = string(buffer);
fileDetails = fileDetails.substr(0, fileDetails.find(" "));
int totPiece = 0;
ifstream ifs(fileUploadPath, ios::binary);
string totalHash = "";
char *piece = new char[PIECE_SIZE], hash[20]; //change array to unsigned for SHA1
while (ifs.read((char *)piece, PIECE_SIZE) || ifs.gcount())
{
totalHash += string((char *)hash);
totPiece++;
memset(piece, 0, PIECE_SIZE);
}
file_data_base f(stoi(fileDetails), fileUploadPath, fileUploadPath, fileUploadPathGroup, totPiece, totalHash, set<peer>());
files_downloaded[stoi(fileDetails)] = f;
if (!flag_peer_seeder)
{
flag_peer_seeder = true;
thread startListenOnPeer(checkForConnections);
startListenOnPeer.detach();
}
}
else if (cmdFlag == 30 && isdigit(string(buffer)[0]))
{
string b = string(buffer);
//cout << "ORIG " << b << endl;
stringstream x(b);
string t;
vector<peer> peerList;
int i = 0, fileid = 0;
while (getline(x, t, ' '))
{
if (i == 0)
fileid = stoi(t);
else
{
peer ppp(t.substr(0, t.find(":")), stoi(t.substr(t.find(":") + 1)), "");
peerList.push_back(peer(ppp));
cout << "Added seed " << ppp.ip << ":" << ppp.port << endl;
}
i++;
}
currentSeederList[fileid] = peerList;
//START DOWNLOAD
thread startdl(dowload_start, fileid, fileDownloadName, fileDownloadPath);
startdl.detach();
}
}
return 0;
}