Repository navigation
Expand file tree
/
Copy pathJSON_JENNET.cpp
More file actions
186 lines (174 loc) · 6.97 KB
/
Copy pathJSON_JENNET.cpp
File metadata and controls
186 lines (174 loc) · 6.97 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
#include"JSON_JENNET.h"
JSON_JENNET::JSON_JENNET()
{
}
JSON_JENNET::~JSON_JENNET()
{
}
char* JSON_JENNET::read_file(const char* filename) {
FILE *f = fopen(filename, "rb");
if (!f) {
perror("Failed to open file");
return NULL;
}
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek(f, 0, SEEK_SET);
if (length < 0) {
fclose(f);
perror("ftell failed");
return NULL;
}
char *buffer = (char*)malloc(length + 1);
if (!buffer) {
fclose(f);
fprintf(stderr, "Memory allocation failed\n");
return NULL;
}
size_t read_len = fread(buffer, 1, length, f);
fclose(f);
if (read_len != length) {
free(buffer);
fprintf(stderr, "Failed to read whole file\n");
return NULL;
}
buffer[length] = '\0';
return buffer;
}
void JSON_JENNET::consturct(Packet* pkt, ETHERNET_HEADER header){
pkt->copyAdv(&header, sizeof(ETHERNET_HEADER));
if (enableIPV4) {
pkt->copyAdv(ipv4.header, sizeof(IPV4_HEADER));
}
if (enableTCP) {
pkt->copyAdv(tcp.header, sizeof(TCP_HEADER));
if (tcp.payload.size()) {
pkt->copyAdv(tcp.payload.data(), tcp.payload.size());
}
}
if (enableICMP) {
pkt->copyAdv(icmp.header, sizeof(ICMP_HEADER));
}
if (enableARP)
{
pkt->copyAdv(arp.header, sizeof(ARP_HEADER));
}
if (enableUDP) {
pkt->copyAdv(udp.header, sizeof(UDP_HEADER));
if (udp.payload.size()) {
pkt->copyAdv(udp.payload.data(), udp.payload.size());
}
}
}
void JSON_JENNET::loadFeatures(const char* filename) {
char *json_text = read_file(filename);
if (!json_text) {
fprintf(stderr, "Failed to read JSON file\n");
return;
}
cJSON *root = cJSON_Parse(json_text);
if (!root) {
fprintf(stderr, "Failed to parse JSON: %s\n", cJSON_GetErrorPtr());
free(json_text);
return;
}
cJSON *ipv4Json = cJSON_GetObjectItem(root, "IPV4");
if (ipv4Json)
{
ipv4.header = new IPV4_HEADER;
totalSize += sizeof(IPV4_HEADER);
enableIPV4 = true;
ipv4.header->version_IHL = (cJSON_GetObjectItem(ipv4Json, "version")->valueint << 4) |
(cJSON_GetObjectItem(ipv4Json, "IHL")->valueint & 0x0F);
ipv4.header->TOS = cJSON_GetObjectItem(ipv4Json, "TOS")->valueint;
ipv4.header->id = convertToBigEndian16(cJSON_GetObjectItem(ipv4Json, "id")->valueint);
ipv4.header->flags_fragmentOffset = convertToBigEndian16(cJSON_GetObjectItem(ipv4Json,"flags")->valueint << 13 | cJSON_GetObjectItem(ipv4Json, "fragmentOffset")->valueint);
ipv4.header->TTL = cJSON_GetObjectItem(ipv4Json, "TTL")->valueint;
ipv4.header->protocol = cJSON_GetObjectItem(ipv4Json, "protocol")->valueint;
}
cJSON *tcpJson = cJSON_GetObjectItem(root, "TCP");
if (tcpJson)
{
tcp.header = new TCP_HEADER;
enableTCP = true;
cJSON *tcpOptionsJson = cJSON_GetObjectItem(root, "TCP_OP");
if (tcpOptionsJson)
{
tcp.addSynOptions(
cJSON_GetObjectItem(tcpOptionsJson, "MSS")->valueint,
cJSON_GetObjectItem(tcpOptionsJson, "WindowScale")->valueint,
cJSON_GetObjectItem(tcpOptionsJson, "TSVal")->valueint,
cJSON_GetObjectItem(tcpOptionsJson, "TSEcho")->valueint,
cJSON_IsTrue(cJSON_GetObjectItem(tcpOptionsJson, "SACK"))
);
}
cJSON *textTCP = cJSON_GetObjectItem(root, "TEXT");
if (textTCP)
{
tcp.addText(cJSON_GetObjectItem(textTCP, "DATA")->valuestring);
}
totalSize += sizeof(TCP_HEADER) + tcp.payload.size();
tcp.construtPrmtv(2);
tcp.header->srcPort = convertToBigEndian16(cJSON_GetObjectItem(tcpJson, "srcPort")->valueint);
tcp.header->destPort = convertToBigEndian16(cJSON_GetObjectItem(tcpJson, "dstPort")->valueint);
tcp.header->seqNum = convertToBigEndian32(cJSON_GetObjectItem(tcpJson, "seqNum")->valueint);
tcp.header->ackNum = convertToBigEndian32(cJSON_GetObjectItem(tcpJson, "ackNum")->valueint);
tcp.header->windowSize = convertToBigEndian16(cJSON_GetObjectItem(tcpJson, "windowSize")->valueint);
tcp.header->flag = cJSON_GetObjectItem(tcpJson, "flags")->valueint;
tcp.header->urgPointer = convertToBigEndian16(cJSON_GetObjectItem(tcpJson, "urgPointer")->valueint);
tcp.header->dataOffReservedAndNS =
(((sizeof(TCP_HEADER) + tcp.payload.size()) / 4) << 4) |
((cJSON_GetObjectItem(tcpJson, "reserved")->valueint & 0x07) << 1) |
(cJSON_GetObjectItem(tcpJson, "NS")->valueint & 0x01);
if (enableIPV4) {
ipv4.header->totalLen = convertToBigEndian16(sizeof(IPV4_HEADER) + sizeof(TCP_HEADER) + tcp.payload.size());
ipv4.applyChecksum();
}
tcp.configurePseudoHeader(*ipv4.header);
tcp.applyChecksum();
}
cJSON *icmpJson = cJSON_GetObjectItem(root, "ICMP");
if (icmpJson)
{
icmp.header = new ICMP_HEADER;
totalSize += sizeof(ICMP_HEADER);
enableICMP = true;
icmp.header->type = cJSON_GetObjectItem(icmpJson, "type")->valueint;
icmp.header->code = cJSON_GetObjectItem(icmpJson, "code")->valueint;
bytes_2 id = (bytes_2)cJSON_GetObjectItem(icmpJson, "id")->valueint;
bytes_2 seq = (bytes_2)cJSON_GetObjectItem(icmpJson, "seq")->valueint;
icmp.header->extendedHeader = convertToBigEndian32(((bytes_4)id << 16) | seq);
ipv4.header->totalLen = convertToBigEndian16(sizeof(IPV4_HEADER) + sizeof(ICMP_HEADER));
icmp.applyChecksum();
}
cJSON *arpJson = cJSON_GetObjectItem(root, "ARP");
if (arpJson)
{
arp.header = new ARP_HEADER;
totalSize += sizeof(ARP_HEADER);
enableARP = true;
memset(arp.header->reciveAdrr, 0, 6);
arp.header->hardwareType = convertToBigEndian16((bytes_2)cJSON_GetObjectItem(arpJson, "htype")->valueint);
arp.header->operation = convertToBigEndian16((bytes_2)cJSON_GetObjectItem(arpJson, "operation")->valueint);
}
cJSON *udpJson = cJSON_GetObjectItem(root, "UDP");
if (udpJson)
{
udp.header = new UDP_HEADER;
totalSize += sizeof(UDP_HEADER);
enableUDP = true;
udp.header->srcPort = convertToBigEndian16(cJSON_GetObjectItem(udpJson, "srcPort")->valueint);
udp.header->dstPort = convertToBigEndian16(cJSON_GetObjectItem(udpJson, "dstPort")->valueint);
cJSON *textUDP = cJSON_GetObjectItem(root, "TEXT");
if (textUDP)
{
udp.addText(cJSON_GetObjectItem(textUDP, "DATA")->valuestring);
totalSize += udp.payload.size();
}
ipv4.header->totalLen = convertToBigEndian16(sizeof(IPV4_HEADER) + sizeof(UDP_HEADER) + udp.payload.size());
udp.configurePseudoHeader(*ipv4.header);
udp.applyChecksum();
}
cJSON_Delete(root);
free(json_text);
}