-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.py
69 lines (57 loc) · 2.08 KB
/
tester.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import uuid
from scapy.layers.rip import RIPEntry, RIP
import genetic_engine
from nets_manager import Translator
from scapy.all import *
def get_network_packs(genome):
left = []
right = []
translator = Translator(genome.nets, genome.nodes)
for f in genome.flows:
packs = f.generate(translator, 0)
for p in packs:
del p.chksum
p.src = p['IP'].src
p.dst = p['IP'].dst
left.extend([p for p in packs if translator.ip2pos[p['IP'].src] == 'l'])
right.extend([p for p in packs if translator.ip2pos[p['IP'].src] == 'r'])
left.sort(key=lambda pack: pack['IP'].time)
right.sort(key=lambda pack: pack['IP'].time)
return left, right
def network_packets_count_tester(genome):
genetic_engine.check_genome(genome)
left, right = get_network_packs(genome)
if len(left) > 0:
send(left, iface='eth1')
if len(right) > 0:
send(right, iface='eth0')
name = """/home/tmp/""" + str(uuid.uuid1())
f = open(name, 'w')
f.write(str(genome))
f.close()
if len(left) > 0:
wrpcap(name + '--left' + '.cap', left)
if len(right) > 0:
wrpcap(name + '--right' + '.cap', right)
return len(left) + len(right)
def network_listener(left_count, right_count):
left_sniffed = sniff(iface='eth0', count=left_count)
right_sniffed = sniff(iface='eth1', count=right_count)
return left_sniffed, right_sniffed
def route_sender(nets, translator):
rip_packs = []
entry_count = 0
rp = RIP()
for i in xrange(len(nets)):
rp = rp/RIPEntry(metric=1, mask=nets, addr=translator.net2ip[i])
entry_count += 1
# в один пакет можно поместить информацию лишь о 20 сетях
# поэтому каждые 20 сетей создаем новый пакет
if entry_count==20:
rip_packs.append(rp)
rp = RIP()
entry_count = 0
# отправляем маршрутную информацию
send(rip_packs, iface='eth0')