-
Notifications
You must be signed in to change notification settings - Fork 1
/
hosts.py
116 lines (96 loc) · 3.8 KB
/
hosts.py
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
import logging
import sys
import sim
STATUS_SHUTDOWN = 'host_status_shutdown'
STATUS_SECURE = 'host_status_secure'
STATUS_VULNERABLE = 'host_status_vulnerable'
STATUS_INFECTED = 'host_status_infected'
# We only create hosts for vulnerable and infected (online) devices.
# Secure and shut-down devices are handled without in-memory objects.
class Host(object): # {{{
@staticmethod
def bootup(hid):
host = Host(hid, STATUS_VULNERABLE)
logging.info('hid %d on_time %f', hid, host.on_time)
sim.host_tracker.add(host)
ev = (sim.now + host.on_time, host.shutdown, None)
sim.enqueue(ev)
def __init__(self, hid, status):
self.hid = hid
self.status = status
self.on_time = sim.dist_host_on_time() # pylint: disable=not-callable
self.infection_time = None
self.bot = None
def infect(self):
logging.debug('hid %d', self.hid)
assert self.status != STATUS_SECURE
assert self.status != STATUS_SHUTDOWN
if self.status == STATUS_INFECTED:
return False
else:
self.status = STATUS_INFECTED
self.infection_time = sim.now
self.bot = sim.bot_factory(self.hid) # pylint: disable=not-callable
self.bot.start()
return True
def shutdown(self, _none):
infection = sim.now
if self.status == STATUS_INFECTED:
self.bot.teardown()
infection = self.infection_time
logging.info('hid %d on_time %f infected %f', self.hid, self.on_time,
(sim.now - infection)/self.on_time)
off_time = sim.dist_host_off_time() # pylint: disable=not-callable
ev = (sim.now + off_time, Host.bootup, self.hid)
sim.enqueue(ev)
sim.host_tracker.delete(self)
# }}}
class HostTracker(object): # {{{
def __init__(self, config):
frac_vulnerable = float(config['frac_vulnerable'])
self.vulnerable_period = int(1.0/frac_vulnerable)
self.hid2host = dict()
def get(self, hid):
if (hid % self.vulnerable_period) != 0:
return None, STATUS_SECURE
if hid not in self.hid2host:
return None, STATUS_SHUTDOWN
else:
host = self.hid2host[hid]
return host, host.status
def add(self, host):
assert (host.hid % self.vulnerable_period) == 0
assert host.hid not in self.hid2host
self.hid2host[host.hid] = host
def delete(self, host):
assert host.hid in self.hid2host
del self.hid2host[host.hid]
def __str__(self):
return 'HostTracker: period %d online %d' % (self.vulnerable_period,
len(self.hid2host))
# }}}
class E2ELatency(object): # {{{
def __init__(self, config):
self.min = float(config['e2e_latency']['min'])
self.max = float(config['e2e_latency']['max'])
assert self.max >= self.min
self.timeout = float(config['e2e_latency']['timeout'])
def get(self, srchid, dsthid):
if srchid == dsthid:
return self.min
h = abs(hash('%d.%d' % (srchid, dsthid)))
scale = h/sys.maxsize
latency = scale*(self.max - self.min) + self.min
logging.debug('E2ELatency src %d dst %d scale %f lat %f',
srchid, dsthid, scale, latency)
return latency
def get_timeout(self):
return self.timeout
def get_auth_delay(self, srchid, dsthid):
return self.get(srchid, dsthid) * sim.config['nrtts_auth']
def get_infect_delay(self, srchid, dsthid):
return self.get(srchid, dsthid) * sim.config['nrtts_infect']
def __str__(self):
return 'E2ELatency min %f max %f timeout %f' % (self.min, self.max,
self.timeout)
# }}}