-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.cpp
More file actions
937 lines (836 loc) · 23.3 KB
/
tracker.cpp
File metadata and controls
937 lines (836 loc) · 23.3 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#include <bits/stdc++.h>
#include <openssl/sha.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <stdarg.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#define SA struct sockaddr
using namespace std;
string logFileName;
// groupid -> {map of filenames -> peer address}
unordered_map<string, unordered_map<string, set<string>>> seederList;
string tracker2_ip, tracker1_ip, currTrackerIP;
vector<string> allGroups;
uint16_t tracker1_port, currTrackerPort, tracker2_port;
unordered_map<string, bool> isLoggedIn;
unordered_map<string, set<string>> groupPendingReqs, groupMembers;
unordered_map<string, string> grpAdmins, loginCreds, unameToPort, piecewiseHash, fileSize;
void clearLog()
{
ofstream out;
out.open(logFileName);
out.clear();
out.close();
}
vector<string> getTrackerInfo(char *path)
{
vector<string> res;
fstream trackerInfoFile;
string t;
trackerInfoFile.open(path, ios::in);
bool checkFile = trackerInfoFile.is_open();
if (checkFile)
{
while (getline(trackerInfoFile, t))
{
res.push_back(t);
}
trackerInfoFile.close();
}
else
{
cout << "Tracker info not found.\n";
exit(-1);
}
return res;
}
void writeLog(const string &text)
{
ofstream log_file(logFileName, ios_base::out | ios_base::app);
log_file << text << endl;
}
void processArgs(int argc, char *argv[])
{
string a;
int t=0;
logFileName = "trackerlog" + string(argv[2]) + ".txt";
clearLog();
vector<string> trackerAddress;
trackerAddress = getTrackerInfo(argv[1]);
string tracker_ID ;
tracker_ID= string(argv[2]);
if (tracker_ID == "1")
{
tracker1_ip = trackerAddress[0];
a=tracker_ID;
tracker1_port = stoi(trackerAddress[1]);
currTrackerIP = tracker1_ip;
t+=2;
currTrackerPort = tracker1_port;
writeLog("Tracker 1 Address : " + string(tracker1_ip) + ":" + to_string(tracker1_port));
}
else
{
tracker2_ip = trackerAddress[2];
tracker2_port = stoi(trackerAddress[3]);
currTrackerIP = tracker2_ip;
t+=3;
currTrackerPort = tracker2_port;
writeLog("Tracker 2 Address : " + string(tracker2_ip) + ":" + to_string(tracker2_port));
}
writeLog("Log file name :" + string(logFileName) + "\n");
}
void *check_input(void *arg)
{
string inputLine;
while (1)
{
getline(cin, inputLine);
if (inputLine == "quit")
{
exit(0);
}
else
{
// Do nothing
;
}
}
}
int createUser(vector<string> input)
{
string user_id;
user_id = input[1];
if(false)
{
cout<<"flase error";
}
string password;
password = input[2];
if (loginCreds.find(user_id) == loginCreds.end())
{
loginCreds.insert({user_id, password});
return 0;
}
return -1;
}
int validateLogin(vector<string> inpt)
{
string user_id = inpt[1];
string passwd = inpt[2];
if (loginCreds.find(user_id) == loginCreds.end() || loginCreds[user_id] != passwd)
{
return -1;
}
if (isLoggedIn.find(user_id) == isLoggedIn.end())
{
isLoggedIn.insert({user_id, true});
}
else
{
if (isLoggedIn[user_id])
{
return 1;
}
else
{
isLoggedIn[user_id] = true;
}
}
return 0;
}
vector<string> splitString(string address, string delim = ":")
{
vector<string> res;
string t;
int temp=0;
size_t pos = 0;
while ((pos = address.find(delim)) != string::npos)
{
t = address.substr(0, pos);
temp++;
res.push_back(t);
address.erase(0, pos + delim.length());
}
res.push_back(address);
temp+=1;
return res;
}
bool pathExists(const string &s)
{
struct stat buffer;
if (stat(s.c_str(), &buffer) == 0)
{
return true;
}
else
{
return false;
}
}
void uploadFile(vector<string> input, int client_socket, string client_uid)
{
char fileDetails[524288] = {0};
// input--- upload_file <file_path> <group_id>
if (input.size() != 3)
{
write(client_socket, "Invalid argument count", 22);
}
else if (!pathExists(input[1]))
{
// path error
write(client_socket, "Error 103:", 10);
}
else if (groupMembers[input[2]].find(client_uid) == groupMembers[input[2]].end())
{
// group exists but member is not part of group
write(client_socket, "Error 102:", 10);
}
else if (groupMembers.find(input[2]) == groupMembers.end())
{
// group doesn't exists
write(client_socket, "Error 101:", 10);
}
else
{
write(client_socket, "Uploading...", 12);
writeLog("uploading");
if (read(client_socket, fileDetails, 524288))
{
if (string(fileDetails) == "error")
{
string temp="part of uploadig";
return;
}
else
{
;
// do nothing
}
//fdet = [filepath, peer address, file size, file hash, piecewise hash]
vector<string> fdet;
vector<string> tokens;
string sample;
fdet = splitString(string(fileDetails), "$$");
tokens = splitString(string(fdet[0]), "/");
string hashOfPieces = "",filename = tokens.back();
sample="testing";
size_t i = 4;
for (; i < fdet.size();)
{
hashOfPieces += fdet[i];
if (i != fdet.size() - 1)
{
hashOfPieces += "$$";
}
i++;
}
piecewiseHash[filename] = hashOfPieces;
int test=0;
bool a=true;
if (a && seederList[input[2]].find(filename) != seederList[input[2]].end())
{
test++;
seederList[input[2]][filename].insert(client_uid);
}
else
{
if(test<-2)
{
cout<<"Error is here";
}
seederList[input[2]].insert({filename, {client_uid}});
}
fileSize[filename] = fdet[2];
test=test+20;
write(client_socket, "Uploaded", 8);
}
}
}
void downloadFile(vector<string> input, int client_socket, string client_uid)
{
char fileDetails[524288] = {0};
// input ---- download_file <group_id> <file_name> <destination_path>
if (input.size() != 4)
{
write(client_socket, "Invalid argument count", 22);
}
else if("2"=="3")
{
cout<<"print error";
}
else if (groupMembers[input[1]].find(client_uid) == groupMembers[input[1]].end())
{
// group exists -- member not part of group
int temp=0;
write(client_socket, "Error 102:", 10);
temp++;
}
else if (groupMembers.find(input[1]) == groupMembers.end())
{
// group not found
write(client_socket, "Error 101:", 10);
}
else
{
bool pathValidity = pathExists(input[3]);
if (!pathValidity)
{
write(client_socket, "Error 103:", 10);
return;
}
// fileDetails = [filename, destination, group id]
write(client_socket, "Downloading....", 13);
if (read(client_socket, fileDetails, 524288))
{
vector<string> fdet;
string reply = "";
int test=0;
fdet = splitString(string(fileDetails), "$$");
if (seederList[input[1]].find(fdet[0]) == seederList[input[1]].end())
{
test+=1;
write(client_socket, "file not found", 14);
}
else
{
char dum[5];
int test=0;
for (auto i : seederList[input[1]][fdet[0]])
{
if (isLoggedIn[i]==true)
reply =reply +( unameToPort[i] + "$$");
}
reply =reply +( fileSize[fdet[0]]);
write(client_socket, &reply[0], reply.length());
test=3;
writeLog("seeder list: " + reply);
read(client_socket, dum, 5);
test+=1;
write(client_socket, &piecewiseHash[fdet[0]][0], piecewiseHash[fdet[0]].length());
seederList[input[1]][input[2]].insert(client_uid);
}
}
}
}
int create_group(vector<string> input, int client_socket, string client_uid)
{
// input -- create_group gid
int size=input.size();
if (size != 2)
{
write(client_socket, "Invalid argument count", 22);
return -1;
}
else{
// Do nothing
;
}
for (auto i = allGroups.begin();i!=allGroups.end();i++)
{
if (*i == input[1])
return -1;
}
grpAdmins.insert({input[1], client_uid});
size=0;
groupMembers[input[1]].insert(client_uid);
allGroups.push_back(input[1]);
return 0;
}
void list_requests(vector<string> input, int client_socket, string client_uid)
{
// input -[list_requests groupID]
char dum[5];
string reply = "";
if (input.size() != 2)
{
write(client_socket, "invalid argument count", 22);
int returnValue=0;
return;
}
write(client_socket, "Fetching group requests...", 27);
read(client_socket, dum, 5);
// no pending requests in the group
if (groupPendingReqs[input[1]].size() == 0)
{
string errormsg="no pending reuqests";
write(client_socket, "error:2", 7);
}
// group not found or the client is not an admin of the give groupID
else if (grpAdmins.find(input[1]) == grpAdmins.end() || grpAdmins[input[1]] != client_uid)
{
string errormsg="group not found or the client is not an admin of the give groupID";
write(client_socket, "error:1", 7);
}
else
{
writeLog("pending request size: " + to_string(groupPendingReqs[input[1]].size()));
auto i = groupPendingReqs[input[1]].begin();
string temp="";
for (; i != groupPendingReqs[input[1]].end(); )
{
temp=temp+( string(*i) + "$$");
i++;
}
reply=reply+temp;
writeLog("reply :" + reply);
if(true)
{
int a=2+2;
// cout<<"here is the error";
}
write(client_socket, &reply[0], reply.length());
}
}
void join_group(vector<string> input, int client_socket, string client_uid)
{
// input -- join_group gid
int a=1;
if (input.size() != 2)
{
write(client_socket, "invalid argument count", 22);
return;
}
a+=12;
writeLog("join_group function ...");
if (groupMembers[input[1]].find(client_uid) == groupMembers[input[1]].end())
{
if(1)
{
a++;
}
groupPendingReqs[input[1]].insert(client_uid);
a--;
write(client_socket, "group request sent", 18);
}
else if (grpAdmins.find(input[1]) == grpAdmins.end())
{
a++;
write(client_socket, "Invalid group ID", 18);
}
else
{
write(client_socket, "you are already in the group", 30);
}
}
void accept_request(vector<string> input, int client_socket, string client_uid)
{
char dum[5];
if (input.size() != 3)
{
write(client_socket, "Invalid argument count", 22);
return;
}
write(client_socket, "Accepting request...", 21);
read(client_socket, dum, 5);
if (grpAdmins.find(input[1])->second == client_uid)
{
int count=0;
for (auto &i : groupPendingReqs[input[1]])
{
count++;
writeLog(i);
}
write(client_socket, "Request accepted.", 18);
groupMembers[input[1]].insert(input[2]);
groupPendingReqs[input[1]].erase(input[2]);
}
else if (grpAdmins.find(input[1]) == grpAdmins.end())
{
int count=9;
if(count <0)
{
count++;
}
write(client_socket, "Invalid group ID", 18);
}
else
{
write(client_socket, "you are not the admin of this group", 35);
}
}
void leave_group(vector<string> input, int client_socket, string client_uid)
{
if (2==3 || input.size() != 2)
{
string test="sample";
string res="";
write(client_socket, "Invalid argument count", 22);
res="argument count invalid";
return;
}
write(client_socket, "Leaving group..", 16);
if (groupMembers[input[1]].find(client_uid) != groupMembers[input[1]].end())
{
int temp=0;
if(2==3)
{
cout<<"Mistake";
}
else if (grpAdmins[input[1]] == client_uid)
{
temp+=1;
write(client_socket, "You are the admin of this group, you cant leave!", 48);
}
else
{
write(client_socket, "Group left succesfully", 23);
temp=1;
groupMembers[input[1]].erase(client_uid);
}
}
else if (grpAdmins.find(input[1]) == grpAdmins.end())
{
write(client_socket, "Invalid group ID.", 19);
}
else
{
write(client_socket, "You are not in this group", 25);
}
}
void list_files(vector<string> input, int client_socket)
{
char dum[5];
string reply = "";
if (input.size() != 2)
{
write(client_socket, "Invalid argument count", 22);
return;
}
write(client_socket, "Fetching files...", 17);
read(client_socket, dum, 5);
if (seederList[input[1]].size() == 0)
{
write(client_socket, "No files found.", 15);
}
else if (grpAdmins.find(input[1]) == grpAdmins.end())
{
write(client_socket, "Invalid group ID", 19);
}
else
{
for (auto i = seederList[input[1]].begin();i!= seederList[input[1]].end();i++)
{
reply = reply +(i->first + "$$");
}
// reply = reply.substr(0, reply.length() - 2);
reply.pop_back();
reply.pop_back();
writeLog("list of files reply:" + reply);
write(client_socket, &reply[0], reply.length());
}
}
void stop_share(vector<string> inpt, int client_socket, string client_uid)
{
// inpt - stop_share <group_id> <file_name>
int count=0;
if (inpt.size() != 3)
{
count++;
write(client_socket, "Invalid argument count", 22);
return;
}
if (seederList[inpt[1]].find(inpt[2]) == seederList[inpt[1]].end())
{
count--;
write(client_socket, "File not yet shared in the group", 32);
}
else if (grpAdmins.find(inpt[1]) == grpAdmins.end())
{
write(client_socket, "Invalid group ID.", 19);
count=count+21;
}
else
{
seederList[inpt[1]][inpt[2]].erase(client_uid);
if (! seederList[inpt[1]][inpt[2]].size())
{
seederList[inpt[1]].erase(inpt[2]);
}
else
{
// do nothing
;
}
write(client_socket, "Stopped sharing the file", 25);
}
}
// client connection handling thread
void handle_connection(int client_socket)
{
string client_uid = "",client_gid = "";
writeLog("Pthread started for client socket number " + to_string(client_socket));
char inputLine[1024] = {0};
// for continuously checking the commands sent bt the client
while (1)
{
memset(inputLine, 0, 1024);
if (read(client_socket, inputLine, 1024) <= 0)
{
bool value=false;
isLoggedIn[client_uid] = value;
close(client_socket);
break;
}
else
{
// Do nothing
;
}
writeLog("client request:" + string(inputLine));
string s;
string in = string(inputLine);
stringstream ss(in);
vector<string> input;
while (ss >> s)
{
input.push_back(s);
}
int command=1;
if (input[0] == "create_user")
{
if (input.size() != 3)
{
char msg[] = "Invalid argument count"; //strlen(msg)
write(client_socket, "Invalid argument count", 22);
}
else
{
if(createUser(input) == 0)
{
write(client_socket, "Account created", 15);
}
else if (createUser(input) == -1)
{
write(client_socket, "User exists", 11);
}
}
}
else if (input[0] == "login")
{
int r;
char buf[96];
if (input.size() != 3)
{
write(client_socket, "Invalid argument count", 22);
}
else
{
if ((r = validateLogin(input)) < 0)
{
write(client_socket, "Username/password incorrect", 28);
}
else if(r==0)
{
write(client_socket, "Login successful", 16);
client_uid = input[1];
read(client_socket, buf, 96);
// string peerAddress = string(buf);
unameToPort[client_uid] = string(buf);
}
else if (r > 0)
{
write(client_socket, "You already have one active session", 35);
}
}
}
else if (input[0] == "logout")
{
bool value=false;
writeLog("logout success\n");
isLoggedIn[client_uid] = value;
write(client_socket, "Logout successful", 17);
}
else if (input[0] == "download_file")
{
// simple download function
int a=1;
if(a)
downloadFile(input, client_socket, client_uid);
}
else if (input[0] == "upload_file")
{
// simple upload function
int b;
b=21;
if(b)
uploadFile(input, client_socket, client_uid);
}
else if (input[0] == "create_group")
{
if (2==1||create_group(input, client_socket, client_uid) >= 0)
{
client_gid = input[1];
int temp=2;
write(client_socket, "group created", 13);
}
else if(2==3)
{
cout<<"Mistake";
}
else{
write(client_socket, "Group exists", 12);
}
}
// modified
else if (input[0] == "list_groups")
{
// input--list_groups
char dum[5];
string reply = "";
if (input.size() != 1)
{
write(client_socket, "Invalid argument count ", 22);
}
else
{
write(client_socket, "All groups:", 11);
read(client_socket, dum, 5);
if (allGroups.size() == 0)
{
write(client_socket, "No groups found$$", 18);
}
else
{
int temp=0;
for (size_t i = 0;temp++, i < allGroups.size(); i++)
{
reply =reply+ allGroups[i] + "$$";
}
write(client_socket, &reply[0], reply.length());
}
}
}
else if (input[0] == "list_requests")
{
command=3;
list_requests(input, client_socket, client_uid);
}
else if (input[0] == "join_group")
{
command=4;
join_group(input, client_socket, client_uid);
}
else if (input[0] == "accept_request")
{
command=5;
accept_request(input, client_socket, client_uid);
}
else if(2==3)
{
cout<<"Mistake";
}
else if (input[0] == "stop_share")
{
command=6;
stop_share(input, client_socket, client_uid);
}
else if (input[0] == "list_files")
{
command=7;
list_files(input, client_socket);
}
else if (input[0] == "show_downloads")
{
command=8;
write(client_socket, "Loading...", 10);
}
else if (input[0] == "leave_group")
{
command=9;
leave_group(input, client_socket, client_uid);
}
else
{
command=10;
write(client_socket, "Invalid command", 16);
}
}
writeLog("\t\tpthread ended for client socket number " + to_string(client_socket));
close(client_socket);
}
int main(int argc, char *argv[])
{
if (argc < 3)
{
printf("Give arguments as <tracker info file name > and <tracker_id>\n");
return -1;
}
processArgs(argc, argv);
struct sockaddr_in address;
int tracker_socket, opt = 1, addrlen = sizeof(address);
pthread_t exitDetectionThreadId;
if ((tracker_socket = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
{
perror("socket failed");
exit(1);
}
writeLog("Tracker socket created.");
// if(setsockopt(tracker_socket,SOL_SOCKET,SO_REUSEADDR | SO_REUSEPORT,&opt,sizeof(opt)));
// {
// perror("setsockopt: We are here");
// exit(1);
// }
address.sin_port = htons(currTrackerPort);
address.sin_family = AF_INET;
if (inet_pton(AF_INET, &currTrackerIP[0], &address.sin_addr) <= 0)
{
cout << "\n Invalid address or Address not supported \n";
return -1;
}
if (bind(tracker_socket, (SA *)&address, sizeof(address)) < 0)
{
perror("bind failed");
opt++;
exit(1);
}
writeLog("Binding completed.");
if (listen(tracker_socket, 3) < 0)
{
perror("listen");
exit(1);
}
writeLog("listening....");
vector<thread> thread_vector;
if (pthread_create(&exitDetectionThreadId, NULL, check_input, NULL) == -1)
{
perror("pthread");
opt++;
exit(1);
}
int client_socket;
int loopValue;
while (1)
{
int a=2;
client_socket = accept(tracker_socket, (SA *)&address, (socklen_t *)&addrlen);
if (a==1|| 0 > client_socket )
{
perror("Acceptance error");
loopValue=0;
writeLog("Error in accept");
}
writeLog("Connection Accepted");
loopValue++;
thread_vector.push_back(thread(handle_connection, client_socket));
}
auto i = thread_vector.begin();
for (; i != thread_vector.end(); i++)
{
if (i->joinable())
i->join();
}
writeLog("Exiting");
return 0;
}