-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_controller.py
186 lines (105 loc) · 4.68 KB
/
final_controller.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from pox.core import core
import pox.openflow.libopenflow_01 as of
log = core.getLogger()
class Final (object):
"""
A Firewall object is created for each switch that connects.
A Connection object for that switch is passed to the __init__ function.
"""
def __init__ (self, connection):
# Keep track of the connection to the switch so that we can
# send it messages!
self.connection = connection
# This binds our PacketIn event listener
connection.addListeners(self)
def do_final (self, packet, packet_in, port_on_switch, switch_id):
match = of.ofp_match.from_packet(packet_in)#match data from current packet event
print "The nw_proto is: ", match.nw_proto
Src = str(match.nw_src)
Dst = str(match.nw_dst)
h1ip = "10.0.1.101"
h2ip = "10.0.2.102"
h3ip = "10.0.3.103"
h4ip = "128.114.50.10"#Untrusted Host ip address
h5ip = "10.0.4.104"#Server Host ip address
ip = 0x0800 #check against match.dl_type
icmp = 1 #check against match.nw_proto
msg = of.ofp_packet_out(data=packet_in)
msg2 = of.ofp_flow_mod()
def send(outPort, ipSrc, ipDst, inPort, pktType, pktProto):
msg2.match.dl_type = pktType
msg2.match.nw_proto = pktProto
msg2.match.nw_src = ipSrc
msg2.match.nw_dst = ipDst
msg2.match.in_port = inPort
msg2.hard_timeout = 300
msg2.idle_timeout = 120
if (outPort == -1):
msg.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
self.connection.send(msg)
msg2.actions.append(of.ofp_action_output(port=of.OFPP_FLOOD))
self.connection.send(msg2)
elif (outPort == -2):
#msg.actions.append(of.ofp_action_output())
self.connection.send(msg)
#msg2.actions.append(of.ofp_action_output())
self.connection.send(msg2)
else:
msg.actions.append(of.ofp_action_output(port=outPort))
self.connection.send(msg)
msg2.actions.append(of.ofp_action_output(port=outPort))
self.connection.send(msg2)
#Implementing controller logic via if-elif-else tree.
if (switch_id == 1 or switch_id == 2 or switch_id == 3 or switch_id == 5):
if (port_on_switch == 1):
send(2, None, None, match.in_port, None, None)
else:
send(1, None, None, match.in_port, None, None)
elif (switch_id == 4):
if (match.dl_type != ip):
send(-1, match.nw_src, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
else:
if ((Src == h4ip and match.nw_proto == icmp) or (Src == h4ip and Dst == h5ip)):
if (match.nw_proto == icmp):
send(-2, match.nw_src, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
else:
send(-2, match.nw_src, match.nw_dst, match.in_port, match.dl_type, None)
elif (Src == h4ip and match.dl_type == ip and match.nw_proto != icmp and Dst != h5ip):
print "snoogens", "The protocol is: ", match.nw_proto
if (Dst == h1ip):
send(1, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
elif (Dst == h2ip):
send(2, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
else:
send(3, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
elif (Src != h4ip and match.dl_type == ip):
if (Dst == h1ip):
send(1, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
elif (Dst == h2ip):
send(2, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
elif (Dst == h3ip):
send(3, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
elif (Dst == h4ip):
send(4, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
else:
send(5, None, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
#elif (match.dl_type != ip):
#send(-1, match.nw_src, match.nw_dst, match.in_port, match.dl_type, match.nw_proto)
def _handle_PacketIn (self, event):
"""
Handles packet in messages from the switch.
"""
packet = event.parsed # This is the parsed packet data.
if not packet.parsed:
log.warning("Ignoring incomplete packet")
return
packet_in = event.ofp # The actual ofp_packet_in message.
self.do_final(packet, packet_in, event.port, event.dpid)
def launch ():
"""
Starts the component
"""
def start_switch (event):
log.debug("Controlling %s" % (event.connection,))
Final(event.connection)
core.openflow.addListenerByName("ConnectionUp", start_switch)