-
Notifications
You must be signed in to change notification settings - Fork 0
/
viralsin_assignment1.cpp
executable file
·931 lines (854 loc) · 31.4 KB
/
viralsin_assignment1.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
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
/**
* @viralsin_assignment1
* @author Viral Sinha <[email protected]>
* @version 1.0
*
* @section LICENSE
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details at
* http://www.gnu.org/copyleft/gpl.html
*
* @section DESCRIPTION
*
* This contains the main function. Add further description here....
*/
#include < iostream > #include < cstdio > #include < stdio.h > #include < string.h > #include "../include/global.h"#include "../include/logger.h"#include < sys / types.h > #include < sys / socket.h > #include < netdb.h > #include < arpa / inet.h > #include < netinet / in .h > #include < unistd.h > #include < sys / time.h > #include < vector > #include < sstream > #include < stdlib.h > #include < algorithm > #include < msgpack.hpp >
using namespace std;
/**
* main function
*
* @param argc Number of arguments
* @param argv The argument list
* @return 0 EXIT_SUCCESS
*/
void printAuthor();
void getIP(char * , int);
void commonPrintFunctionForSuccess(char * ,
const char * );
void commonPrintFunctionForError(char * );
void getPortforServer(char * , char * );
void loginToServer(string, string, string);
void startServer(string);
void receiveAndRelay(string, int, int, int, fd_set);
void unicastMessage(string, string, int);
void broadcastMessage(string, string, int, int, int, fd_set);
void displayListOfBlockedClients(string, int);
string getOriginalMessage(string);
string getSendersIp(string);
string getIpfromSocket(int);
string ltrim(const string);
string rtrim(const string);
string skipFirstWord(string);
string skipTwoWords(string);
int isValidPort(string);
int isValidIp(string);
int sendMessage(string, int);
int checkBlockList(string, string);
int getSocketFdFromIp(int, string);
int getPortFromIpForBlockedList(string, int);
int unblockIp(string, string);
int validateIpAndPort(string, string);
int checkLocalList(string);
int validateIP(string, string);
int validateIForSending(string);
int checkBlockedListInServer(string);
int checkForValidPort(string);
//Reference : https://stackoverflow.com/questions/40896477/using-msgpack-to-encode-a-user-defined-structure
//MSGPACK requires this method MSGAPACK to be defined for it to be considered a MSGPACK type
struct loggedInDetails {
string name;
string ip;
int port;
MSGPACK_DEFINE(name, ip, port);
};
vector < loggedInDetails > getDetailsOfConnectedClients(int, int, fd_set);
struct blockedListProto {
string name;
string ip;
int port;
};
struct bufferProto {
string fromIp;
string toIp;
string message;
};
void checkforMessagesInBuffer(int, int);
vector < bufferProto > bufferList;
//Inserting into structure on the basis of port number
//https://stackoverflow.com/questions/4892680/sorting-a-vector-of-structs
bool comparePorts(loggedInDetails first, loggedInDetails second) {
return first.port < second.port;
}
//Inserting into structure on the basis of port number
//https://stackoverflow.com/questions/4892680/sorting-a-vector-of-structs
bool comparePortsForBlockedListDisplay(blockedListProto first, blockedListProto second) {
return first.port < second.port;
}
vector < blockedListProto > blockListByClient;
vector < loggedInDetails > clientList;
vector < pair < string, string > > blockList;
fd_set globalMasterSet;
char systemIp[INET_ADDRSTRLEN];
int clientLoggedIn = 0;
int main(int argc, char ** argv) {
/*Init. Logger*/
cse4589_init_log(argv[2]);
fclose(fopen(LOGFILE, "w"));
/*Start Here*/
if (strcmp(argv[1], "s") == 0) {
startServer(argv[2]);
} else {
while (true) {
string intr, input;
vector < string > commandTokens;
//Reference:https://en.cppreference.com/w/cpp/string/basic_string/getline
getline(cin, input);
stringstream ss(input);
while (getline(ss, intr, ' ')) {
commandTokens.push_back(intr);
}
if (commandTokens[0] == "AUTHOR")
printAuthor();
if (commandTokens[0] == "EXIT")
break;
if (commandTokens[0] == "IP")
getIP("IP", 1);
if (commandTokens[0] == "PORT") {
commonPrintFunctionForSuccess("PORT", argv[2]);
}
if (commandTokens[0] == "LOGIN") {
int isValid = validateIpAndPort(commandTokens[1], commandTokens[2]);
if (isValid == 1) {
cse4589_print_and_log("[LOGIN:SUCCESS]\n");
cse4589_print_and_log("[LOGIN:END]\n");
loginToServer(commandTokens[1], commandTokens[2], argv[2]);
} else {
cse4589_print_and_log("[LOGIN:ERROR]\n");
cse4589_print_and_log("[LOGIN:END]\n");
}
}
}
}
return 0;
}
void getIP(char * cmd, int print) {
int sockFd;
struct addrinfo hints;
struct addrinfo * addressInfo;
memset( & hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo("8.8.8.8", "3453", & hints, & addressInfo) == -1) {
commonPrintFunctionForError("IP");
}
if ((sockFd = socket(addressInfo - > ai_family, addressInfo - > ai_socktype, addressInfo - > ai_protocol)) == -1) {
commonPrintFunctionForError("IP");
}
if (connect(sockFd, addressInfo - > ai_addr, addressInfo - > ai_addrlen) == -1) {
commonPrintFunctionForError("IP");
}
struct sockaddr_in sa;
memset( & sa, 0, sizeof(sa));
socklen_t len = sizeof(sa);
//Reference : https://support.sas.com/documentation/onlinedoc/sasc/doc700/html/lr2/zockname.htm//
getsockname(sockFd, (struct sockaddr * ) & sa, & len);
memset(systemIp, '\0', sizeof(systemIp));
//beej//
inet_ntop(addressInfo - > ai_family, & sa.sin_addr, systemIp, sizeof(systemIp));
if (print == 1)
commonPrintFunctionForSuccess("IP", systemIp);
}
void printAuthor() {
cse4589_print_and_log("[AUTHOR:SUCCESS]\n");
cse4589_print_and_log("I, viralsin, have read and understood the course academic integrity policy.\n");
cse4589_print_and_log("[AUTHOR:END]\n");
}
void commonPrintFunctionForSuccess(char * command,
const char * output) {
cse4589_print_and_log("[%s:SUCCESS]\n", command);
cse4589_print_and_log("%s:%s\n", command, output);
cse4589_print_and_log("[%s:END]\n", command);
}
void commonPrintFunctionForError(char * command) {
cse4589_print_and_log("[%s:Error]\n", command);
cse4589_print_and_log("[%s:END]\n", command);
}
void loginToServer(string ip, string port, string lport) {
int fdMax;
int sockFd, addressStatus;
fd_set master;
fd_set write_fds;
FD_ZERO( & master);
FD_ZERO( & write_fds);
//adding input fd to the main set - 0 = STDIN
FD_SET(0, & master);
fdMax = 0;
char ipstr[INET_ADDRSTRLEN];
struct addrinfo hints;
struct addrinfo * addressInfo;
struct addrinfo * bindaddressInfo;
memset( & hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(ip.c_str(), port.c_str(), & hints, & addressInfo) == -1) {
cse4589_print_and_log("[LOGIN:ERROR]\n");
cse4589_print_and_log("[LOGIN:END]\n");
}
if ((sockFd = socket(addressInfo - > ai_family, addressInfo - > ai_socktype, addressInfo - > ai_protocol)) == -1)
perror("listener: socket");
FD_SET(sockFd, & master);
if (sockFd > fdMax)
fdMax = sockFd;
getaddrinfo(NULL, lport.c_str(), & hints, & bindaddressInfo);
bind(sockFd, bindaddressInfo - > ai_addr, bindaddressInfo - > ai_addrlen);
if (connect(sockFd, addressInfo - > ai_addr, addressInfo - > ai_addrlen) == -1) {
clientLoggedIn = 0;
} else {
clientLoggedIn = 1;
while (1) {
write_fds = master;
select(fdMax + 1, & write_fds, NULL, NULL, 0);
if (FD_ISSET(0, & write_fds)) {
vector < string > tokens;
string input, tmp, rest, intr;
getline(cin, input);
stringstream ss(input);
int i = 0;
while (getline(ss, intr, ' ')) {
if (i < 1)
tokens.push_back(intr);
i++;
}
if (tokens[0] == "SEND" && clientLoggedIn == 1) {
int isValidSend = validateIForSending(input);
if (isValidSend == 1) {
int retValue = sendMessage(input, sockFd);
if (retValue == -1) {
cse4589_print_and_log("[SEND:ERROR]\n");
cse4589_print_and_log("[SEND:END]\n");
} else {
cse4589_print_and_log("[SEND:SUCCESS]\n");
cse4589_print_and_log("[SEND:END]\n");
}
} else {
cse4589_print_and_log("[SEND:ERROR]\n");
cse4589_print_and_log("[SEND:END]\n");
}
}
if (tokens[0] == "BROADCAST" && clientLoggedIn == 1) {
int retValue = sendMessage(input, sockFd);
if (retValue == -1) {
cse4589_print_and_log("[BROADCAST:ERROR]\n");
cse4589_print_and_log("[BROADCAST:END]\n");
} else {
cse4589_print_and_log("[BROADCAST:SUCCESS]\n");
cse4589_print_and_log("[BROADCAST:END]\n");
}
}
if (tokens[0] == "AUTHOR")
printAuthor();
if (tokens[0] == "IP")
getIP("IP", 1);
if (tokens[0] == "PORT") {
commonPrintFunctionForSuccess("PORT", lport.c_str());
}
if (tokens[0] == "LOGOUT" && clientLoggedIn == 1) {
clientLoggedIn = 0;
string removeSocketString = "REMOVE";
send(sockFd, removeSocketString.c_str(), removeSocketString.length(), 0);
close(sockFd);
cse4589_print_and_log("[LOGOUT:SUCCESS]\n");
cse4589_print_and_log("[LOGOUT:END]\n");
break;
}
if (tokens[0] == "EXIT") {
clientLoggedIn = 0;
exit(0);
}
if (tokens[0] == "LIST") {
for (int i = 0; i < clientList.size(); i++) {
if (i == 0)
cse4589_print_and_log("[LIST:SUCCESS]\n");
cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", i + 1, clientList[i].name.c_str(), clientList[i].ip.c_str(), clientList[i].port);
if (i == clientList.size() - 1)
cse4589_print_and_log("[LIST:END]\n");
}
}
if (tokens[0] == "REFRESH" && clientLoggedIn == 1) {
string inputMsg = "REFRESH";
int retValue = send(sockFd, inputMsg.c_str(), inputMsg.length(), 0);
if (retValue == -1) {
cse4589_print_and_log("[REFRESH:ERROR]\n");
cse4589_print_and_log("[REFRESH:END]\n");
} else {
cse4589_print_and_log("[REFRESH:SUCCESS]\n");
cse4589_print_and_log("[REFRESH:END]\n");
}
}
if (tokens[0] == "BLOCK" && clientLoggedIn == 1) {
if (validateIP(input, "") == 1) {
send(sockFd, input.c_str(), input.length(), 0);
} else {
cse4589_print_and_log("[BLOCK:ERROR]\n");
cse4589_print_and_log("[BLOCK:END]\n");
}
}
if (tokens[0] == "UNBLOCK" && clientLoggedIn == 1) {
if (validateIP(input, "") == 1) {
send(sockFd, input.c_str(), input.length(), 0);
} else {
cse4589_print_and_log("[UNBLOCK:ERROR]\n");
cse4589_print_and_log("[UNBLOCK:END]\n");
}
}
} else {
if (FD_ISSET(sockFd, & write_fds)) {
char buff[5000];
memset(buff, '\0', sizeof(buff));
int recvLen = recv(sockFd, buff, sizeof(buff), 0);
if (recvLen > 0) {
//Reference : http://www.thomaswhitton.com/blog/2013/07/03/binary-message-format-c-plus-plus-examples/
try {
msgpack::object_handle oh = msgpack::unpack(buff, sizeof(buff));
msgpack::object obj = oh.get();
vector < loggedInDetails > rvec;
obj.convert(rvec);
clientList = rvec;
} catch (...) {
buff[recvLen] = '\0';
string tempString(buff);
if (tempString == "BLOCKSUCCESS") {
cse4589_print_and_log("[BLOCK:SUCCESS]\n");
cse4589_print_and_log("[BLOCK:END]\n");
} else {
if (tempString == "BLOCKFAILURE") {
cse4589_print_and_log("[BLOCK:ERROR]\n");
cse4589_print_and_log("[BLOCK:END]\n");
} else {
if (tempString == "UNBLOCKSUCCESS") {
cse4589_print_and_log("[UNBLOCK:SUCCESS]\n");
cse4589_print_and_log("[UNBLOCK:END]\n");
} else {
if (tempString == "UNBLOCKFAILURE") {
cse4589_print_and_log("[UNBLOCK:ERROR]\n");
cse4589_print_and_log("[UNBLOCK:END]\n");
} else {
string originalMessage = getOriginalMessage(tempString);
string sendersIp = getSendersIp(tempString);
getIP("IP", 0);
string ip(systemIp);
cse4589_print_and_log("[RECEIVED:SUCCESS]\n");
cse4589_print_and_log("msg from:%s\n[msg]:%s\n", sendersIp.c_str(), originalMessage.c_str());
cse4589_print_and_log("[RECEIVED:END]\n");
memset(buff, '\0', sizeof(buff));
}
}
}
}
}
}
}
}
}
}
}
void startServer(string serverPort) {
fd_set master;
fd_set read_fds;
fd_set write_fds;
int sockFd {}, fdMax {}, newSocketFd, addressStatus;
FD_ZERO( & globalMasterSet);
FD_ZERO( & master);
FD_ZERO( & read_fds);
FD_ZERO( & write_fds);
FD_SET(0, & master);
fdMax = 0;
char ipstr[INET_ADDRSTRLEN];
struct addrinfo hints;
struct addrinfo * addressInfo;
memset( & hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, serverPort.c_str(), & hints, & addressInfo);
if ((sockFd = socket(addressInfo - > ai_family, addressInfo - > ai_socktype, addressInfo - > ai_protocol)) == -1)
perror("listener: socket");
bind(sockFd, addressInfo - > ai_addr, addressInfo - > ai_addrlen);
FD_SET(sockFd, & master);
if (sockFd > fdMax)
fdMax = sockFd;
listen(sockFd, 4);
while (1) {
read_fds = master;
select(fdMax + 1, & read_fds, NULL, NULL, NULL);
for (int i = 0; i <= fdMax; i++) {
if (FD_ISSET(i, & read_fds)) {
if (i == sockFd) {
socklen_t namelen = sizeof(addressInfo);
newSocketFd = accept(sockFd, (struct sockaddr * ) & addressInfo, & namelen);
if (newSocketFd == -1)
perror("accept");
FD_SET(newSocketFd, & master);
if (newSocketFd > fdMax)
fdMax = newSocketFd;
globalMasterSet = master;
clientList = getDetailsOfConnectedClients(fdMax, sockFd, globalMasterSet);
//Reference : http://www.thomaswhitton.com/blog/2013/07/03/binary-message-format-c-plus-plus-examples/
msgpack::sbuffer sbuf;
msgpack::packer < msgpack::sbuffer > pk( & sbuf);
pk.pack(clientList);
send(newSocketFd, sbuf.data(), sbuf.size(), 0);
checkforMessagesInBuffer(newSocketFd, fdMax);
} else {
if (i == 0) {
string cmd;
vector < string > tokens;
string intr;
getline(cin, cmd);
stringstream ss(cmd);
int i = 0;
while (getline(ss, intr, ' ')) {
if (i < 1)
tokens.push_back(intr);
i++;
}
if (tokens[0] == "IP") {
getIP("IP", 1);
}
if (tokens[0] == "AUTHOR")
printAuthor();
if (tokens[0] == "PORT")
commonPrintFunctionForSuccess("PORT", serverPort.c_str());
if (tokens[0] == "LIST") {
for (int i = 0; i < clientList.size(); i++) {
if (i == 0)
cse4589_print_and_log("[LIST:SUCCESS]\n");
cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", i + 1, clientList[i].name.c_str(), clientList[i].ip.c_str(), clientList[i].port);
if (i == clientList.size() - 1)
cse4589_print_and_log("[LIST:END]\n");
}
}
if (tokens[0] == "BLOCKED") {
string targetIp = rtrim(ltrim(skipFirstWord(cmd)));
if (checkBlockedListInServer(targetIp) == 1)
displayListOfBlockedClients(targetIp, fdMax);
else {
cse4589_print_and_log("[BLOCKED:ERROR]\n");
cse4589_print_and_log("[BLOCKED:END]\n");
}
}
} else {
char buff[1000];
memset(buff, '\0', sizeof(buff));
int lenOfData = recv(i, buff, sizeof(buff), 0);
if (lenOfData <= 0) {
perror("recv");
} else {
buff[lenOfData] = '\0';
string stringMessage(buff);
if (stringMessage == "REFRESH") {
clientList = getDetailsOfConnectedClients(fdMax, sockFd, globalMasterSet);
msgpack::sbuffer sbuf;
msgpack::packer < msgpack::sbuffer > pk( & sbuf);
pk.pack(clientList);
send(i, sbuf.data(), sbuf.size(), 0);
} else {
if (stringMessage == "REMOVE") {
FD_CLR(i, & globalMasterSet);
//FD_CLR(i,&master);
clientList = getDetailsOfConnectedClients(fdMax, sockFd, globalMasterSet);
} else {
string input = stringMessage;
string firstWord = input.substr(0, input.find(" "));
if (firstWord == "BLOCK") {
string iptoblock = skipFirstWord(stringMessage);
if (checkBlockList(getIpfromSocket(i), rtrim(ltrim(iptoblock))) == 0) {
blockList.push_back(make_pair(getIpfromSocket(i), rtrim(ltrim(iptoblock))));
string blockSuccess = "BLOCKSUCCESS";
send(i, blockSuccess.c_str(), blockSuccess.length(), 0);
} else {
string blockFailure = "BLOCKFAILURE";
send(i, blockFailure.c_str(), blockFailure.length(), 0);
}
} else {
if (firstWord == "UNBLOCK") {
string iptounblock = skipFirstWord(stringMessage);
int unblockSuccess = unblockIp(getIpfromSocket(i), rtrim(ltrim(iptounblock)));
if (unblockSuccess == 1) {
string unblockSuccessString = "UNBLOCKSUCCESS";
send(i, unblockSuccessString.c_str(), unblockSuccessString.length(), 0);
} else {
string unblockFailureString = "UNBLOCKFAILURE";
send(i, unblockFailureString.c_str(), unblockFailureString.length(), 0);
}
} else {
receiveAndRelay(stringMessage, i, sockFd, fdMax, master);
memset(buff, '\0', sizeof(buff));
}
}
}
}
}
}
}
}
}
}
}
int sendMessage(string message, int socket) {
getIP("IP", 0);
string iP(systemIp);
message = message + "^" + systemIp;
if (send(socket, message.c_str(), message.length(), 0) == -1) {
return -1;
} else {
return 0;
}
}
void receiveAndRelay(string message, int listeningSocket, int connectionSocket, int maximumSocket, fd_set masterlist) {
stringstream check1(message);
string intr;
while (getline(check1, intr, ' ')) {
if (intr == "SEND") {
string originalMessage = skipTwoWords(message);
string recpIp;
stringstream ssIp(message);
int i = 0;
while (getline(ssIp, intr, ' ')) {
if (i == 1) {
recpIp = intr;
break;
}
i++;
}
unicastMessage(originalMessage, recpIp, maximumSocket);
break;
}
if (intr == "BROADCAST") {
string originalMessage = skipFirstWord(message);
broadcastMessage(originalMessage, "255.255.255.255", listeningSocket, connectionSocket, maximumSocket, masterlist);
break;
}
}
}
void broadcastMessage(string message, string ipRecp, int listeningSocket, int connectionSocket, int maximumSocket, fd_set master) {
int i = 0;
for (int j = 0; j <= maximumSocket; j++) {
if (FD_ISSET(j, & master)) {
if (j != listeningSocket && j != connectionSocket && j != 0) {
string sendersIp = getSendersIp(message);
if ((checkBlockList(getIpfromSocket(j), sendersIp)) == 0) {
if (send(j, message.c_str(), message.length(), 0) == -1) {
commonPrintFunctionForError("BROADCAST");
} else {
if (i == 0) {
string tempString = getOriginalMessage(message);
cse4589_print_and_log("[RELAYED:SUCCESS]\n");
cse4589_print_and_log("msg from:%s, to:255.255.255.255\n[msg]:%s\n", sendersIp.c_str(), tempString.c_str());
cse4589_print_and_log("[RELAYED:END]\n");
i++;
}
}
}
}
}
}
}
void unicastMessage(string message, string recpIp, int maximumSocket) {
getIP("IP", 0);
string ip(systemIp);
string originalMessage = rtrim(ltrim(message));
string recpiIp = rtrim(ltrim(recpIp));
for (int j = 0; j <= maximumSocket; j++) {
struct sockaddr_in * connectedIp;
socklen_t len = sizeof(connectedIp);
if ((getpeername(j, (struct sockaddr * ) & connectedIp, & len) != -1)) {
char ipBuf[INET_ADDRSTRLEN];
struct sockaddr_in * ipv4 = (struct sockaddr_in * ) & connectedIp;
void * addr = & ipv4 - > sin_addr;
inet_ntop(AF_INET, addr, ipBuf, sizeof(ipBuf));
string receviersip(ipBuf);
if (receviersip.compare(recpIp) == 0) {
string last_element = getSendersIp(originalMessage);
int isRecvIpBlocked = checkBlockList(receviersip, last_element);
if (isRecvIpBlocked == 0) {
if (FD_ISSET(j, & globalMasterSet)) {
if (send(j, originalMessage.c_str(), originalMessage.length(), 0) != -1) {
string tempString = getOriginalMessage(originalMessage);
cse4589_print_and_log("[RELAYED:SUCCESS]\n");
cse4589_print_and_log("msg from:%s, to:%s\n[msg]:%s\n", last_element.c_str(), receviersip.c_str(), tempString.c_str());
cse4589_print_and_log("[RELAYED:END]\n");
break;
}
} else {
bufferProto obj;
obj.fromIp = last_element;
obj.toIp = receviersip;
obj.message = originalMessage;
bufferList.push_back(obj);
}
} else {
break;
}
}
}
}
}
void displayListOfBlockedClients(string targetIp, int maximumSocket) {
blockListByClient.clear();
for (int i = 0; i < blockList.size(); i++) {
if (blockList[i].first == targetIp) {
blockedListProto b;
struct in_addr ipv4addr;
memset( & ipv4addr, 0, sizeof(ipv4addr));
inet_pton(AF_INET, blockList[i].second.c_str(), & ipv4addr);
struct hostent * he;
he = gethostbyaddr( & ipv4addr, sizeof ipv4addr, AF_INET);
b.name = he - > h_name;
b.ip = blockList[i].second;
b.port = getPortFromIpForBlockedList(blockList[i].second, maximumSocket);
blockListByClient.push_back(b);
}
}
sort(blockListByClient.begin(), blockListByClient.end(), comparePortsForBlockedListDisplay);
for (int i = 0; i < blockListByClient.size(); i++) {
if (i == 0)
cse4589_print_and_log("[BLOCKED:SUCCESS]\n");
cse4589_print_and_log("%-5d%-35s%-20s%-8d\n", i + 1, blockListByClient[i].name.c_str(), blockListByClient[i].ip.c_str(), blockListByClient[i].port);
if (i == blockListByClient.size() - 1)
cse4589_print_and_log("[BLOCKED:END]\n");
}
}
int getPortFromIpForBlockedList(string ipForPort, int maximumSocket) {
int port {};
for (int j = 0; j <= maximumSocket; j++) {
if (FD_ISSET(j, & globalMasterSet)) {
struct sockaddr_in * connectedIp;
socklen_t len = sizeof(connectedIp);
if ((getpeername(j, (struct sockaddr * ) & connectedIp, & len) != -1)) {
char ipBuf[INET_ADDRSTRLEN];
struct sockaddr_in * ipv4 = (struct sockaddr_in * ) & connectedIp;
void * addr = & ipv4 - > sin_addr;
inet_ntop(AF_INET, addr, ipBuf, sizeof(ipBuf));
string receviersip(ipBuf);
if (ipBuf == ipForPort) {
port = ntohs(ipv4 - > sin_port);
}
}
}
}
return port;
}
vector < loggedInDetails > getDetailsOfConnectedClients(int maximumSocket, int serverSocket, fd_set globalmaster) {
clientList.clear();
vector < loggedInDetails > loggedinvector;
for (int j = 0; j <= maximumSocket; j++) {
if (FD_ISSET(j, & globalmaster)) {
struct sockaddr_in * connectedIp;
socklen_t len = sizeof(connectedIp);
if ((getpeername(j, (struct sockaddr * ) & connectedIp, & len) != -1) && j != serverSocket) {
char ipBuf[INET_ADDRSTRLEN];
struct sockaddr_in * ipv4 = (struct sockaddr_in * ) & connectedIp;
void * addr = & ipv4 - > sin_addr;
int port = ntohs(ipv4 - > sin_port);
inet_ntop(AF_INET, addr, ipBuf, sizeof(ipBuf));
string receviersip(ipBuf);
struct hostent * he;
struct in_addr ipv4addr;
https: //beej.us/guide/bgnet/html/single/bgnet.html#inet_ntopman
inet_pton(AF_INET, receviersip.c_str(), & ipv4addr);
//Reference : https://beej.us/guide/bgnet/html/single/bgnet.html#gethostbynameman
he = gethostbyaddr( & ipv4addr, sizeof ipv4addr, AF_INET);
loggedInDetails l;
l.name = he - > h_name;
l.ip = receviersip;
l.port = port;
loggedinvector.push_back(l);
}
}
}
sort(loggedinvector.begin(), loggedinvector.end(), comparePorts);
return loggedinvector;
}
//Reference : http://www.cplusplus.com/reference/string/string/find_first_not_of/
//https://www.techiedelight.com/trim-string-cpp-remove-leading-trailing-spaces/
string ltrim(string s) {
size_t start = s.find_first_not_of(" ");
return (start == string::npos) ? "" : s.substr(start);
}
//Reference : http://www.cplusplus.com/reference/string/string/find_last_not_of/
//https://www.techiedelight.com/trim-string-cpp-remove-leading-trailing-spaces/
string rtrim(string s) {
size_t end = s.find_last_not_of(" ");
return (end == string::npos) ? "" : s.substr(0, end + 1);
}
string getOriginalMessage(string message) {
string tempString(message);
string last_element(tempString.substr(tempString.rfind("^") + 1));
size_t found = tempString.rfind("^");
if (found != std::string::npos)
tempString.replace(found, tempString.length(), "");
tempString = rtrim(ltrim(tempString));
return tempString;
}
string getSendersIp(string message) {
string tempString(message);
string last_element(tempString.substr(tempString.rfind("^") + 1));
return last_element;
}
string getIpfromSocket(int socket) {
struct sockaddr_in * connectedIp;
socklen_t len = sizeof(connectedIp);
getpeername(socket, (struct sockaddr * ) & connectedIp, & len);
char ipBuf[INET_ADDRSTRLEN];
struct sockaddr_in * ipv4 = (struct sockaddr_in * ) & connectedIp;
void * addr = & ipv4 - > sin_addr;
int port = ntohs(ipv4 - > sin_port);
inet_ntop(AF_INET, addr, ipBuf, sizeof(ipBuf));
string receviersip(ipBuf);
return receviersip;
}
int checkBlockList(string recvrsIp, string sendersIp) {
int returnValue = 0;
pair < string, string > p = std::make_pair(recvrsIp, sendersIp);
if (std::find(blockList.begin(), blockList.end(), p) != blockList.end())
returnValue = 1;
return returnValue;
}
int unblockIp(string requestFromIp, string requestedIp) {
int returnValue = 0;
for (int i = 0; i < blockList.size(); i++) {
if (blockList[i].first == requestFromIp && blockList[i].second == requestedIp) {
blockList.erase(blockList.begin() + i);
returnValue = 1;
break;
}
}
return returnValue;
}
//Reference for extracting string from inpu
//http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
string skipFirstWord(string inputString) {
string returnString, tmp;
stringstream ssMessage(inputString);
ssMessage >> tmp;
getline(ssMessage, returnString);
return returnString;
}
//Reference for extracting string from inpu
//http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/
string skipTwoWords(string inputString) {
string returnString, tmp;
stringstream ssMessage(inputString);
ssMessage >> tmp >> tmp;
getline(ssMessage, returnString);
return returnString;
}
int validateIP(string inputString, string port) {
int isValidReceiver = 1;
string receiversIp = skipFirstWord(inputString);
receiversIp = rtrim(ltrim(receiversIp));
int validIporNot = validateIpAndPort(receiversIp, port);
if (validIporNot == 0) {
isValidReceiver = 0;
}
int isInLocalList = checkLocalList(receiversIp);
if (isInLocalList == 0)
isValidReceiver = 0;
return isValidReceiver;
}
int validateIpAndPort(string ip, string port) {
int isIpValid = 1;
struct sockaddr_in sa;
memset( & sa, 0, sizeof(sa));
ip = rtrim(ltrim(ip));
//Reference from beej to check for proper IP
int result = inet_pton(AF_INET, ip.c_str(), & (sa.sin_addr));
if (result != 1) {
isIpValid = 0;
}
if (!port.empty()) {
if (checkForValidPort(rtrim(ltrim(port))) == 0) {
isIpValid = 0;
}
}
return isIpValid;
}
int checkForValidPort(string port) {
int isValidPort = 1;
for (int i = 0; i < port.length(); i++) {
if (port[i] == '0' || port[i] == '1' || port[i] == '2' || port[i] == '3' || port[i] == '4' || port[i] == '5' || port[i] == '6' || port[i] == '7' || port[i] == '8' || port[i] == '9') {
isValidPort = 1;
} else {
isValidPort = 0;
break;
}
}
if (atoi(port.c_str()) < 0 || atoi(port.c_str()) > 65535)
isValidPort = 0;
return isValidPort;
}
int checkLocalList(string inputIp) {
int isInList = 0;
for (int i = 0; i < clientList.size(); i++) {
if (clientList[i].ip == inputIp) {
isInList = 1;
}
}
return isInList;
}
int validateIForSending(string inputString) {
int isValidReceiver = 1;
stringstream ss(inputString);
string intr;
string receiversIp;
int i = 0;
while (getline(ss, intr, ' ')) {
if (i == 1) {
receiversIp = intr;
break;
}
i++;
}
receiversIp = rtrim(ltrim(receiversIp));
int validIporNot = validateIpAndPort(receiversIp, "");
if (validIporNot == 0) {
isValidReceiver = 0;
}
int isInLocalList = checkLocalList(receiversIp);
if (isInLocalList == 0)
isValidReceiver = 0;
return isValidReceiver;
}
int checkBlockedListInServer(string inputString) {
int isValidIp = 1;
int validIporNot = validateIpAndPort(inputString, "");
if (validIporNot == 0) {
isValidIp = 0;
return isValidIp;
}
for (int i = 0; i < blockListByClient.size(); i++) {
if (blockListByClient[i].ip == inputString) {
isValidIp = 1;
}
}
return isValidIp;
}
void checkforMessagesInBuffer(int clientSocket, int maximumSocket) {
string clientIp = getIpfromSocket(clientSocket);
for (int i = 0; i < bufferList.size(); i++) {
if (bufferList[i].toIp == clientIp) {
unicastMessage(bufferList[i].message, clientIp, maximumSocket);
}
}
}