-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
376 lines (341 loc) · 10.5 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
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
#include <iostream>
#include <QtXml/QtXml>
#include "gcn.h"
#include <string>
using namespace std;
const float time_step = 0.01f;
const float time_fin = 0.11f;
float time_curr;
struct domain_data_struct
{
int id;
int total_hosts_number;
int active_hosts_number; // N(t)
map<int,float> security_vector; // SV(t)
map<int,int> infection_vector; // IV(t)
float total_resoures;
float load_resoures; // Res(t)
Domain_FSM *d_fsm; // FPS
};
struct flow_data_struct
{
int id;
int source_domain;
int destination_domain;
string start_rate_function;
string path_calc_type;
vector<int> flow_path;
set<string> tag_cloud;
bool malware_flag;
int malware_id;
};
struct malware_data_struct
{
int id;
int infection_speed;
float copy_size;
};
struct link_data_struct
{
int id;
int s_domain_id;
int d_domain_id;
float bandwidth;
};
map<int,float> traverse_sv_node(const QDomNode& node)
{
map<int,float> sv_map;
QDomNode svNode = node.firstChild();
while(!svNode.isNull())
{
if(svNode.isElement())
{
QDomElement svElement = svNode.toElement();
if(!svElement.isNull())
{
if(svElement.tagName() == "SV_elem")
{
sv_map.insert(pair<int,float>(svElement.attribute("malware_id","").toInt(),
svElement.text().toFloat()));
}
}
}
svNode = svNode.nextSibling();
}
return sv_map;
}
map<int,int> traverse_iv_node(const QDomNode& node)
{
map<int,int> iv_map;
QDomNode ivNode = node.firstChild();
while(!ivNode.isNull())
{
if(ivNode.isElement())
{
QDomElement ivElement = ivNode.toElement();
if(!ivElement.isNull())
{
if(ivElement.tagName() == "IV_elem")
{
iv_map.insert(pair<int,int>(ivElement.attribute("malware_id","").toInt(),
ivElement.text().toInt()));
}
}
}
ivNode = ivNode.nextSibling();
}
return iv_map;
}
domain_data_struct traverse_domain_node(const QDomNode& node)
{
domain_data_struct dds;
QDomNode domainNode = node.firstChild();
while(!domainNode.isNull())
{
if(domainNode.isElement())
{
QDomElement domainElement = domainNode.toElement();
if(!domainElement.isNull())
{
if(domainElement.tagName() == "hosts_number")
{
dds.active_hosts_number = domainElement.text().toInt();
dds.total_hosts_number = domainElement.text().toInt();
}
else if(domainElement.tagName() == "res")
{
dds.total_resoures = domainElement.text().toFloat();
}
else if(domainElement.tagName() == "SV")
{
dds.security_vector = traverse_sv_node(domainNode);
}
else if(domainElement.tagName() == "IV")
{
dds.infection_vector = traverse_iv_node(domainNode);
}
}
}
domainNode = domainNode.nextSibling();
}
return dds;
}
malware_data_struct traverse_malware_node(const QDomNode& node)
{
malware_data_struct mds;
QDomNode malwareNode = node.firstChild();
while(!malwareNode.isNull())
{
if(malwareNode.isElement())
{
QDomElement malwareElement = malwareNode.toElement();
if(!malwareElement.isNull())
{
if(malwareElement.tagName() == "infection_speed")
{
mds.infection_speed = malwareElement.text().toInt();
}
else if(malwareElement.tagName() == "copy_size")
{
mds.copy_size = malwareElement.text().toFloat();
}
}
}
malwareNode = malwareNode.nextSibling();
}
return mds;
}
set<string> traverse_tag_cloud_node(const QDomNode& node)
{
set<string> tc;
QDomNode tagCloudNode = node.firstChild();
while(!tagCloudNode.isNull())
{
if(tagCloudNode.isElement())
{
QDomElement tagCloudElement = tagCloudNode.toElement();
if(!tagCloudElement.isNull())
{
if(tagCloudElement.tagName() == "tag")
{
tc.insert(tagCloudElement.text().toStdString());
}
}
}
tagCloudNode = tagCloudNode.nextSibling();
}
cout << endl;
return tc;
}
vector<int> traverse_flow_path_node(const QDomNode& node)
{
vector<int> fp;
QDomNode flowPathNode = node.firstChild();
while(!flowPathNode.isNull())
{
if(flowPathNode.isElement())
{
QDomElement flowPathElement = flowPathNode.toElement();
if(!flowPathElement.isNull())
{
if(flowPathElement.tagName() == "domain_in_path")
{
fp.push_back(flowPathElement.text().toInt());
}
}
}
flowPathNode = flowPathNode.nextSibling();
}
cout << endl;
return fp;
}
flow_data_struct traverse_flow_node(const QDomNode& node)
{
flow_data_struct fds;
QDomNode flowNode = node.firstChild();
while(!flowNode.isNull())
{
if(flowNode.isElement())
{
QDomElement flowElement = flowNode.toElement();
if(!flowElement.isNull())
{
if(flowElement.tagName() == "source_domain")
{
fds.source_domain = flowElement.text().toInt();
}
else if(flowElement.tagName() == "destination_domain")
{
fds.destination_domain = flowElement.text().toInt();
}
else if(flowElement.tagName() == "start_rate_function")
{
fds.start_rate_function = flowElement.text().toStdString();
}
else if(flowElement.tagName() == "flow_path")
{
fds.path_calc_type = flowElement.attribute("type","").toStdString();
if (fds.path_calc_type == "manual")
{
fds.flow_path = traverse_flow_path_node(flowNode);
}
}
else if(flowElement.tagName() == "tag_cloud")
{
fds.tag_cloud = traverse_tag_cloud_node(flowNode);
}
else if(flowElement.tagName() == "malware_flag")
{
if (flowElement.text() == "true")
{
fds.malware_flag = true;
fds.malware_id = flowElement.attribute("malware_id","").toInt();
}
else
{
fds.malware_flag = false;
fds.malware_id = -1;
}
}
}
}
flowNode = flowNode.nextSibling();
}
return fds;
}
void traverseNode(const QDomNode& node, GCN* network)
{
QDomNode domNode = node.firstChild();
while(!domNode.isNull())
{
if(domNode.isElement())
{
QDomElement domElement = domNode.toElement();
if(!domElement.isNull())
{
if(domElement.tagName() == "domain")
{
domain_data_struct dds;
dds = traverse_domain_node(domNode);
dds.id = domElement.attribute("id", "").toInt();
network->add_domain(dds.id,dds.total_hosts_number,dds.security_vector,
dds.infection_vector,dds.total_resoures);
}
else if(domElement.tagName() == "link")
{
link_data_struct lds;
lds.id = domElement.attribute("id","").toInt();
lds.s_domain_id = domElement.attribute("s_domain_id","").toInt();
lds.d_domain_id = domElement.attribute("d_domain_id","").toInt();
lds.bandwidth = domElement.text().toFloat();
// there is no speccial function traverse_link_node - because i'm lazy =)
network->add_link(lds.id,lds.s_domain_id,lds.d_domain_id,lds.bandwidth);
}
else if(domElement.tagName() == "malware")
{
malware_data_struct mds;
mds = traverse_malware_node(domNode);
mds.id = domElement.attribute("id","").toInt();
network->add_malware(mds.id,mds.infection_speed,mds.copy_size);
}
else if(domElement.tagName() == "flow")
{
flow_data_struct fds;
fds = traverse_flow_node(domNode);
fds.id = domElement.attribute("id","").toInt();
network->add_flow(fds.id,fds.source_domain,fds.destination_domain,
fds.start_rate_function,fds.path_calc_type,fds.flow_path,fds.tag_cloud,fds.malware_flag,fds.malware_id);
}
}
}
traverseNode(domNode,network);
domNode = domNode.nextSibling();
}
}
void Read_Config(GCN* network)
{
QDomDocument domDoc;
QFile file("C:\\Users\\anvial\\CppPr\\SystemModel\\net_config.xml");
if(file.open(QIODevice::ReadOnly))
{
if(domDoc.setContent(&file))
{
QDomElement domElement= domDoc.documentElement();
traverseNode(domElement, network);
}
file.close();
}
}
void test_set_diff()
{
set<int> s1,s2,s3;
s1.insert(1);
s1.insert(3);
s1.insert(2);
s2.insert(1);
s2.insert(3);
s2.insert(2);
s2.insert(4);
set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(), inserter(s3, s3.begin()));
cout << "DIFF = " << s3.size() << endl;
}
int main()
{
GCN* network = new GCN();
Read_Config(network);
network->show_all();
//BEGIN TEST ZONE
test_set_diff();
//END TEST ZONE
// main loop
time_curr = 0.11f;
while(time_curr <= time_fin)
{
// cout << "Curr time = " << time_curr << endl;
// network->log_domain_load_res(time_curr,2);
network->log_domain_inf_hosts(time_curr,2,1);
network->tick(time_curr);
time_curr += time_step;
}
return 0;
}