Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Final Skeleton
- #
- # Hints/Reminders from Lab 3:
- #
- # To check the source and destination of an IP packet, you can use
- # the header information... For example:
- #
- # ip_header = packet.find('ipv4')
- #
- # if ip_header.srcip == "1.1.1.1":
- # print "Packet is from 1.1.1.1"
- #
- # Important Note: the "is" comparison DOES NOT work for IP address
- # comparisons in this way. You must use ==.
- #
- # To send an OpenFlow Message telling a switch to send packets out a
- # port, do the following, replacing <PORT> with the port number the
- # switch should send the packets out:
- #
- # msg = of.ofp_flow_mod()
- # msg.match = of.ofp_match.from_packet(packet)
- # msg.idle_timeout = 30
- # msg.hard_timeout = 30
- #
- # msg.actions.append(of.ofp_action_output(port = <PORT>))
- # msg.data = packet_in
- # self.connection.send(msg)
- #
- # To drop packets, simply omit the action.
- #
- 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):
- ip = packet.find('ipv4')
- icmp = packet.find('icmp')
- # Separate IP traffic from non-IP traffic
- if ip is not None:
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match.from_packet(packet)
- msg.idle_timeout = 30
- msg.hard_timeout = 60
- if switch_id == 1:
- if ip.dstip == "10.1.1.10":
- end_port = 3
- print("pass on")
- else:
- end_port = 2
- print("go back")
- elif switch_id == 2:
- if ip.dstip == "10.2.2.20":
- end_port = 3
- print("pass on")
- else:
- end_port = 2
- print("go back")
- elif switch_id == 3:
- if ip.dstip == "10.3.3.30":
- end_port = 3
- print("pass on")
- else:
- end_port = 2
- print("go back")
- elif switch_id == 4:
- if ip.srcip == "123.45.67.89" and icmp is not None:
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match.from_packet(packet)
- msg.idle_timeout = 30
- msg.hard_timeout = 30
- msg.data = packet_in
- self.connection.send(msg)
- if ip.dstip == "10.1.1.10":
- end_port = 1
- print("s4 to s1")
- if ip.dstip == "10.2.2.20":
- end_port = 2
- if ip.dstip == "10.3.3.30":
- end_port = 3
- if ip.dstip == "123.45.67.89":
- end_port = 4
- if ip.dstip == "10.5.5.50":
- if ip.srcip == "123.45.67.89":
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match.from_packet(packet)
- msg.idle_timeout = 30
- msg.hard_timeout = 30
- msg.data = packet_in
- self.connection.send(msg)
- else:
- end_port = 5
- elif switch_id == 5:
- if ip.dstip == "10.5.5.50":
- end_port = 3
- else:
- end_port = 2
- #if ip.srcip == "123.45.67.89":
- # if ip.protocol != icmp_protocol and ip.dstip != "10.5.5.50":
- # msg.actions.append(of.ofp_action_output(port = end_port))
- msg.actions.append(of.ofp_action_output(port = end_port))
- msg.data = packet_in
- self.connection.send(msg)
- else:
- msg = of.ofp_flow_mod()
- msg.match = of.ofp_match.from_packet(packet)
- msg.idle_timeout = 30
- msg.hard_timeout = 30
- msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
- msg.data = packet_in
- self.connection.send(msg)
- def _handle_PacketIn (self, event):
- """
- Handles packet in messages from the switch.
- """
- packet = event.parsed
- if not packet.parsed:
- log.warning("Ignoring incomplete packet")
- return
- packet_in = event.ofp
- 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement