-
Notifications
You must be signed in to change notification settings - Fork 0
/
module_pox.py
127 lines (96 loc) · 2.8 KB
/
module_pox.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
#coding:utf-8
from pox.core import core
from pox.lib.util import dpidToStr
import pox.openflow.libopenflow_01 as of
from pox.openflow.of_json import *
from pox.lib.recoco import Timer
import time
import json
log = core.getLogger()
temp = time.time()
table={}
stats={}
blacklist = []
def sendpacket(event, dst_port = of.OFPP_ALL):
#ofp.in_port port par lequel le paquet est arrivé
msg = of.ofp_packet_out(in_port = event.ofp.in_port)
if event.ofp.buffer_id not in [-1,None]:
msg.buffer_id = event.ofp.buffer_id
else:
if not event.ofp.data:
return
msg.data = event.ofp.data
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
def switch(event):
global table
global temp
global blacklist
global stats
packet = event.parsed
#Ajout d'une entrée à la table de commutation
table[packet.src] = event.port
# essai de lecture du port de destination corespondant
#à la mac de destination
dst_port = table.get(packet.dst)
if str(packet.src) in blacklist:
sendpacket(event,port = 0)
#blacklist contenant les ports physiques à ignorer
elif packet.dst.is_multicast:
sendpacket(event,of.OFPP_FLOOD)
# gestion des Broadcast , à limiter
elif str(packet.dst) not in table:
sendpacket(event,of.OFPP_FLOOD)
else:
#creation et transmission d'une ligne de table de switch
dst_port = packet.dst
msg = of.ofp_flow_mod()
msg.idle_timeout = 10
msg.hard_timeout = 30
msg.actions.append(of.ofp_action_output(port = dst_port))
event.connection.send(msg)
log.debug("Installing %s;%i -> %s;%i" %
( packet.src, event.ofp.in_port, packet.dst, dst_port))
gather(packet,event)
if (int(time.time() -temp))>= 4:
temp = time.time()
with open("stats.txt","a+") as f:
f.write(str(stats))
blacklist = detect()
if len(blacklist) > 0:
with open("blocked","w+") as f:
f.write(str(blacklist)+"\n")
log.debug("removing")
for src_port in blacklist:
msg = of.ofp_flow_mod()
msg.idle_timeout = 100
msg.hard_timeout = 300
msg.match.in_port = int(src_port)
msg.actions.append(of.ofp_action_output(port = 0))
event.connection.send(msg)
log.debug("blocking %s",src_port)
def gather(packet,event):
global stats
src = str(event.port)
dst = str(packet.dst)
log.debug('port %s ----dest adress: %s',src , dst)
try:
stats[src+";"+dst] = stats[src+";"+dst] +1
except:
stats[src+";"+dst] = 1
#log.debug("N_comm :%s -- %s",src+";"+dst,stats[src+";"+dst])
def detect():
global stats
to_block=[]
for srcdst,t in stats.iteritems():
if (t>=400):
to_block.append(srcdst.split(';')[0])
with open("blocks.txt","a+") as f:
f.write(str(srcdst)+"---"+str(t))
f.write(" "+str(temp))
stats={}
return to_block
log.debug("temp %s", str(int(time.time())))
return True
def launch():
core.openflow.addListenerByName("PacketIn",switch)