forked from hip2b2/poxstuff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong2.py
110 lines (88 loc) · 3.19 KB
/
pong2.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
# Copyright 2012 James McCauley
#
# This file is part of POX.
#
# POX is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# POX is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with POX. If not, see <http://www.gnu.org/licenses/>.
"""
A simple component that always replies to ARPs and pings.
When running this component, pings (and pretty much nothing else!)
should always work.
"""
from pox.core import core
import pox.openflow.libopenflow_01 as of
import pox.lib.packet as pkt
from pox.lib.addresses import EthAddr
log = core.getLogger()
def _handle_PacketIn (event):
packet = event.parsed
if packet.find("arp"):
# Reply to ARP
a = packet.find("arp")
if a.opcode == a.REQUEST:
r = pkt.arp()
r.hwtype = a.hwtype
r.prototype = a.prototype
r.hwlen = a.hwlen
r.protolen = a.protolen
r.opcode = r.REPLY
r.hwdst = a.hwsrc
r.protodst = a.protosrc
r.protosrc = a.protodst
r.hwsrc = EthAddr("02:00:DE:AD:BE:EF")
e = pkt.ethernet(type=packet.type, src=r.hwsrc, dst=a.hwsrc)
e.payload = r
msg = of.ofp_packet_out()
msg.data = e.pack()
msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT))
msg.in_port = event.port
event.connection.send(msg)
log.info("%s ARPed for %s", r.protodst, r.protosrc)
elif packet.find("icmp"):
# Reply to pings
# Make the ping reply
icmp = pkt.icmp()
icmp.type = pkt.TYPE_ECHO_REPLY
icmp.payload = packet.find("icmp").payload
# Make the IP packet around it
ipp = pkt.ipv4()
ipp.protocol = ipp.ICMP_PROTOCOL
ipp.srcip = packet.find("ipv4").dstip
ipp.dstip = packet.find("ipv4").srcip
# Ethernet around that...
e = pkt.ethernet()
e.src = packet.dst
e.dst = packet.src
e.type = e.IP_TYPE
# Hook them up...
ipp.payload = icmp
e.payload = ipp
# Send it back to the input port
msg = of.ofp_packet_out()
msg.actions.append(of.ofp_action_output(port = of.OFPP_IN_PORT))
msg.data = e.pack()
msg.in_port = event.port
event.connection.send(msg)
log.debug("%s pinged %s", ipp.dstip, ipp.srcip)
elif packet.find("tcp"):
log.debug("tcp found: %s:%s to %s:%s", packet.find("ipv4").srcip, packet.find("tcp").srcport, packet.find("ipv4").dstip, packet.find("tcp").dstport)
elif packet.find("udp"):
log.debug("udp found: %s:%s to %s:%s", packet.find("ipv4").srcip, packet.find("udp").srcport, packet.find("ipv4").dstip, packet.find("udp").dstport)
def launch ():
import pox.log.color
pox.log.color.launch()
import pox.log
pox.log.launch(format="[@@@bold@@@level%(name)-22s@@@reset] " +
"@@@bold%(message)s@@@normal")
core.openflow.addListenerByName("PacketIn", _handle_PacketIn)
log.info("Pong component running.")