forked from martimy/flowmanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_monitor.py
113 lines (94 loc) · 3.54 KB
/
flow_monitor.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
# Copyright (c) 2019 Maen Artimy
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from ryu.lib import pcaplib
from ryu.lib.packet import arp
from ryu.lib.packet import ethernet
from ryu.lib.packet import icmp
from ryu.lib.packet import ipv4
from ryu.lib.packet import ipv6
from ryu.lib.packet import packet
from ryu.lib.packet import packet_base
from ryu.lib.packet import tcp
from ryu.lib.packet import udp
from ryu.lib.packet import vlan
import json
ETHERNET = ethernet.ethernet.__name__
VLAN = vlan.vlan.__name__
IPV4 = ipv4.ipv4.__name__
IPV6 = ipv6.ipv6.__name__
ARP = arp.arp.__name__
ICMP = icmp.icmp.__name__
TCP = tcp.tcp.__name__
UDP = udp.udp.__name__
# class Writer2(pcaplib.Writer):
# def __init__(self, file_obj, snaplen=65535, network=1):
# super(Writer2, self).__init__(
# file_obj, snaplen=snaplen, network=network)
# def write_pkt(self, buf, ts=None):
# super(Writer2, self).write_pkt(buf, ts=ts)
# self._f.flush()
class Tracker():
all_stats = []
def untrack(self, id):
if id in self.existing_name(self.all_stats):
root = self.get_name(id, self.all_stats)
self.all_stats.remove(root)
def reset(self, id):
if id in self.existing_name(self.all_stats):
root = self.get_name(id, self.all_stats)
root["children"] = []
def track(self, id, pkt):
# Find if a tree has been created for this ID,
# Otherwise, create a new one
if id in self.existing_name(self.all_stats):
root = self.get_name(id, self.all_stats)
else:
root = {"name": id, "children": []}
self.all_stats.append(root)
# Get all protocols in a packet
header_list = [(p.protocol_name, p) for p in pkt.protocols if isinstance(
p, packet_base.PacketBase)]
for k in header_list:
name = self.getName(k, header_list)
if name in self.existing_name(root['children']):
# If the protcol is found in the tree, make it root
# for the next protocol
root = self.get_name(name, root['children'])
else:
# If the protcol is not found in the tree, create a new node
# and make it root for the next protocol
new_root = {"name": name, "children": []}
root['children'].append(new_root)
root = new_root
c = root.setdefault('count', 0)
root['count'] = c + 1
return self.all_stats
def getName(self, k, header_list):
if k[0] in [ETHERNET, IPV4, IPV6]:
s = k[1].src
d = k[1].dst
return "{} [{}, {}]".format(k[0], s, d)
if k[0] in [TCP, UDP]:
#s = k[1].src_port
d = k[1].dst_port
return "{} [{}]".format(k[0], d)
return k[0]
def existing_name(self, lst):
for n in lst:
yield n['name']
def get_name(self, name, lst):
for n in lst:
if n.get('name', None) == name:
return n
return None