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
|
// simple worm model based on message passing
//
#include "worm.h"
#include "random.h"
#include "math.h"
// timer for sending probes
void ProbingTimer::expire(Event*) {
t_->timeout();
}
// base class for worm: host is invulnerable by default
static class WormAppClass : public TclClass {
public:
WormAppClass() : TclClass("Application/Worm") {}
TclObject* create(int, const char*const*) {
return (new WormApp());
}
} class_app_worm;
// Initialize static variables
double WormApp::total_addr_ = pow(2, 32);
int WormApp::first_probe_ = 0;
WormApp::WormApp() : Application() {
// get probing rate from configuration
bind("ScanRate", &scan_rate_);
// get probing port from configuration
bind("ScanPort", &scan_port_);
// get probing port from configuration
bind("ScanPacketSize", &p_size_);
}
void WormApp::process_data(int nbytes, AppData*) {
recv(nbytes);
}
void WormApp::recv(int) {
if (!first_probe_) {
first_probe_ = 1;
printf("D FP %.2f\n", Scheduler::instance().clock());
}
//printf("D U %.2f %d\n",
// Scheduler::instance().clock(), my_addr_);
}
void WormApp::timeout() {
}
int WormApp::command(int argc, const char*const* argv) {
Tcl& tcl = Tcl::instance();
if (argc == 3) {
if (strcmp(argv[1], "attach-agent") == 0) {
agent_ = (Agent*) TclObject::lookup(argv[2]);
if (agent_ == 0) {
tcl.resultf("no such agent %s", argv[2]);
return(TCL_ERROR);
}
agent_->attachApp(this);
my_addr_ = agent_->addr();
//printf("%d\n", my_addr_);
return(TCL_OK);
}
}
return(Application::command(argc, argv));
}
// Initialize stats (number of infected hosts) for DN
unsigned long DnhWormApp::infect_total_ = 0;
unsigned long DnhWormApp::addr_high_ = 0;
unsigned long DnhWormApp::addr_low_ = 0;
unsigned long DnhWormApp::default_gw_ = 0;
float DnhWormApp::local_p_ = 0;
// class to model vulnerable hosts in detailed network
static class DnhWormAppClass : public TclClass {
public:
DnhWormAppClass() : TclClass("Application/Worm/Dnh") {}
TclObject* create(int, const char*const*) {
return (new DnhWormApp());
}
} class_app_worm_dnh;
DnhWormApp::DnhWormApp() : WormApp() {
infected_ = false;
timer_ = NULL;
}
void DnhWormApp::recv(int) {
if (infected_) {
//printf("Node %d is infected already...\n", my_addr_);
} else {
if (!first_probe_) {
first_probe_ = 1;
printf("D FP %.2f\n", Scheduler::instance().clock());
}
printf("D C %.2f %lu %lu\n",
Scheduler::instance().clock(), infect_total_, my_addr_);
// start to probe other hosts
probe();
}
}
void DnhWormApp::timeout() {
timer_->resched(p_inv_);
send_probe();
}
void DnhWormApp::probe() {
infected_ = true;
infect_total_++;
if (scan_rate_) {
p_inv_ = 1.0 / scan_rate_;
timer_ = new ProbingTimer((WormApp *)this);
timer_->sched(p_inv_);
}
}
void DnhWormApp::send_probe() {
double range_low, range_high;
unsigned long d_addr;
ns_addr_t dst;
// do not probe myself
d_addr = my_addr_;
if (Random::uniform(0.0, 1.0) < local_p_) {
range_low = addr_low_;
range_high = addr_high_;
} else {
range_low = 0;
range_high = total_addr_;
}
while (d_addr == my_addr_)
d_addr = static_cast<unsigned long>(Random::uniform(range_low, range_high));
// probe within my AS
if (addr_low_ <= d_addr && d_addr <= addr_high_) {
//printf("D PD %.2f %d %d\n",
// Scheduler::instance().clock(), my_addr_, d_addr);
} else {
//printf("Node %d is probing node %d, within AN, send to node %d\n",
// my_addr_, d_addr, default_gw_);
}
dst.addr_ = d_addr;
dst.port_ = scan_port_;
agent_->sendto((int)p_size_, (const char *)NULL, dst);
}
int DnhWormApp::command(int argc, const char*const* argv) {
if (argc == 3) {
if (strcmp(argv[1], "gw") == 0) {
default_gw_ = atol(argv[2]);
return(TCL_OK);
}
if (strcmp(argv[1], "local-p") == 0) {
local_p_ = atof(argv[2]);
return(TCL_OK);
}
}
if (argc == 4) {
if (strcmp(argv[1], "addr-range") == 0) {
addr_low_ = atol(argv[2]);
addr_high_ = atol(argv[3]);
//printf("DN low: %d, high: %d\n", addr_low_, addr_high_);
return(TCL_OK);
}
}
return(WormApp::command(argc, argv));
}
// class to model vulnerable hosts in detailed network
static class AnWormAppClass : public TclClass {
public:
AnWormAppClass() : TclClass("Application/Worm/An") {}
TclObject* create(int, const char*const*) {
return (new AnWormApp());
}
} class_app_worm_an;
AnWormApp::AnWormApp() : WormApp() {
// using 1 second as the unit of time step
//time_step_ = 1;
timer_ = NULL;
addr_low_ = addr_high_ = my_addr_;
s_ = i_ = 0;
v_percentage_ = 0;
beta_ = gamma_ = 0;
n_ = r_ = 1;
probe_in = probe_out = probe_recv = 0;
// get time step from configuration
bind("TimeStep", &time_step_);
}
void AnWormApp::start() {
// initial value of i and s
i_ = 1;
s_ -= 1;
timer_ = new ProbingTimer((WormApp *)this);
timer_->sched((double)time_step_);
//printf("start\n");
}
void AnWormApp::recv(int) {
probe_recv++;
//printf("AN (%d) received probes from outside...%f \n", my_addr_, probe_recv);
}
void AnWormApp::timeout() {
//printf("timeout\n");
timer_->resched((double)time_step_);
update();
}
void AnWormApp::update() {
// schedule next timeout
timer_->resched(time_step_);
probe_out = scan_rate_ * i_ * (dn_high_ - dn_low_ + 1) * time_step_ / total_addr_;
// not every probe received has effect
probe_in = probe_recv * s_ / n_;
probe_recv = 0;
// update states in abstract networks
// update r (recovered/removed)
r_ = r_ + gamma_ * i_;
if (r_ < 0)
r_ = 0;
if (r_ > n_)
r_ = n_;
// update i (infected)
// contains four parts:
// 1. i of last time period, 2. increase due to internal probing
// 3. decrease due to internal recovery/removal,
// 4. increase due to external probing
// should use n_ or s_max_???
//i_ = i_ + beta_ * i_ * (s_ / n_) * time_step_ - gamma_ * i_ + probe_in;
i_ = i_ + beta_ * i_ * (s_ / s_max_) * time_step_ - gamma_ * i_ + probe_in;
if (i_ < 0)
i_ = 0;
if (i_ > n_ - r_)
i_ = n_ - r_;
// update s (susceptible)
// use n = r + i + s
s_ = n_ - r_ - i_;
printf("A %.2f %d %d %d %d %d\n",
Scheduler::instance().clock(),
(int)s_, (int)i_, (int)r_, (int)probe_in, (int)probe_out);
// probe outside networks
// should not be cumulated!!!
//probe_out = 2;
if (probe_out > 1) {
//printf("ANS %.2f %d %d %d %d %d\n",
// Scheduler::instance().clock(),
// (int)s_, (int)i_, (int)r_, (int)probe_in, (int)probe_out);
probe((int)(probe_out + 0.5));
}
}
void AnWormApp::probe(int times) {
// send out probes in a batch
int i;
unsigned long d_addr;
ns_addr_t dst;
i = 0;
while (i < times) {
d_addr = dn_low_ + (int)Random::uniform(dn_high_ - dn_low_);
// do not send to myself or AS
if (dn_low_ < d_addr && d_addr < dn_high_) {
// probe outside
//printf("AN is probing node %d, outside AN\n", d_addr);
dst.addr_ = d_addr;
dst.port_ = scan_port_;
agent_->sendto((int)p_size_, (const char *)NULL, dst);
i++;
}
}
}
int AnWormApp::command(int argc, const char*const* argv) {
if (argc == 3) {
if (strcmp(argv[1], "v_percent") == 0) {
if (n_ == 0) {
printf("space range is not specificed!\n");
return(TCL_ERROR);
} else {
v_percentage_ = atof(argv[2]);
s_ = (int)(n_ * v_percentage_);
if (s_ < 1)
s_ = 1;
s_max_ = s_;
r_ = n_ - s_;
// use the equation in Moore's Internet Quarantine paper:
// beta = scan_rate * total_vulnerable / 2^32
beta_ = scan_rate_ * s_max_ / total_addr_;
//printf("inferred beta from scan rate: %f, %f, %d, %f\n",
// beta_, scan_rate_, (int)s_max_, total_addr_);
return(TCL_OK);
}
}
if (strcmp(argv[1], "beta") == 0) {
beta_ = atof(argv[2]);
//printf("beta: %f\n", beta_);
return(TCL_OK);
}
if (strcmp(argv[1], "gamma") == 0) {
gamma_ = atof(argv[2]);
//printf("gamma: %f\n", gamma_);
return(TCL_OK);
}
}
if (argc == 4) {
if (strcmp(argv[1], "addr-range") == 0) {
addr_low_ = atol(argv[2]);
addr_high_ = atol(argv[3]);
// initialize SIR model states
n_ = addr_high_ - addr_low_ + 1;
//printf("AN low: %d, high: %d, n: %f\n", addr_low_, addr_high_, n_);
return(TCL_OK);
}
if (strcmp(argv[1], "dn-range") == 0) {
dn_low_ = atoi(argv[2]);
dn_high_ = atoi(argv[3]);
//printf("AN-DN low: %d, high: %d\n", dn_low_, dn_high_);
return(TCL_OK);
}
}
return(WormApp::command(argc, argv));
}
|