Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. # Final Skeleton
  2. #
  3. # Hints/Reminders from Lab 3:
  4. #
  5. # To check the source and destination of an IP packet, you can use
  6. # the header information... For example:
  7. #
  8. # ip_header = packet.find('ipv4')
  9. #
  10. # if ip_header.srcip == "1.1.1.1":
  11. # print "Packet is from 1.1.1.1"
  12. #
  13. # Important Note: the "is" comparison DOES NOT work for IP address
  14. # comparisons in this way. You must use ==.
  15. #
  16. # To send an OpenFlow Message telling a switch to send packets out a
  17. # port, do the following, replacing <PORT> with the port number the
  18. # switch should send the packets out:
  19. #
  20. # msg = of.ofp_flow_mod()
  21. # msg.match = of.ofp_match.from_packet(packet)
  22. # msg.idle_timeout = 30
  23. # msg.hard_timeout = 30
  24. #
  25. # msg.actions.append(of.ofp_action_output(port = <PORT>))
  26. # msg.data = packet_in
  27. # self.connection.send(msg)
  28. #
  29. # To drop packets, simply omit the action.
  30. #
  31.  
  32. from pox.core import core
  33. import pox.openflow.libopenflow_01 as of
  34.  
  35. log = core.getLogger()
  36.  
  37. class Final (object):
  38. """
  39. A Firewall object is created for each switch that connects.
  40. A Connection object for that switch is passed to the __init__ function.
  41. """
  42. def __init__ (self, connection):
  43. # Keep track of the connection to the switch so that we can
  44. # send it messages!
  45. self.connection = connection
  46.  
  47. # This binds our PacketIn event listener
  48. connection.addListeners(self)
  49.  
  50. def do_final (self, packet, packet_in, port_on_switch, switch_id):
  51. ip = packet.find('ipv4')
  52. icmp = packet.find('icmp')
  53.  
  54. # Separate IP traffic from non-IP traffic
  55. if ip is not None:
  56. msg = of.ofp_flow_mod()
  57. msg.match = of.ofp_match.from_packet(packet)
  58. msg.idle_timeout = 30
  59. msg.hard_timeout = 60
  60. if switch_id == 1:
  61. if ip.dstip == "10.1.1.10":
  62. end_port = 3
  63. print("pass on")
  64. else:
  65. end_port = 2
  66. print("go back")
  67. elif switch_id == 2:
  68. if ip.dstip == "10.2.2.20":
  69. end_port = 3
  70. print("pass on")
  71. else:
  72. end_port = 2
  73. print("go back")
  74. elif switch_id == 3:
  75. if ip.dstip == "10.3.3.30":
  76. end_port = 3
  77. print("pass on")
  78. else:
  79. end_port = 2
  80. print("go back")
  81. elif switch_id == 4:
  82. if ip.srcip == "123.45.67.89" and icmp is not None:
  83. msg = of.ofp_flow_mod()
  84. msg.match = of.ofp_match.from_packet(packet)
  85. msg.idle_timeout = 30
  86. msg.hard_timeout = 30
  87. msg.data = packet_in
  88. self.connection.send(msg)
  89. if ip.dstip == "10.1.1.10":
  90. end_port = 1
  91. print("s4 to s1")
  92. if ip.dstip == "10.2.2.20":
  93. end_port = 2
  94. if ip.dstip == "10.3.3.30":
  95. end_port = 3
  96. if ip.dstip == "123.45.67.89":
  97. end_port = 4
  98. if ip.dstip == "10.5.5.50":
  99. if ip.srcip == "123.45.67.89":
  100. msg = of.ofp_flow_mod()
  101. msg.match = of.ofp_match.from_packet(packet)
  102. msg.idle_timeout = 30
  103. msg.hard_timeout = 30
  104. msg.data = packet_in
  105. self.connection.send(msg)
  106. else:
  107. end_port = 5
  108.  
  109. elif switch_id == 5:
  110. if ip.dstip == "10.5.5.50":
  111. end_port = 3
  112. else:
  113. end_port = 2
  114.  
  115. #if ip.srcip == "123.45.67.89":
  116. # if ip.protocol != icmp_protocol and ip.dstip != "10.5.5.50":
  117. # msg.actions.append(of.ofp_action_output(port = end_port))
  118.  
  119. msg.actions.append(of.ofp_action_output(port = end_port))
  120. msg.data = packet_in
  121. self.connection.send(msg)
  122. else:
  123. msg = of.ofp_flow_mod()
  124. msg.match = of.ofp_match.from_packet(packet)
  125. msg.idle_timeout = 30
  126. msg.hard_timeout = 30
  127. msg.actions.append(of.ofp_action_output(port = of.OFPP_FLOOD))
  128. msg.data = packet_in
  129. self.connection.send(msg)
  130.  
  131. def _handle_PacketIn (self, event):
  132. """
  133. Handles packet in messages from the switch.
  134. """
  135. packet = event.parsed
  136. if not packet.parsed:
  137. log.warning("Ignoring incomplete packet")
  138. return
  139.  
  140. packet_in = event.ofp
  141. self.do_final(packet, packet_in, event.port, event.dpid)
  142.  
  143. def launch ():
  144. """
  145. Starts the component
  146. """
  147. def start_switch (event):
  148. log.debug("Controlling %s" % (event.connection,))
  149. Final(event.connection)
  150. core.openflow.addListenerByName("ConnectionUp", start_switch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement