-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcn.cpp
326 lines (278 loc) · 11.2 KB
/
gcn.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
#include "gcn.h"
#include <QDebug>
GCN::GCN()
{
std::cout << "Created Global Network" << std::endl;
}
int GCN::add_domain(int domain_id, int thn, map<int, float> sv, map<int, int> iv, float tr)
{
Domain* domain = new Domain(domain_id,thn,sv,iv,tr);
this->domain_map.insert(pair<int, Domain*>(domain_id,domain));
return 0;
}
int GCN::add_link(int link_id, int sdid, int ddid, float b)
{
Link* link = new Link(link_id,sdid,ddid,b);
this->link_map.insert(pair<int, Link*>(link_id,link));
// add link to tick link state map
tick_link_state_map.insert(pair<int,float>(link_id,0.0f));
return 0;
}
int GCN::add_flow(int flow_id, int sd, int dd, string srf, string pct,vector<int> fp, set<string> tc, bool mf, int mid)
{
Flow* flow = new Flow(flow_id,sd,dd,srf,pct,fp,tc,mf,mid);
this->flow_map.insert(pair<int, Flow*>(flow_id,flow));
return 0;
}
int GCN::add_malware(int malware_id, int is, float cs)
{
Malware* malware = new Malware(malware_id,is,cs);
this->malware_map.insert(pair<int, Malware*>(malware_id,malware));
return 0;
}
//int GCN::flow_streaming(int flow_id)
//{
// Flow* flow;
// vector<int> flow_path;
// vector<int>::iterator flow_path_iter;
// flow = flow_map[flow_id];
// flow_path = flow->get_flow_path();
// for(flow_path_iter = flow_path.begin(); flow_path_iter != flow_path.end(); ++flow_path_iter)
// {
// Domain* domain;
// domain = domain_map[(*flow_path_iter)];
// if((*flow_path_iter) == flow_path.front()) // first iteration
// {
// float start_rate = flow->start_rate_generator();
// float overload = domain->add_load(start_rate);
// if(overload > 0.0f)
// {
// flow->change_state((*flow_path_iter),start_rate - overload, overload, 0.0f);
// }
// else
// {
// flow->change_state((*flow_path_iter),start_rate,0.0f,0.0f);
// }
// }
// else if((*flow_path_iter) == flow_path.back()) // last iteration
// {
// // TO DO;
// }
// }
// return 0;
//}
// domains than flows
// THIS THE MOST FUCKING COMPLEX FUNCTION IN THIS MODEL
int GCN::flows_throught_domains_streaming(float time)
{
map<int, Domain*>::iterator domain_map_iter;
for(domain_map_iter = domain_map.begin(); domain_map_iter != domain_map.end(); ++domain_map_iter)
{
Domain* domain;
int domain_id = domain_map_iter->first;
domain = domain_map_iter->second;
map<int, Flow*>::iterator flow_map_iter;
int domain_for_flow_type;
for(flow_map_iter = flow_map.begin(); flow_map_iter != flow_map.end(); ++flow_map_iter)
{
Flow* flow;
int flow_id = flow_map_iter->first;
flow = flow_map_iter->second;
domain_for_flow_type = flow->get_domain_type_for_this_flow(domain_id);
if(domain_for_flow_type == 0) // start domain for this flow
{
float start_rate = flow->start_rate_generator();
float overload = domain->add_load(flow_id, start_rate); // change domain load... overload may be NULL
flow->change_state(domain_id,start_rate - overload,overload,0.0f); // change flow state at this domain
//change domains vuln host number
if(flow->is_malware())
{
Malware* malware;
malware = malware_map[flow->get_flow_malware_id()];
domain->change_inf_hosts_number(flow->get_flow_malware_id(),start_rate,
malware->get_copy_size(),malware->get_infection_speed());
}
}
else if (domain_for_flow_type == 1) // transit domain for this flow
{
int prev_domain_id = flow->get_prev_domain_in_flow_path(domain_id);
Link* link = this->get_link(prev_domain_id,domain_id);
Domain* prev_domain = domain_map[prev_domain_id];
float flow_rate_in_prev_domain = prev_domain->get_flow_res(flow_id); // see how muck ress this flow took in prev domain
float transfer_rate; // next stage to calc transfer rate for this flow
float link_bandwidth = link->get_bandwidth(); // general link bandwidth
int link_id = link->get_link_id(); // link id
float tick_link_bandwidth = tick_link_state_map[link_id]; // current tick link bandwidth
if(flow_rate_in_prev_domain > (link_bandwidth - tick_link_bandwidth))
transfer_rate = link_bandwidth - tick_link_bandwidth; // we cant transfer more then link allowed
else
transfer_rate = flow_rate_in_prev_domain;
//change domains (curr and prev) load
prev_domain->sub_load(flow_id,transfer_rate);
domain->add_load(flow_id,transfer_rate);
//change domains vuln host number
if(flow->is_malware())
{
Malware* malware;
malware = malware_map[flow->get_flow_malware_id()];
domain->change_inf_hosts_number(flow->get_flow_malware_id(),transfer_rate,
malware->get_copy_size(),malware->get_infection_speed());
}
//change tick link state
tick_link_state_map[link_id] += transfer_rate;
}
else if (domain_for_flow_type == 2) // last domain for this flow
{
int prev_domain_id = flow->get_prev_domain_in_flow_path(domain_id);
Link* link = this->get_link(prev_domain_id,domain_id);
Domain* prev_domain = domain_map[prev_domain_id];
float flow_rate_in_prev_domain = prev_domain->get_flow_res(flow_id); // see how muck ress this flow took in prev domain
float transfer_rate; // next stage to calc transfer rate for this flow
float link_bandwidth = link->get_bandwidth(); // general link bandwidth
int link_id = link->get_link_id(); // link id
float tick_link_bandwidth = tick_link_state_map[link_id]; // current tick link bandwidth
if(flow_rate_in_prev_domain > (link_bandwidth - tick_link_bandwidth))
transfer_rate = link_bandwidth - tick_link_bandwidth; // we cant transfer more then link allowed
else
transfer_rate = flow_rate_in_prev_domain;
//change domains (curr and prev) load
prev_domain->sub_load(flow_id,transfer_rate); // NO ADD LOAD - becouse of consume
//change domains vuln host number
if(flow->is_malware())
{
Malware* malware;
malware = malware_map[flow->get_flow_malware_id()];
domain->change_inf_hosts_number(flow->get_flow_malware_id(),transfer_rate,
malware->get_copy_size(),malware->get_infection_speed());
}
//change tick link state
tick_link_state_map[link_id] += transfer_rate;
}
}
}
return 0;
}
Link *GCN::get_link(int s_domain_id, int d_domain_id)
{
map<int, Link*>::iterator link_map_iter;
for(link_map_iter = link_map.begin(); link_map_iter != link_map.end(); ++link_map_iter)
{
Link* link;
pair<int,int> domain_pair;
link = link_map_iter->second;
domain_pair = link->get_domain_pair();
if((domain_pair.first == s_domain_id) &&
(domain_pair.second == d_domain_id))
return link;
}
return NULL;
}
float GCN::get_link_bandwidth(int s_domain_id, int d_domain_id)
{
map<int, Link*>::iterator link_map_iter;
for(link_map_iter = link_map.begin(); link_map_iter != link_map.end(); ++link_map_iter)
{
Link* link;
pair<int,int> domain_pair;
link = link_map_iter->second;
domain_pair = link->get_domain_pair();
if((domain_pair.first == s_domain_id) &&
(domain_pair.second == d_domain_id))
return link->get_bandwidth();
}
return 0.0f;
}
int GCN::get_link_id(int s_domain_id, int d_domain_id)
{
map<int, Link*>::iterator link_map_iter;
for(link_map_iter = link_map.begin(); link_map_iter != link_map.end(); ++link_map_iter)
{
Link* link;
pair<int,int> domain_pair;
link = link_map_iter->second;
domain_pair = link->get_domain_pair();
if((domain_pair.first == s_domain_id) &&
(domain_pair.second == d_domain_id))
return link->get_link_id();
}
return -1;
}
void GCN::log_domain_load_res(float time, int domain_id)
{
Domain* domain;
domain = domain_map[domain_id];
cout << "TIME: " << time << " LOAD_RES = " << domain->get_load_res() << endl;
}
void GCN::log_domain_inf_hosts(float time, int domain_id, int malware_id)
{
Domain* domain;
domain = domain_map[domain_id];
cout << "TIME: " << time << " INF_HOSTS = " << domain->get_inf_hosts(malware_id) << endl;
}
void GCN::show_links()
{
map<int, Link*>::iterator link_map_iter;
Link* link;
cout << endl << "---Short info about links in Network " << endl;
for(link_map_iter = link_map.begin(); link_map_iter != link_map.end(); ++link_map_iter)
{
link = link_map_iter->second;
link->get_link_info();
}
cout << endl;
}
void GCN::show_domains()
{
map<int, Domain*>::iterator domain_map_iter;
Domain* domain;
cout << endl << "---Short info about domains in Network " << endl;
for(domain_map_iter = domain_map.begin(); domain_map_iter != domain_map.end(); ++domain_map_iter)
{
domain = domain_map_iter->second;
domain->get_domain_info();
}
cout << endl;
}
void GCN::show_flows()
{
map<int, Flow*>::iterator flow_map_iter;
Flow* flow;
cout << endl << "---Short info about flows in Network " << endl;
for(flow_map_iter = flow_map.begin(); flow_map_iter != flow_map.end(); ++flow_map_iter)
{
flow = flow_map_iter->second;
flow->get_flow_info();
}
cout << endl;
}
void GCN::show_malware()
{
map<int, Malware*>::iterator malware_map_iter;
Malware* malware;
cout << endl << "---Short info about malwares in Network " << endl;
for(malware_map_iter = malware_map.begin(); malware_map_iter != malware_map.end(); ++malware_map_iter)
{
malware = malware_map_iter->second;
malware->get_malware_info();
}
cout << endl;
}
void GCN::show_all()
{
cout << endl << "--- ALL INFO ABOUT NETWORK MODEL ---" << endl;
this->show_domains();
this->show_links();
this->show_flows();
this->show_malware();
}
int GCN::tick(float time)
{
// makw NULL all tick link states
map<int,float>::iterator tick_link_state_map_iter;
for(tick_link_state_map_iter = tick_link_state_map.begin(); tick_link_state_map_iter != tick_link_state_map.end(); ++tick_link_state_map_iter)
{
tick_link_state_map_iter->second = 0.0f;
}
this->flows_throught_domains_streaming(time);
return 0;
}