-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_link.py
149 lines (135 loc) · 4.99 KB
/
test_link.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
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
import random
import os
import argparse
import sketches
import numpy as np
from network_sketches import *
from os.path import basename
from scapy.all import *
def get_packet_length(pkt):
n = 0
while not pkt.name in ['IPv6', 'IP']:
header = pkt.copy()
header.remove_payload()
n += len(header)
pkt = pkt.getlayer(1)
ip_len = pkt.len
header = pkt.copy()
header.remove_payload()
n += len(header)
return n + ip_len
neighbors = {
#qMp-fe4c fe80::6f0:21ff:fe03:64
"04:f0:21:03:00:64" : (0.05**4, 0.0, 1.5, 1.8),
#GS-RamblaBadal fe80::215:6dff:fe80:11ce
"00:15:6d:80:11:ce" : (0.5, 0.5, 33.9, 98.6),
#GSmVictoria fe80::de9f:dbff:fe08:8da9
"dc:9f:db:08:8d:a9" : (0.05**4, 0.03**4, 2.5, 4.8),
}
parser = argparse.ArgumentParser()
parser.add_argument("_id")
parser.add_argument("threshold", type=float)
parser.add_argument("nmax", default=1, nargs='?', type=int)
globals().update(vars(parser.parse_args()))
pcap = "pcaps/morning_sagunt.pcap"
local_mac = "dc:9f:db:08:89:71"
local_address = "fe80::de9f:dbff:fe08:8971"
pattern = 'morning_sagunt'
interval = 5.
num_cols = 32
num_rows = 32
captured_length = 94
neigh_in = dict()
neigh_out = dict()
local_in = dict()
local_out = dict()
sketch = sketches.FastCount32(num_cols, num_rows, 'cw4')
net_sketch = NetworkSketch(sketch)
for neighbor in neighbors:
neigh_in[neighbor] = net_sketch.copy()
neigh_out[neighbor] = net_sketch.copy()
local_in[neighbor] = net_sketch.copy()
local_out[neighbor] = net_sketch.copy()
results = []
pkts = PcapReader(pcap)
next_interval = pkts.next().time + interval
pkts.close()
pkts = PcapReader(pcap)
global_in = net_sketch.copy()
global_out = net_sketch.copy()
i = -1
for pkt in pkts:
i += 1
if pkt.time > next_interval:
# Corrupt sketches and create global:
detected = False
for neighbor in neighbors:
loss_probability = neighbors[neighbor][1]
neigh_in[neighbor].corrupt(local_out[neighbor], threshold, loss_probability, False, nmax)
if local_out[neighbor].detect_link(neigh_in[neighbor], threshold,
loss_probability, False, nmax):
detected = True
loss_probability = neighbors[neighbor][0]
neigh_out[neighbor].corrupt(local_in[neighbor], threshold, loss_probability, True, nmax)
if local_in[neighbor].detect_link(neigh_out[neighbor], threshold, loss_probability, True, nmax):
detected = True
global_in += neigh_out[neighbor]
global_out += neigh_in[neighbor]
# Clear sketch for next iteration
neigh_out[neighbor].clear()
neigh_in[neighbor].clear()
local_in[neighbor].clear()
local_out[neighbor].clear()
# Compute the statistics:
estimated_sent = global_in.second_moment()
estimated_received = global_out.second_moment()
estimated_missing_packets = global_in.difference(global_out)
max_value = max(global_in.sketch.get_max(), global_out.sketch.get_max())
result = (estimated_sent, estimated_received,
estimated_missing_packets, max_value, interval, num_rows, num_cols,
str(sketch.__class__), detected)
results.append(result)
# Clear the sketches
global_in.clear()
global_out.clear()
next_interval += interval
src = pkt.getfieldval('src')
dst = pkt.getfieldval('dst')
# Skip pkts for or from local address
if pkt.getlayer(1).src == local_address or pkt.getlayer(1).dst == local_address:
continue
if src in neighbors and dst == local_mac:
neighbor = neighbors[src]
# Compute the packet drop probabilities:
link_drop = random.random() < neighbor[0]
# Add to neigh out_dump
neigh_out[src].update(pkt)
# Add to local in if no drop:
if not link_drop:
local_in[src].update(pkt)
# First drop with p_neigh probability:
if link_drop:
# Skip also next:
pkts.next()
i += 1
continue
elif dst in neighbors and src == local_mac:
neighbor = neighbors[dst]
# Add to local out
local_out[dst].update(pkt)
# If not dropped, add to neigh_in
if random.random() < neighbor[1]:
continue
neigh_in[dst].update(pkt)
results_dtype = [ ('EstimatedIn', 'float'),
('EstimatedOut', 'float'),
('EstimatedDifference', 'float'),
('MaxValue', 'float'),
('Interval', 'float'),
('SketchRows', 'float'),
('SketchColumns', 'float'),
('SketchClass', '|S96'),
('NeighborDetected', 'bool_')]
result = np.array(results, results_dtype)
np.savetxt("test_link_%s.csv" % _id, result, delimiter=',', fmt="%s",
header=",".join(result.dtype.names), comments='')