Merge pull request #889 from gpotter2/py3-contrib-13

Python 3: fix OPENFLOW
This commit is contained in:
Pierre Lalet 2017-10-24 17:39:44 +02:00 committed by GitHub
commit e60e0da204
2 changed files with 17 additions and 16 deletions

View File

@ -16,6 +16,7 @@ import struct
from scapy.fields import *
from scapy.layers.l2 import *
from scapy.layers.inet import *
from scapy.compat import orb
### If prereq_autocomplete is True then match prerequisites will be
### automatically handled. See OFPMatch class.
@ -199,7 +200,7 @@ class OFPMatch(Packet):
p = p[:25] + struct.pack("!B", 0x06) + p[26:]
l = l[:-6] + "0" + l[-5:]
ins = "".join(chr(int("".join(x),2)) for x in zip(*[iter(l)]*8))
ins = b"".join(chb(int("".join(x),2)) for x in zip(*[iter(l)]*8))
p = ins + p[4:]
return p + pay
@ -1187,18 +1188,18 @@ def OpenFlow(self, payload):
if self is None or self.dport == 6653 or self.dport == 6633 or self.sport == 6653 or self.sport == 6633:
# port 6653 has been allocated by IANA, port 6633 should no longer be used
# OpenFlow function may be called with None self in OFPPacketField
of_type = ord(payload[1])
of_type = orb(payload[1])
if of_type == 1:
err_type = ord(payload[9])
err_type = orb(payload[9])
# err_type is a short int, but last byte is enough
if err_type == 255: err_type = 65535
return ofp_error_cls[err_type]
elif of_type == 16:
mp_type = ord(payload[9])
mp_type = orb(payload[9])
if mp_type == 255: mp_type = 65535
return ofp_stats_request_cls[mp_type]
elif of_type == 17:
mp_type = ord(payload[9])
mp_type = orb(payload[9])
if mp_type == 255: mp_type = 65535
return ofp_stats_reply_cls[mp_type]
else:

View File

@ -4,21 +4,21 @@
= OFPTHello(), simple hello message
ofm = OFPTHello()
str(ofm) == b'\x01\x00\x00\x08\x00\x00\x00\x00'
raw(ofm) == b'\x01\x00\x00\x08\x00\x00\x00\x00'
= OFPTEchoRequest(), echo request
ofm = OFPTEchoRequest()
str(ofm) == b'\x01\x02\x00\x08\x00\x00\x00\x00'
raw(ofm) == b'\x01\x02\x00\x08\x00\x00\x00\x00'
= OFPMatch(), check wildcard completion
ofm = OFPMatch(in_port=1, nw_tos=8)
ofm = OFPMatch(str(ofm))
ofm = OFPMatch(raw(ofm))
assert(ofm.wildcards1 == 0x1)
ofm.wildcards2 == 0xfe
= OpenFlow(), generic method test with OFPTEchoRequest()
ofm = OFPTEchoRequest()
s = str(ofm)
s = raw(ofm)
isinstance(OpenFlow(None,s)(s), OFPTEchoRequest)
= OFPTFlowMod(), check codes and defaults values
@ -36,20 +36,20 @@ act1 = OFPATSetNwSrc(nw_addr='192.168.42.1')
act2 = OFPATOutput(port='CONTROLLER')
act3 = OFPATSetDlSrc(dl_addr='1a:d5:cb:4e:3c:64')
ofm = OFPTFlowMod(priority=1000, match=mtc, flags='CHECK_OVERLAP', actions=[act1,act2,act3])
str(ofm)
raw(ofm)
s = b'\x01\x0e\x00h\x00\x00\x00\x00\x00?\xc8\xfd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8*\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\xe8\xff\xff\xff\xff\xff\xff\x00\x02\x00\x06\x00\x08\xc0\xa8*\x01\x00\x00\x00\x08\xff\xfd\xff\xff\x00\x04\x00\x10\x1a\xd5\xcbN<d\x00\x00\x00\x00\x00\x00'
str(ofm) == s
raw(ofm) == s
= OFPETBadRequest() containing a flow_mod with wrong table_id
flowmod = OFPTFlowMod(actions=OFPATOutput(port='LOCAL'))
ofm = OFPETBadRequest(errcode='OFPBRC_EPERM', data=str(flowmod))
ofm = OFPETBadRequest(errcode='OFPBRC_EPERM', data=raw(flowmod))
hexdump(ofm)
s = b'\x01\x01\x00\\\x00\x00\x00\x00\x00\x01\x00\x05\x01\x0e\x00P\x00\x00\x00\x00\x00?\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x08\xff\xfe\xff\xff'
str(ofm) == s
raw(ofm) == s
= OFPTPacketIn() containing an Ethernet frame
ofm = OFPTPacketIn(data=Ether()/IP()/ICMP())
p = OFPTPacketIn(str(ofm))
p = OFPTPacketIn(raw(ofm))
dat = p.data
assert(isinstance(dat, Ether))
assert(isinstance(dat.payload, IP))
@ -67,14 +67,14 @@ p[TCP].dport == 6653
= TCP()/OFPTHello() dissection, check new TCP.guess_payload_class
o = TCP()/OFPTHello()
p = TCP(str(o))
p = TCP(raw(o))
p[TCP].sport == 6653
isinstance(p[TCP].payload, OFPTHello)
= complete Ether()/IP()/TCP()/OFPTFeaturesRequest()
ofm = Ether(src='00:11:22:33:44:55',dst='01:23:45:67:89:ab')/IP(src='10.0.0.7',dst='192.168.0.42')/TCP(sport=6633)/OFPTFeaturesRequest(xid=23)
s = b'\x01#Eg\x89\xab\x00\x11"3DU\x08\x00E\x00\x000\x00\x01\x00\x00@\x06\xaf\xee\n\x00\x00\x07\xc0\xa8\x00*\x19\xe9\x00P\x00\x00\x00\x00\x00\x00\x00\x00P\x02 \x00\xa9\xa4\x00\x00\x01\x05\x00\x08\x00\x00\x00\x17'
assert(str(ofm) == s)
assert(raw(ofm) == s)
e = Ether(s)
e.show2()
e[OFPTFeaturesRequest].xid == 23