This repository was archived by the owner on Feb 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilter.py
More file actions
86 lines (68 loc) · 2.73 KB
/
filter.py
File metadata and controls
86 lines (68 loc) · 2.73 KB
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
import networkx as nx
import matplotlib.pyplot as plt
from socket import socket as sock, AF_INET, SOCK_DGRAM
from select import select
POLL = 0
READ_SIZE = 4096
HDR_LEN = 24
REC_LEN = 52
NUM_FLOWS = 1
class Filter:
def __init__(self, host="", port=6969):
self.host = host
self.port = port
self.listener = sock(AF_INET, SOCK_DGRAM, 0)
self.listener.bind((host, port))
self.flowGraph = nx.Graph()
self.data = {}
self.nodeNames = ["---"]
self.nodeData = ["Select a node."]
self.avgByteGraph = []
self.avgLenGraph = []
self.numFlowsGraph = []
self.buffer = ""
self.lastTwentyRecords = []
self.lastTwentyHeaders = []
def updateNetworkGraph(self):
pos=nx.spring_layout(self.flowGraph)
nx.draw(self.flowGraph, pos, fontsize=10)
plt.axis('off')
plt.savefig("graph.png")
def addFlowToGraph(self, routerSrc, dst, nextHop):
if nextHop == "0.0.0.0":
self.flowGraph.add_edge(routerSrc, dst)
else:
self.flowGraph.add_edge(routerSrc, nextHop)
def getAvgBytes(self):
def getAvgFlowLength(self):
def getNumOfFlows(self):
return self.numFlowsGraph
def generateGraphs(self):
return [ self.getAvgBytes(), self.getAvgFlowLength(), self.getNumOfFlows() ]
def update(self, nodeIndex):
while select([self.listener], [], [], POLL)[0]:
self.buffer += self.listener.recv(READ_SIZE)
while self.buffer != "":
hdrPayload = list(unpack("!HHLLLLBBH", self.buffer[:HDR_LEN]))
self.lastTwentyHeaders.append(hdrPayload)
if len(self.lastTwentyHeaders) > 20:
self.lastTwentyHeaders = self.lastTwentyHeaders[-20:]
for idx in range(hdrPayload[NUM_FLOWS]):
start = HDR_LEN + (idx * REC_LEN)
end = start + REC_LEN
recPayload = list(unpack("!LLLHHLLLLHHBBBBHHBBHL", self.buffer[start:end]))
self.updateMetrics(getKey(recPayload), recPayload)
self.lastTwentyRecords.append(recPayload)
if len(self.lastTwentyRecords) > 20:
self.lastTwentyRecords = self.lastTwentyRecords[-20:]
return [ self.generateGraphs(), self.getNewNodes(), self.nodeData[nodeIndex] ]
def updateMetrics(self, unique, record):
#Check if the flow record is already in the dictionary
if unique in self.data:
flowRecord = self.data[unique]
# Check if start times are the same
if flowRecord[START] == record[START]:
self.data[unique][L3_BYTES] += record[L3_BYTES]
self.data[unique][END] = record[END]
else:
self.data[unique] = record