#mytopo.py
from mininet.topo import Topo
class MyTopo(Topo):
def __init__(self):
Topo.__init__(self)
#add hosts
h1=self.addHost('h1',ip='10.1.1.1')
h2=self.addHost('h2',ip='10.1.1.2')
h3=self.addHost('h3',ip='10.1.1.3')
#add switchs
s1=self.addSwitch('s1')
#add controllers
c0 = self.addController('c0', controller=RemoteController, ip='127.0.0.1', port=6633)
#add links
self.addLink(h1, s1,1,1)
self.addLink(h2, s1,1,2)
self.addLink(h3, s1,1,3)
self.addLink(c0,s1,1,4)
topos = { 'mytopo': ( lambda: MyTopo() ) }
#construction.py
from scapy.all import *
def construction():
srcp = 1024
x=0
for i in range(10):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp , dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
for i in range(6):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp + 1, dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
for i in range(3):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp + 2, dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
for i in range(9):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp + 3, dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
for i in range(2):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp + 4, dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
for i in range(3):
ip_layer = IP(src="10.0.0.1", dst="10.0.0.2",proto=6)
tcp_layer = TCP(sport=srcp + 5, dport=80,flags="S")
tcp_packet = ip_layer / tcp_layer
wrpcap("./w/output_"+ str(x) +"pcap", tcp_packet)
x+=1
if __name__ == '__main__':
construction()
#CM.py
import mytopo
import construction
from scapy.all import *
from collections import defaultdict
import os
from scapy.all import rdpcap
class CountMinSketch:
def __init__(self, w, d):
self.w = w
self.d = d
self.tables = [defaultdict(int) for _ in range(w)] # 创建包含w个默认字典的列表,每个字典的默认值为0
def add(self, item): # 添加元素
for table in self.tables: # 遍历每个表
h = hash(item) % (1 << self.d) # 计算哈希值h,取模2^d
table[h] += 1
def estimate(self, item):
min_estimate = float('inf') # 初始化为正无穷大
for table in self.tables:
h = hash(item) % (1 << self.d)
if table[h] < min_estimate:
min_estimate = table[h]
return min_estimate
def process_packet(packet, sketch): # 处理数据包
key = (packet['Source'],packet['Destination'],packet['Src Port'], packet['Dst Port'], packet['Protocol']) # 五元组作为键
sketch.add(key)
return sketch
if __name__ == "__main__":
w = 10
d = 5
sketch = CountMinSketch(w, d)
# 获取目录work下的所有文件名
file_list = os.listdir("w")
packets = []
for file_name in file_list:
if file_name.endswith(".pcap"):
packets_temp=rdpcap(os.path.join("w",file_name))
packets.extend(packets_temp)
for packet in packets:
sketch = process_packet(packet, sketch)
for key in sketch.tables[0]:
print(f"{key}: {sketch.estimate(key)}")
#construction.py是自己用scapy库构造数据包,mytopo.py是搭建拓扑,CM.py是用count-min sketch 来统计构造的数据包中每个流的数量,哈希键值是五元组(源IP,目的IP,源端口,目的端口,传输层协议),结果在终端运行的时候报下面这个错误,根据网上的教程把scapy目录下的all.py里面关于ipv6的代码注释掉了还是没用,想求助一下有人知道怎么解决吗