-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
66 lines (52 loc) · 1.54 KB
/
main.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
#include "RawSocket.hpp"
const char SERVER_MESSAGE[] = "\x04\x02Server~Open~1~";
const uint16_t DESTINATION_PORT = 47777;
const uint16_t SOURCE_PORT = 56947;
const __int32_t BROADCAST_OPTION = 1;
void signalCatch(int socket);
int main(int argc, char** argv) {
// Ensure proper usage
__int8_t sourceIpNo = 2;
__int8_t destIpNo = 4;
switch (argc){
case 1:{
printf("Usage: -s SourceIp -d DestinationIp\n");
return 0;
}
case 5:{
__int8_t verif = 0;
for (__int8_t i=1; i<argc; i++){
if (strcmp("-s", argv[i]) == 0){
sourceIpNo = i+1;
verif += 2;
}
if (strcmp("-d", argv[i]) == 0){
destIpNo = i+1;
verif += 3;
}
}
if (((sourceIpNo - destIpNo) == 2 || (destIpNo - sourceIpNo) == 2) && verif == 5)
break;
}
default:{
printf("Incorrect usage\n");
return 0;
}
}
signal(SIGINT, signalCatch);
RawSocket rawSocket;
rawSocket.setBroadcastOption(BROADCAST_OPTION);
rawSocket.setSource(argv[sourceIpNo], SOURCE_PORT);
rawSocket.setDestination(argv[destIpNo], DESTINATION_PORT);
rawSocket.setData((char*)SERVER_MESSAGE, strlen(SERVER_MESSAGE));
printf("Sending packets...\n");
for(;;) {
rawSocket.sendDatagram();
}
return 0;
}
void signalCatch(int socket) {
printf("\nClosing socket.\n");
close(socket);
exit(0);
}