python3: Remove use of buffer()

python3 doesn't have buffer().  They are mostly needed by the fact that
python2.6's unpack doesn't take bytearray.  Replace them with
six.binary_type or remove them where not needed.

Signed-off-by: IWAMOTO Toshihiro <iwamoto@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
IWAMOTO Toshihiro 2015-06-24 18:47:03 +09:00 committed by FUJITA Tomonori
parent 267bcda006
commit 79ad56e682
12 changed files with 155 additions and 129 deletions

View File

@ -174,7 +174,7 @@ class _Value(object):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
values = struct.unpack_from(cls._VALUE_PACK_STR, buffer(buf)) values = struct.unpack_from(cls._VALUE_PACK_STR, six.binary_type(buf))
return dict(zip(cls._VALUE_FIELDS, values)) return dict(zip(cls._VALUE_FIELDS, values))
def serialize_value(self): def serialize_value(self):
@ -623,7 +623,7 @@ class _RouteDistinguisher(StringifyMixin, _TypeDisp, _Value):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
assert len(buf) == 8 assert len(buf) == 8
(type_,) = struct.unpack_from(cls._PACK_STR, buffer(buf)) (type_,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
subcls = cls._lookup_type(type_) subcls = cls._lookup_type(type_)
return subcls(type_=type_, **subcls.parse_value(rest)) return subcls(type_=type_, **subcls.parse_value(rest))
@ -733,7 +733,7 @@ class _AddrPrefix(StringifyMixin):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(length, ) = struct.unpack_from(cls._PACK_STR, buffer(buf)) (length, ) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
byte_length = (length + 7) // 8 byte_length = (length + 7) // 8
addr = cls._from_bin(rest[:byte_length]) addr = cls._from_bin(rest[:byte_length])
@ -800,7 +800,7 @@ class _LabelledAddrPrefix(_AddrPrefix):
@classmethod @classmethod
def _label_from_bin(cls, bin): def _label_from_bin(cls, bin):
(b1, b2, b3) = struct.unpack_from(cls._LABEL_PACK_STR, buffer(bin)) (b1, b2, b3) = struct.unpack_from(cls._LABEL_PACK_STR, six.binary_type(bin))
rest = bin[struct.calcsize(cls._LABEL_PACK_STR):] rest = bin[struct.calcsize(cls._LABEL_PACK_STR):]
return (b1 << 16) | (b2 << 8) | b3, rest return (b1 << 16) | (b2 << 8) | b3, rest
@ -1135,7 +1135,7 @@ class _OptParam(StringifyMixin, _TypeDisp, _Value):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(type_, length) = struct.unpack_from(cls._PACK_STR, buffer(buf)) (type_, length) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
value = bytes(rest[:length]) value = bytes(rest[:length])
rest = rest[length:] rest = rest[length:]
@ -1188,7 +1188,7 @@ class _OptParamCapability(_OptParam, _TypeDisp):
caps = [] caps = []
while len(buf) > 0: while len(buf) > 0:
(code, length) = struct.unpack_from(cls._CAP_HDR_PACK_STR, (code, length) = struct.unpack_from(cls._CAP_HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
value = buf[struct.calcsize(cls._CAP_HDR_PACK_STR):] value = buf[struct.calcsize(cls._CAP_HDR_PACK_STR):]
buf = buf[length + 2:] buf = buf[length + 2:]
kwargs = { kwargs = {
@ -1258,11 +1258,11 @@ class BGPOptParamCapabilityGracefulRestart(_OptParamCapability):
@classmethod @classmethod
def parse_cap_value(cls, buf): def parse_cap_value(cls, buf):
(restart, ) = struct.unpack_from(cls._CAP_PACK_STR, buffer(buf)) (restart, ) = struct.unpack_from(cls._CAP_PACK_STR, six.binary_type(buf))
buf = buf[2:] buf = buf[2:]
l = [] l = []
while len(buf) > 0: while len(buf) > 0:
l.append(struct.unpack_from("!HBB", buffer(buf))) l.append(struct.unpack_from("!HBB", buf))
buf = buf[4:] buf = buf[4:]
return {'flags': restart >> 12, 'time': restart & 0xfff, 'tuples': l} return {'flags': restart >> 12, 'time': restart & 0xfff, 'tuples': l}
@ -1289,7 +1289,7 @@ class BGPOptParamCapabilityFourOctetAsNumber(_OptParamCapability):
@classmethod @classmethod
def parse_cap_value(cls, buf): def parse_cap_value(cls, buf):
(as_number, ) = struct.unpack_from(cls._CAP_PACK_STR, buffer(buf)) (as_number, ) = struct.unpack_from(cls._CAP_PACK_STR, six.binary_type(buf))
return {'as_number': as_number} return {'as_number': as_number}
def serialize_cap_value(self): def serialize_cap_value(self):
@ -1311,7 +1311,7 @@ class BGPOptParamCapabilityMultiprotocol(_OptParamCapability):
@classmethod @classmethod
def parse_cap_value(cls, buf): def parse_cap_value(cls, buf):
(afi, reserved, safi,) = struct.unpack_from(cls._CAP_PACK_STR, (afi, reserved, safi,) = struct.unpack_from(cls._CAP_PACK_STR,
buffer(buf)) six.binary_type(buf))
return { return {
'afi': afi, 'afi': afi,
'reserved': reserved, 'reserved': reserved,
@ -1354,13 +1354,13 @@ class _PathAttribute(StringifyMixin, _TypeDisp, _Value):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(flags, type_) = struct.unpack_from(cls._PACK_STR, buffer(buf)) (flags, type_) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
if (flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) != 0: if (flags & BGP_ATTR_FLAG_EXTENDED_LENGTH) != 0:
len_pack_str = cls._PACK_STR_EXT_LEN len_pack_str = cls._PACK_STR_EXT_LEN
else: else:
len_pack_str = cls._PACK_STR_LEN len_pack_str = cls._PACK_STR_LEN
(length,) = struct.unpack_from(len_pack_str, buffer(rest)) (length,) = struct.unpack_from(len_pack_str, six.binary_type(rest))
rest = rest[struct.calcsize(len_pack_str):] rest = rest[struct.calcsize(len_pack_str):]
value = bytes(rest[:length]) value = bytes(rest[:length])
rest = rest[length:] rest = rest[length:]
@ -1469,7 +1469,7 @@ class _BGPPathAttributeAsPathCommon(_PathAttribute):
while buf: while buf:
(type_, num_as) = struct.unpack_from(cls._SEG_HDR_PACK_STR, (type_, num_as) = struct.unpack_from(cls._SEG_HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
if type_ is not cls._AS_SET and type_ is not cls._AS_SEQUENCE: if type_ is not cls._AS_SET and type_ is not cls._AS_SEQUENCE:
return False return False
@ -1494,12 +1494,12 @@ class _BGPPathAttributeAsPathCommon(_PathAttribute):
while buf: while buf:
(type_, num_as) = struct.unpack_from(cls._SEG_HDR_PACK_STR, (type_, num_as) = struct.unpack_from(cls._SEG_HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
buf = buf[struct.calcsize(cls._SEG_HDR_PACK_STR):] buf = buf[struct.calcsize(cls._SEG_HDR_PACK_STR):]
l = [] l = []
for i in range(0, num_as): for i in range(0, num_as):
(as_number,) = struct.unpack_from(as_pack_str, (as_number,) = struct.unpack_from(as_pack_str,
buffer(buf)) six.binary_type(buf))
buf = buf[struct.calcsize(as_pack_str):] buf = buf[struct.calcsize(as_pack_str):]
l.append(as_number) l.append(as_number)
if type_ == cls._AS_SET: if type_ == cls._AS_SET:
@ -1568,7 +1568,7 @@ class BGPPathAttributeNextHop(_PathAttribute):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
(ip_addr,) = struct.unpack_from(cls._VALUE_PACK_STR, buffer(buf)) (ip_addr,) = struct.unpack_from(cls._VALUE_PACK_STR, six.binary_type(buf))
return { return {
'value': addrconv.ipv4.bin_to_text(ip_addr), 'value': addrconv.ipv4.bin_to_text(ip_addr),
} }
@ -1621,7 +1621,7 @@ class _BGPPathAttributeAggregatorCommon(_PathAttribute):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
(as_number, addr) = struct.unpack_from(cls._VALUE_PACK_STR, (as_number, addr) = struct.unpack_from(cls._VALUE_PACK_STR,
buffer(buf)) six.binary_type(buf))
return { return {
'as_number': as_number, 'as_number': as_number,
'addr': addrconv.ipv4.bin_to_text(addr), 'addr': addrconv.ipv4.bin_to_text(addr),
@ -1669,7 +1669,8 @@ class BGPPathAttributeCommunities(_PathAttribute):
communities = [] communities = []
elem_size = struct.calcsize(cls._VALUE_PACK_STR) elem_size = struct.calcsize(cls._VALUE_PACK_STR)
while len(rest) >= elem_size: while len(rest) >= elem_size:
(comm, ) = struct.unpack_from(cls._VALUE_PACK_STR, buffer(rest)) (comm, ) = struct.unpack_from(cls._VALUE_PACK_STR,
six.binary_type(rest))
communities.append(comm) communities.append(comm)
rest = rest[elem_size:] rest = rest[elem_size:]
return { return {
@ -1730,7 +1731,8 @@ class BGPPathAttributeOriginatorId(_PathAttribute):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
(originator_id,) = struct.unpack_from(cls._VALUE_PACK_STR, buffer(buf)) (originator_id,) = struct.unpack_from(cls._VALUE_PACK_STR,
six.binary_type(buf))
return { return {
'value': addrconv.ipv4.bin_to_text(originator_id), 'value': addrconv.ipv4.bin_to_text(originator_id),
} }
@ -1762,7 +1764,7 @@ class BGPPathAttributeClusterList(_PathAttribute):
elem_size = struct.calcsize(cls._VALUE_PACK_STR) elem_size = struct.calcsize(cls._VALUE_PACK_STR)
while len(rest) >= elem_size: while len(rest) >= elem_size:
(cluster_id, ) = struct.unpack_from( (cluster_id, ) = struct.unpack_from(
cls._VALUE_PACK_STR, buffer(rest)) cls._VALUE_PACK_STR, six.binary_type(rest))
cluster_list.append(addrconv.ipv4.bin_to_text(cluster_id)) cluster_list.append(addrconv.ipv4.bin_to_text(cluster_id))
rest = rest[elem_size:] rest = rest[elem_size:]
return { return {
@ -1880,7 +1882,8 @@ class _ExtendedCommunity(StringifyMixin, _TypeDisp, _Value):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_high, payload) = struct.unpack_from(cls._PACK_STR, buffer(buf)) (type_high, payload) = struct.unpack_from(cls._PACK_STR,
six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
type_ = type_high & cls._TYPE_HIGH_MASK type_ = type_high & cls._TYPE_HIGH_MASK
subcls = cls._lookup_type(type_) subcls = cls._lookup_type(type_)
@ -2006,7 +2009,7 @@ class BGPPathAttributeMpReachNLRI(_PathAttribute):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
(afi, safi, next_hop_len,) = struct.unpack_from(cls._VALUE_PACK_STR, (afi, safi, next_hop_len,) = struct.unpack_from(cls._VALUE_PACK_STR,
buffer(buf)) six.binary_type(buf))
rest = buf[struct.calcsize(cls._VALUE_PACK_STR):] rest = buf[struct.calcsize(cls._VALUE_PACK_STR):]
next_hop_bin = rest[:next_hop_len] next_hop_bin = rest[:next_hop_len]
rest = rest[next_hop_len:] rest = rest[next_hop_len:]
@ -2103,7 +2106,7 @@ class BGPPathAttributeMpUnreachNLRI(_PathAttribute):
@classmethod @classmethod
def parse_value(cls, buf): def parse_value(cls, buf):
(afi, safi,) = struct.unpack_from(cls._VALUE_PACK_STR, buffer(buf)) (afi, safi,) = struct.unpack_from(cls._VALUE_PACK_STR, six.binary_type(buf))
binnlri = buf[struct.calcsize(cls._VALUE_PACK_STR):] binnlri = buf[struct.calcsize(cls._VALUE_PACK_STR):]
addr_cls = _get_addr_class(afi, safi) addr_cls = _get_addr_class(afi, safi)
nlri = [] nlri = []
@ -2169,7 +2172,7 @@ class BGPMessage(packet_base.PacketBase, _TypeDisp):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._HDR_LEN)) '%d < %d' % (len(buf), cls._HDR_LEN))
(marker, len_, type_) = struct.unpack_from(cls._HDR_PACK_STR, (marker, len_, type_) = struct.unpack_from(cls._HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
msglen = len_ msglen = len_
if len(buf) < msglen: if len(buf) < msglen:
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
@ -2248,7 +2251,7 @@ class BGPOpen(BGPMessage):
def parser(cls, buf): def parser(cls, buf):
(version, my_as, hold_time, (version, my_as, hold_time,
bgp_identifier, opt_param_len) = struct.unpack_from(cls._PACK_STR, bgp_identifier, opt_param_len) = struct.unpack_from(cls._PACK_STR,
buffer(buf)) six.binary_type(buf))
rest = buf[struct.calcsize(cls._PACK_STR):] rest = buf[struct.calcsize(cls._PACK_STR):]
binopts = rest[:opt_param_len] binopts = rest[:opt_param_len]
opt_param = [] opt_param = []
@ -2345,15 +2348,15 @@ class BGPUpdate(BGPMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
offset = 0 offset = 0
(withdrawn_routes_len,) = struct.unpack_from('!H', buffer(buf), offset) buf = six.binary_type(buf)
binroutes = buffer(buf[offset + 2: (withdrawn_routes_len,) = struct.unpack_from('!H', buf, offset)
offset + 2 + withdrawn_routes_len]) binroutes = buf[offset + 2:
offset + 2 + withdrawn_routes_len]
offset += 2 + withdrawn_routes_len offset += 2 + withdrawn_routes_len
(total_path_attribute_len,) = struct.unpack_from('!H', buffer(buf), (total_path_attribute_len,) = struct.unpack_from('!H', buf, offset)
offset) binpathattrs = buf[offset + 2:
binpathattrs = buffer(buf[offset + 2: offset + 2 + total_path_attribute_len]
offset + 2 + total_path_attribute_len]) binnlri = buf[offset + 2 + total_path_attribute_len:]
binnlri = buffer(buf[offset + 2 + total_path_attribute_len:])
withdrawn_routes = [] withdrawn_routes = []
while binroutes: while binroutes:
r, binroutes = BGPWithdrawnRoute.parser(binroutes) r, binroutes = BGPWithdrawnRoute.parser(binroutes)
@ -2507,7 +2510,7 @@ class BGPNotification(BGPMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(error_code, error_subcode,) = struct.unpack_from(cls._PACK_STR, (error_code, error_subcode,) = struct.unpack_from(cls._PACK_STR,
buffer(buf)) six.binary_type(buf))
data = bytes(buf[2:]) data = bytes(buf[2:])
return { return {
"error_code": error_code, "error_code": error_code,
@ -2562,7 +2565,7 @@ class BGPRouteRefresh(BGPMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(afi, demarcation, safi,) = struct.unpack_from(cls._PACK_STR, (afi, demarcation, safi,) = struct.unpack_from(cls._PACK_STR,
buffer(buf)) six.binary_type(buf))
return { return {
"afi": afi, "afi": afi,
"safi": safi, "safi": safi,

View File

@ -17,11 +17,13 @@
BGP Monitoring Protocol draft-ietf-grow-bmp-07 BGP Monitoring Protocol draft-ietf-grow-bmp-07
""" """
import six
import struct
from ryu.lib.packet import packet_base from ryu.lib.packet import packet_base
from ryu.lib.packet import stream_parser from ryu.lib.packet import stream_parser
from ryu.lib.packet.bgp import BGPMessage from ryu.lib.packet.bgp import BGPMessage
from ryu.lib import addrconv from ryu.lib import addrconv
import struct
VERSION = 3 VERSION = 3
@ -132,7 +134,7 @@ class BMPMessage(packet_base.PacketBase, _TypeDisp):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._HDR_LEN)) '%d < %d' % (len(buf), cls._HDR_LEN))
(version, len_, type_) = struct.unpack_from(cls._HDR_PACK_STR, (version, len_, type_) = struct.unpack_from(cls._HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
return version, len_, type_ return version, len_, type_
@ -229,7 +231,7 @@ class BMPPeerMessage(BMPMessage):
(peer_type, peer_flags, peer_distinguisher, (peer_type, peer_flags, peer_distinguisher,
peer_address, peer_as, peer_bgp_id, peer_address, peer_as, peer_bgp_id,
timestamp1, timestamp2) = struct.unpack_from(cls._PEER_HDR_PACK_STR, timestamp1, timestamp2) = struct.unpack_from(cls._PEER_HDR_PACK_STR,
buffer(buf)) six.binary_type(buf))
rest = buf[struct.calcsize(cls._PEER_HDR_PACK_STR):] rest = buf[struct.calcsize(cls._PEER_HDR_PACK_STR):]
@ -239,11 +241,11 @@ class BMPPeerMessage(BMPMessage):
is_post_policy = False is_post_policy = False
if peer_flags & (1 << 7): if peer_flags & (1 << 7):
peer_address = addrconv.ipv6.bin_to_text(buffer(peer_address)) peer_address = addrconv.ipv6.bin_to_text(peer_address)
else: else:
peer_address = addrconv.ipv4.bin_to_text(buffer(peer_address[:4])) peer_address = addrconv.ipv4.bin_to_text(peer_address[:4])
peer_bgp_id = addrconv.ipv4.bin_to_text(buffer(peer_bgp_id)) peer_bgp_id = addrconv.ipv4.bin_to_text(peer_bgp_id)
timestamp = float(timestamp1) + timestamp2 * (10 ** -6) timestamp = float(timestamp1) + timestamp2 * (10 ** -6)
@ -385,7 +387,7 @@ class BMPStatisticsReport(BMPPeerMessage):
def parser(cls, buf): def parser(cls, buf):
kwargs, rest = super(BMPStatisticsReport, cls).parser(buf) kwargs, rest = super(BMPStatisticsReport, cls).parser(buf)
stats_count, = struct.unpack_from('!I', buffer(rest)) stats_count, = struct.unpack_from('!I', six.binary_type(rest))
buf = rest[struct.calcsize('!I'):] buf = rest[struct.calcsize('!I'):]
@ -395,7 +397,8 @@ class BMPStatisticsReport(BMPPeerMessage):
if len(buf) < cls._MIN_LEN: if len(buf) < cls._MIN_LEN:
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._MIN_LEN)) '%d < %d' % (len(buf), cls._MIN_LEN))
(type_, len_) = struct.unpack_from(cls._TLV_PACK_STR, buffer(buf)) (type_, len_) = struct.unpack_from(cls._TLV_PACK_STR,
six.binary_type(buf))
if len(buf) < (cls._MIN_LEN + len_): if len(buf) < (cls._MIN_LEN + len_):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
@ -410,10 +413,10 @@ class BMPStatisticsReport(BMPPeerMessage):
type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_PATH_LOOP or \ type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_PATH_LOOP or \
type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_ORIGINATOR_ID or \ type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_ORIGINATOR_ID or \
type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_CONFED_LOOP: type_ == BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_CONFED_LOOP:
value, = struct.unpack_from('!I', buffer(value)) value, = struct.unpack_from('!I', six.binary_type(value))
elif type_ == BMP_STAT_TYPE_ADJ_RIB_IN or \ elif type_ == BMP_STAT_TYPE_ADJ_RIB_IN or \
type_ == BMP_STAT_TYPE_LOC_RIB: type_ == BMP_STAT_TYPE_LOC_RIB:
value, = struct.unpack_from('!Q', buffer(value)) value, = struct.unpack_from('!Q', six.binary_type(value))
buf = buf[cls._MIN_LEN + len_:] buf = buf[cls._MIN_LEN + len_:]
@ -491,13 +494,13 @@ class BMPPeerDownNotification(BMPPeerMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
kwargs, buf = super(BMPPeerDownNotification, cls).parser(buf) kwargs, buf = super(BMPPeerDownNotification, cls).parser(buf)
reason, = struct.unpack_from('!B', buffer(buf)) reason, = struct.unpack_from('!B', six.binary_type(buf))
buf = buf[struct.calcsize('!B'):] buf = buf[struct.calcsize('!B'):]
if reason == BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION: if reason == BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION:
data, rest = BGPMessage.parser(buf) data, rest = BGPMessage.parser(buf)
elif reason == BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION: elif reason == BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION:
data = struct.unpack_from('!H', buffer(buf)) data = struct.unpack_from('!H', six.binary_type(buf))
elif reason == BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION: elif reason == BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION:
data, rest = BGPMessage.parser(buf) data, rest = BGPMessage.parser(buf)
elif reason == BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION: elif reason == BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION:
@ -591,9 +594,7 @@ class BMPPeerUpNotification(BMPPeerMessage):
kwargs, rest = super(BMPPeerUpNotification, cls).parser(buf) kwargs, rest = super(BMPPeerUpNotification, cls).parser(buf)
(local_address, local_port, (local_address, local_port,
remote_port) = struct.unpack_from(cls._PACK_STR, buffer(rest)) remote_port) = struct.unpack_from(cls._PACK_STR, six.binary_type(rest))
local_address = buffer(local_address)
if '.' in kwargs['peer_address']: if '.' in kwargs['peer_address']:
local_address = addrconv.ipv4.bin_to_text(local_address[:4]) local_address = addrconv.ipv4.bin_to_text(local_address[:4])
@ -665,7 +666,8 @@ class BMPInitiation(BMPMessage):
if len(buf) < cls._MIN_LEN: if len(buf) < cls._MIN_LEN:
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._MIN_LEN)) '%d < %d' % (len(buf), cls._MIN_LEN))
(type_, len_) = struct.unpack_from(cls._TLV_PACK_STR, buffer(buf)) (type_, len_) = struct.unpack_from(cls._TLV_PACK_STR,
six.binary_type(buf))
if len(buf) < (cls._MIN_LEN + len_): if len(buf) < (cls._MIN_LEN + len_):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
@ -728,7 +730,8 @@ class BMPTermination(BMPMessage):
if len(buf) < cls._MIN_LEN: if len(buf) < cls._MIN_LEN:
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._MIN_LEN)) '%d < %d' % (len(buf), cls._MIN_LEN))
(type_, len_) = struct.unpack_from(cls._TLV_PACK_STR, buffer(buf)) (type_, len_) = struct.unpack_from(cls._TLV_PACK_STR,
six.binary_type(buf))
if len(buf) < (cls._MIN_LEN + len_): if len(buf) < (cls._MIN_LEN + len_):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
@ -738,7 +741,7 @@ class BMPTermination(BMPMessage):
if type_ == BMP_TERM_TYPE_STRING: if type_ == BMP_TERM_TYPE_STRING:
value = value.decode('utf-8') value = value.decode('utf-8')
elif type_ == BMP_TERM_TYPE_REASON: elif type_ == BMP_TERM_TYPE_REASON:
value, = struct.unpack_from('!H', buffer(value)) value, = struct.unpack_from('!H', six.binary_type(value))
buf = buf[cls._MIN_LEN + len_:] buf = buf[cls._MIN_LEN + len_:]

View File

@ -17,6 +17,7 @@
RFC 2328 OSPF version 2 RFC 2328 OSPF version 2
""" """
import six
import struct import struct
try: try:
@ -151,7 +152,7 @@ class LSAHeader(StringifyMixin):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._HDR_LEN)) '%d < %d' % (len(buf), cls._HDR_LEN))
(ls_age, options, type_, id_, adv_router, ls_seqnum, checksum, (ls_age, options, type_, id_, adv_router, ls_seqnum, checksum,
length,) = struct.unpack_from(cls._HDR_PACK_STR, buffer(buf)) length,) = struct.unpack_from(cls._HDR_PACK_STR, six.binary_type(buf))
adv_router = addrconv.ipv4.bin_to_text(adv_router) adv_router = addrconv.ipv4.bin_to_text(adv_router)
rest = buf[cls._HDR_LEN:] rest = buf[cls._HDR_LEN:]
lsacls = LSA._lookup_type(type_) lsacls = LSA._lookup_type(type_)
@ -167,7 +168,7 @@ class LSAHeader(StringifyMixin):
} }
if issubclass(lsacls, OpaqueLSA): if issubclass(lsacls, OpaqueLSA):
(id_,) = struct.unpack_from('!I', buffer(id_)) (id_,) = struct.unpack_from('!I', id_)
value['opaque_type'] = (id_ & 0xff000000) >> 24 value['opaque_type'] = (id_ & 0xff000000) >> 24
value['opaque_id'] = (id_ & 0xffffff) value['opaque_id'] = (id_ & 0xffffff)
else: else:
@ -266,7 +267,7 @@ class RouterLSA(LSA):
link = buf[:cls._PACK_LEN] link = buf[:cls._PACK_LEN]
rest = buf[cls._PACK_LEN:] rest = buf[cls._PACK_LEN:]
(id_, data, type_, tos, metric) = \ (id_, data, type_, tos, metric) = \
struct.unpack_from(cls._PACK_STR, buffer(link)) struct.unpack_from(cls._PACK_STR, six.binary_type(link))
id_ = addrconv.ipv4.bin_to_text(id_) id_ = addrconv.ipv4.bin_to_text(id_)
data = addrconv.ipv4.bin_to_text(data) data = addrconv.ipv4.bin_to_text(data)
return cls(id_, data, type_, tos, metric), rest return cls(id_, data, type_, tos, metric), rest
@ -291,7 +292,8 @@ class RouterLSA(LSA):
links = [] links = []
hdr = buf[:cls._PACK_LEN] hdr = buf[:cls._PACK_LEN]
buf = buf[cls._PACK_LEN:] buf = buf[cls._PACK_LEN:]
(flags, padding, num) = struct.unpack_from(cls._PACK_STR, buffer(hdr)) (flags, padding, num) = struct.unpack_from(cls._PACK_STR,
six.binary_type(hdr))
while buf: while buf:
link, buf = cls.Link.parser(buf) link, buf = cls.Link.parser(buf)
links.append(link) links.append(link)
@ -331,13 +333,14 @@ class NetworkLSA(LSA):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._PACK_LEN)) '%d < %d' % (len(buf), cls._PACK_LEN))
binmask = buf[:cls._PACK_LEN] binmask = buf[:cls._PACK_LEN]
(mask,) = struct.unpack_from(cls._PACK_STR, buffer(binmask)) (mask,) = struct.unpack_from(cls._PACK_STR, six.binary_type(binmask))
mask = addrconv.ipv4.bin_to_text(mask) mask = addrconv.ipv4.bin_to_text(mask)
buf = buf[cls._PACK_LEN:] buf = buf[cls._PACK_LEN:]
routers = [] routers = []
while buf: while buf:
binrouter = buf[:cls._PACK_LEN] binrouter = buf[:cls._PACK_LEN]
(router,) = struct.unpack_from(cls._PACK_STR, buffer(binrouter)) (router,) = struct.unpack_from(cls._PACK_STR,
six.binary_type(binrouter))
router = addrconv.ipv4.bin_to_text(router) router = addrconv.ipv4.bin_to_text(router)
routers.append(router) routers.append(router)
buf = buf[cls._PACK_LEN:] buf = buf[cls._PACK_LEN:]
@ -375,8 +378,8 @@ class SummaryLSA(LSA):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls_PACK_LEN)) '%d < %d' % (len(buf), cls_PACK_LEN))
buf = buf[:cls._PACK_LEN] buf = buf[:cls._PACK_LEN]
(mask, tos, metric_fst, metric_lst) = struct.unpack_from(cls._PACK_STR, (mask, tos, metric_fst, metric_lst) = struct.unpack_from(
buffer(buf)) cls._PACK_STR, six.binary_type(buf))
mask = addrconv.ipv4.bin_to_text(mask) mask = addrconv.ipv4.bin_to_text(mask)
metric = metric_fst << 16 | (metric_lst & 0xffff) metric = metric_fst << 16 | (metric_lst & 0xffff)
return { return {
@ -419,7 +422,7 @@ class ASExternalLSA(LSA):
ext_nw = buf[:cls._PACK_LEN] ext_nw = buf[:cls._PACK_LEN]
rest = buf[cls._PACK_LEN:] rest = buf[cls._PACK_LEN:]
(mask, flags, metric_fst, metric_lst, fwd_addr, (mask, flags, metric_fst, metric_lst, fwd_addr,
tag) = struct.unpack_from(cls._PACK_STR, buffer(ext_nw)) tag) = struct.unpack_from(cls._PACK_STR, six.binary_type(ext_nw))
mask = addrconv.ipv4.bin_to_text(mask) mask = addrconv.ipv4.bin_to_text(mask)
metric = metric_fst << 16 | (metric_lst & 0xffff) metric = metric_fst << 16 | (metric_lst & 0xffff)
fwd_addr = addrconv.ipv4.bin_to_text(fwd_addr) fwd_addr = addrconv.ipv4.bin_to_text(fwd_addr)
@ -548,7 +551,7 @@ class OpaqueBody(StringifyMixin, _TypeDisp):
class ExtendedPrefixOpaqueBody(OpaqueBody): class ExtendedPrefixOpaqueBody(OpaqueBody):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
buf = buffer(buf) buf = six.binary_type(buf)
tlvs = [] tlvs = []
while buf: while buf:
(type_, length) = struct.unpack_from('!HH', buf) (type_, length) = struct.unpack_from('!HH', buf)
@ -567,7 +570,7 @@ class ExtendedPrefixOpaqueBody(OpaqueBody):
class ExtendedLinkOpaqueBody(OpaqueBody): class ExtendedLinkOpaqueBody(OpaqueBody):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
buf = buffer(buf) buf = six.binary_type(buf)
tlvs = [] tlvs = []
while buf: while buf:
(type_, length) = struct.unpack_from('!HH', buf) (type_, length) = struct.unpack_from('!HH', buf)
@ -657,7 +660,8 @@ class OSPFMessage(packet_base.PacketBase, _TypeDisp):
raise stream_parser.StreamParser.TooSmallException( raise stream_parser.StreamParser.TooSmallException(
'%d < %d' % (len(buf), cls._HDR_LEN)) '%d < %d' % (len(buf), cls._HDR_LEN))
(version, type_, length, router_id, area_id, checksum, au_type, (version, type_, length, router_id, area_id, checksum, au_type,
authentication) = struct.unpack_from(cls._HDR_PACK_STR, buffer(buf)) authentication) = struct.unpack_from(cls._HDR_PACK_STR,
six.binary_type(buf))
# Exclude checksum and authentication field for checksum validation. # Exclude checksum and authentication field for checksum validation.
if packet_utils.checksum(buf[:12] + buf[14:16] + buf[cls._HDR_LEN:]) \ if packet_utils.checksum(buf[:12] + buf[14:16] + buf[cls._HDR_LEN:]) \
@ -731,7 +735,7 @@ class OSPFHello(OSPFMessage):
def parser(cls, buf): def parser(cls, buf):
(mask, hello_interval, options, priority, dead_interval, (mask, hello_interval, options, priority, dead_interval,
designated_router, backup_router) = struct.unpack_from(cls._PACK_STR, designated_router, backup_router) = struct.unpack_from(cls._PACK_STR,
buffer(buf)) six.binary_type(buf))
mask = addrconv.ipv4.bin_to_text(mask) mask = addrconv.ipv4.bin_to_text(mask)
designated_router = addrconv.ipv4.bin_to_text(designated_router) designated_router = addrconv.ipv4.bin_to_text(designated_router)
backup_router = addrconv.ipv4.bin_to_text(backup_router) backup_router = addrconv.ipv4.bin_to_text(backup_router)
@ -739,7 +743,7 @@ class OSPFHello(OSPFMessage):
binneighbors = buf[cls._PACK_LEN:len(buf)] binneighbors = buf[cls._PACK_LEN:len(buf)]
while binneighbors: while binneighbors:
n = binneighbors[:4] n = binneighbors[:4]
n = addrconv.ipv4.bin_to_text(buffer(n)) n = addrconv.ipv4.bin_to_text(six.binary_type(n))
binneighbors = binneighbors[4:] binneighbors = binneighbors[4:]
neighbors.append(n) neighbors.append(n)
return { return {
@ -793,7 +797,7 @@ class OSPFDBDesc(OSPFMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
(mtu, options, flags, (mtu, options, flags,
sequence_number) = struct.unpack_from(cls._PACK_STR, buffer(buf)) sequence_number) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf))
i_flag = (flags >> 2) & 0x1 i_flag = (flags >> 2) & 0x1
m_flag = (flags >> 1) & 0x1 m_flag = (flags >> 1) & 0x1
ms_flag = flags & 0x1 ms_flag = flags & 0x1
@ -848,7 +852,7 @@ class OSPFLSReq(OSPFMessage):
link = buf[:cls._PACK_LEN] link = buf[:cls._PACK_LEN]
rest = buf[cls._PACK_LEN:] rest = buf[cls._PACK_LEN:]
(type_, id_, adv_router) = struct.unpack_from(cls._PACK_STR, (type_, id_, adv_router) = struct.unpack_from(cls._PACK_STR,
buffer(link)) six.binary_type(link))
id_ = addrconv.ipv4.bin_to_text(id_) id_ = addrconv.ipv4.bin_to_text(id_)
adv_router = addrconv.ipv4.bin_to_text(adv_router) adv_router = addrconv.ipv4.bin_to_text(adv_router)
return cls(type_, id_, adv_router), rest return cls(type_, id_, adv_router), rest
@ -899,7 +903,7 @@ class OSPFLSUpd(OSPFMessage):
@classmethod @classmethod
def parser(cls, buf): def parser(cls, buf):
binnum = buf[:cls._PACK_LEN] binnum = buf[:cls._PACK_LEN]
(num,) = struct.unpack_from(cls._PACK_STR, buffer(binnum)) (num,) = struct.unpack_from(cls._PACK_STR, six.binary_type(binnum))
buf = buf[cls._PACK_LEN:] buf = buf[cls._PACK_LEN:]
lsas = [] lsas = []

View File

@ -14,6 +14,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import six
import base64 import base64
import collections import collections
import logging import logging
@ -29,11 +31,16 @@ from . import ofproto_common
LOG = logging.getLogger('ryu.ofproto.ofproto_parser') LOG = logging.getLogger('ryu.ofproto.ofproto_parser')
# This is merely for API compatibility on python2
if six.PY3:
buffer = bytes
def header(buf): def header(buf):
assert len(buf) >= ofproto_common.OFP_HEADER_SIZE assert len(buf) >= ofproto_common.OFP_HEADER_SIZE
# LOG.debug('len %d bufsize %d', len(buf), ofproto.OFP_HEADER_SIZE) # LOG.debug('len %d bufsize %d', len(buf), ofproto.OFP_HEADER_SIZE)
return struct.unpack_from(ofproto_common.OFP_HEADER_PACK_STR, buffer(buf)) return struct.unpack_from(ofproto_common.OFP_HEADER_PACK_STR,
six.binary_type(buf))
_MSG_PARSERS = {} _MSG_PARSERS = {}

View File

@ -131,7 +131,7 @@ class OFPErrorMsg(MsgBase):
@classmethod @classmethod
def parser(cls, datapath, version, msg_type, msg_len, xid, buf): def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
type_, = struct.unpack_from('!H', buffer(buf), type_, = struct.unpack_from('!H', six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
if type_ == ofproto.OFPET_EXPERIMENTER: if type_ == ofproto.OFPET_EXPERIMENTER:
return OFPErrorExperimenterMsg.parser(datapath, version, msg_type, return OFPErrorExperimenterMsg.parser(datapath, version, msg_type,

View File

@ -229,7 +229,7 @@ class OFPErrorMsg(MsgBase):
@classmethod @classmethod
def parser(cls, datapath, version, msg_type, msg_len, xid, buf): def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
type_, = struct.unpack_from('!H', buffer(buf), type_, = struct.unpack_from('!H', six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
if type_ == ofproto.OFPET_EXPERIMENTER: if type_ == ofproto.OFPET_EXPERIMENTER:
return OFPErrorExperimenterMsg.parser(datapath, version, msg_type, return OFPErrorExperimenterMsg.parser(datapath, version, msg_type,
@ -3615,7 +3615,7 @@ class OFPMultipartReply(MsgBase):
@classmethod @classmethod
def parser(cls, datapath, version, msg_type, msg_len, xid, buf): def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
type_, flags = struct.unpack_from( type_, flags = struct.unpack_from(
ofproto.OFP_MULTIPART_REPLY_PACK_STR, buffer(buf), ofproto.OFP_MULTIPART_REPLY_PACK_STR, six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
stats_type_cls = cls._STATS_MSG_TYPES.get(type_) stats_type_cls = cls._STATS_MSG_TYPES.get(type_)
msg = super(OFPMultipartReply, stats_type_cls).parser( msg = super(OFPMultipartReply, stats_type_cls).parser(
@ -4966,7 +4966,7 @@ class OFPTableFeatureProp(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_, length,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (type_, length,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
bin_prop = buf[struct.calcsize(cls._PACK_STR):length] bin_prop = buf[struct.calcsize(cls._PACK_STR):length]
rest = buf[utils.round_up(length, 8):] rest = buf[utils.round_up(length, 8):]
try: try:
@ -5015,7 +5015,7 @@ class OFPInstructionId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_, len_,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (type_, len_,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
rest = buf[len_:] rest = buf[len_:]
return cls(type_=type_, len_=len_), rest return cls(type_=type_, len_=len_), rest
@ -5067,7 +5067,8 @@ class OFPTableFeaturePropNextTables(OFPTableFeatureProp):
rest = buf rest = buf
ids = [] ids = []
while rest: while rest:
(i,) = struct.unpack_from(cls._TABLE_ID_PACK_STR, buffer(rest), 0) (i,) = struct.unpack_from(cls._TABLE_ID_PACK_STR,
six.binary_type(rest), 0)
rest = rest[struct.calcsize(cls._TABLE_ID_PACK_STR):] rest = rest[struct.calcsize(cls._TABLE_ID_PACK_STR):]
ids.append(i) ids.append(i)
return { return {
@ -5102,7 +5103,7 @@ class OFPActionId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_, len_,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (type_, len_,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
rest = buf[len_:] rest = buf[len_:]
return cls(type_=type_, len_=len_), rest return cls(type_=type_, len_=len_), rest
@ -5179,7 +5180,7 @@ class OFPOxmId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(oxm,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (oxm,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
# oxm (32 bit) == class (16) | field (7) | hasmask (1) | length (8) # oxm (32 bit) == class (16) | field (7) | hasmask (1) | length (8)
# in case of experimenter OXMs, another 32 bit value # in case of experimenter OXMs, another 32 bit value
# (experimenter id) follows. # (experimenter id) follows.
@ -5190,7 +5191,7 @@ class OFPOxmId(StringifyMixin):
class_ = oxm >> (7 + 1 + 8) class_ = oxm >> (7 + 1 + 8)
if class_ == ofproto.OFPXMC_EXPERIMENTER: if class_ == ofproto.OFPXMC_EXPERIMENTER:
(exp_id,) = struct.unpack_from(cls._EXPERIMENTER_ID_PACK_STR, (exp_id,) = struct.unpack_from(cls._EXPERIMENTER_ID_PACK_STR,
buffer(rest), 0) six.binary_type(rest), 0)
rest = rest[struct.calcsize(cls._EXPERIMENTER_ID_PACK_STR):] rest = rest[struct.calcsize(cls._EXPERIMENTER_ID_PACK_STR):]
subcls = OFPExperimenterOxmId subcls = OFPExperimenterOxmId
return subcls(type_=type_, exp_id=exp_id, hasmask=hasmask, return subcls(type_=type_, exp_id=exp_id, hasmask=hasmask,

View File

@ -249,7 +249,7 @@ class OFPErrorMsg(MsgBase):
@classmethod @classmethod
def parser(cls, datapath, version, msg_type, msg_len, xid, buf): def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
type_, = struct.unpack_from('!H', buffer(buf), type_, = struct.unpack_from('!H', six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
if type_ == ofproto.OFPET_EXPERIMENTER: if type_ == ofproto.OFPET_EXPERIMENTER:
return OFPErrorExperimenterMsg.parser(datapath, version, msg_type, return OFPErrorExperimenterMsg.parser(datapath, version, msg_type,
@ -2061,7 +2061,7 @@ class OFPMultipartReply(MsgBase):
@classmethod @classmethod
def parser(cls, datapath, version, msg_type, msg_len, xid, buf): def parser(cls, datapath, version, msg_type, msg_len, xid, buf):
type_, flags = struct.unpack_from( type_, flags = struct.unpack_from(
ofproto.OFP_MULTIPART_REPLY_PACK_STR, buffer(buf), ofproto.OFP_MULTIPART_REPLY_PACK_STR, six.binary_type(buf),
ofproto.OFP_HEADER_SIZE) ofproto.OFP_HEADER_SIZE)
stats_type_cls = cls._STATS_MSG_TYPES.get(type_) stats_type_cls = cls._STATS_MSG_TYPES.get(type_)
msg = super(OFPMultipartReply, stats_type_cls).parser( msg = super(OFPMultipartReply, stats_type_cls).parser(
@ -2264,7 +2264,7 @@ class OFPInstructionId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_, len_,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (type_, len_,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
rest = buf[len_:] rest = buf[len_:]
return cls(type_=type_, len_=len_), rest return cls(type_=type_, len_=len_), rest
@ -2314,7 +2314,7 @@ class OFPActionId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(type_, len_,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (type_, len_,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
rest = buf[len_:] rest = buf[len_:]
return cls(type_=type_, len_=len_), rest return cls(type_=type_, len_=len_), rest
@ -2367,7 +2367,7 @@ class OFPTableFeaturePropNextTables(OFPTableFeatureProp):
rest = cls.get_rest(buf) rest = cls.get_rest(buf)
ids = [] ids = []
while rest: while rest:
(i,) = struct.unpack_from(cls._TABLE_ID_PACK_STR, buffer(rest), 0) (i,) = struct.unpack_from(cls._TABLE_ID_PACK_STR, six.binary_type(rest), 0)
rest = rest[struct.calcsize(cls._TABLE_ID_PACK_STR):] rest = rest[struct.calcsize(cls._TABLE_ID_PACK_STR):]
ids.append(i) ids.append(i)
return cls(table_ids=ids) return cls(table_ids=ids)
@ -2418,7 +2418,7 @@ class OFPOxmId(StringifyMixin):
@classmethod @classmethod
def parse(cls, buf): def parse(cls, buf):
(oxm,) = struct.unpack_from(cls._PACK_STR, buffer(buf), 0) (oxm,) = struct.unpack_from(cls._PACK_STR, six.binary_type(buf), 0)
# oxm (32 bit) == class (16) | field (7) | hasmask (1) | length (8) # oxm (32 bit) == class (16) | field (7) | hasmask (1) | length (8)
# in case of experimenter OXMs, another 32 bit value # in case of experimenter OXMs, another 32 bit value
# (experimenter id) follows. # (experimenter id) follows.
@ -2429,7 +2429,7 @@ class OFPOxmId(StringifyMixin):
class_ = oxm >> (7 + 1 + 8) class_ = oxm >> (7 + 1 + 8)
if class_ == ofproto.OFPXMC_EXPERIMENTER: if class_ == ofproto.OFPXMC_EXPERIMENTER:
(exp_id,) = struct.unpack_from(cls._EXPERIMENTER_ID_PACK_STR, (exp_id,) = struct.unpack_from(cls._EXPERIMENTER_ID_PACK_STR,
buffer(rest), 0) six.binary_type(rest), 0)
rest = rest[struct.calcsize(cls._EXPERIMENTER_ID_PACK_STR):] rest = rest[struct.calcsize(cls._EXPERIMENTER_ID_PACK_STR):]
subcls = OFPExperimenterOxmId subcls = OFPExperimenterOxmId
return subcls(type_=type_, exp_id=exp_id, hasmask=hasmask, return subcls(type_=type_, exp_id=exp_id, hasmask=hasmask,

View File

@ -84,7 +84,7 @@ class Test_packet_in_filter(unittest.TestCase):
def test_pkt_in_filter_truncated(self): def test_pkt_in_filter_truncated(self):
datapath = ProtocolDesc(version=ofproto_v1_3.OFP_VERSION) datapath = ProtocolDesc(version=ofproto_v1_3.OFP_VERSION)
truncated_data = buffer('') truncated_data = ''
pkt_in = ofproto_v1_3_parser.OFPPacketIn(datapath, pkt_in = ofproto_v1_3_parser.OFPPacketIn(datapath,
data=truncated_data) data=truncated_data)
ev = ofp_event.EventOFPPacketIn(pkt_in) ev = ofp_event.EventOFPPacketIn(pkt_in)

View File

@ -14,6 +14,7 @@
# limitations under the License. # limitations under the License.
import unittest import unittest
import six
import struct import struct
from nose.tools import ok_, eq_ from nose.tools import ok_, eq_
@ -41,7 +42,7 @@ class TestMsgPackInto(unittest.TestCase):
pack_utils.msg_pack_into(fmt, buf, offset, arg1, arg2) pack_utils.msg_pack_into(fmt, buf, offset, arg1, arg2)
check_offset = len(buf) - len_ check_offset = len(buf) - len_
res = struct.unpack_from(fmt, buffer(buf), check_offset) res = struct.unpack_from(fmt, six.binary_type(buf), check_offset)
eq_(arg1, res[0]) eq_(arg1, res[0])
eq_(arg2, res[1]) eq_(arg2, res[1])

View File

@ -15,6 +15,8 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4 # vim: tabstop=4 shiftwidth=4 softtabstop=4
import six
import binascii import binascii
import unittest import unittest
from nose.tools import * from nose.tools import *
@ -27,6 +29,9 @@ from ryu.ofproto import ofproto_v1_0, ofproto_v1_0_parser
import logging import logging
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
if six.PY3:
buffer = bytes
class TestOfproto_Parser(unittest.TestCase): class TestOfproto_Parser(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -17,6 +17,7 @@
import unittest import unittest
import logging import logging
import six
import struct import struct
import inspect import inspect
@ -77,7 +78,7 @@ class Test_icmpv6_header(unittest.TestCase):
prev = ipv6(6, 0, 0, 4, 58, 255, src_ipv6, dst_ipv6) prev = ipv6(6, 0, 0, 4, 58, 255, src_ipv6, dst_ipv6)
buf = self.icmp.serialize(bytearray(), prev) buf = self.icmp.serialize(bytearray(), prev)
(type_, code, csum) = struct.unpack(self.icmp._PACK_STR, buffer(buf)) (type_, code, csum) = struct.unpack(self.icmp._PACK_STR, six.binary_type(buf))
eq_(type_, self.type_) eq_(type_, self.type_)
eq_(code, self.code) eq_(code, self.code)
@ -153,7 +154,7 @@ class Test_icmpv6_echo_request(unittest.TestCase):
echo = icmpv6.echo(self.id_, self.seq, echo_data) echo = icmpv6.echo(self.id_, self.seq, echo_data)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, echo) icmp = icmpv6.icmpv6(self.type_, self.code, 0, echo)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
(id_, seq) = struct.unpack_from(echo._PACK_STR, buf, icmp._MIN_LEN) (id_, seq) = struct.unpack_from(echo._PACK_STR, buf, icmp._MIN_LEN)
@ -303,7 +304,7 @@ class Test_icmpv6_neighbor_solicit(unittest.TestCase):
nd_csum = icmpv6_csum(prev, self.buf) nd_csum = icmpv6_csum(prev, self.buf)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd) icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
(res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN) (res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN)
@ -323,7 +324,7 @@ class Test_icmpv6_neighbor_solicit(unittest.TestCase):
nd_csum = icmpv6_csum(prev, self.buf + self.data) nd_csum = icmpv6_csum(prev, self.buf + self.data)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd) icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
(res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN) (res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN)
@ -446,7 +447,7 @@ class Test_icmpv6_neighbor_advert(Test_icmpv6_neighbor_solicit):
nd_csum = icmpv6_csum(prev, self.buf + self.data) nd_csum = icmpv6_csum(prev, self.buf + self.data)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd) icmp = icmpv6.icmpv6(self.type_, self.code, 0, nd)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
(res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN) (res, dst) = struct.unpack_from(nd._PACK_STR, buf, icmp._MIN_LEN)
@ -591,7 +592,7 @@ class Test_icmpv6_router_solicit(unittest.TestCase):
rs_csum = icmpv6_csum(prev, self.buf) rs_csum = icmpv6_csum(prev, self.buf)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, rs) icmp = icmpv6.icmpv6(self.type_, self.code, 0, rs)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
res = struct.unpack_from(rs._PACK_STR, buf, icmp._MIN_LEN) res = struct.unpack_from(rs._PACK_STR, buf, icmp._MIN_LEN)
@ -610,7 +611,7 @@ class Test_icmpv6_router_solicit(unittest.TestCase):
rs_csum = icmpv6_csum(prev, self.buf + self.data) rs_csum = icmpv6_csum(prev, self.buf + self.data)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, rs) icmp = icmpv6.icmpv6(self.type_, self.code, 0, rs)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
res = struct.unpack_from(rs._PACK_STR, buf, icmp._MIN_LEN) res = struct.unpack_from(rs._PACK_STR, buf, icmp._MIN_LEN)
@ -1007,7 +1008,7 @@ class Test_icmpv6_membership_query(unittest.TestCase):
mld = icmpv6.mld(self.maxresp, self.address) mld = icmpv6.mld(self.maxresp, self.address)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, mld) icmp = icmpv6.icmpv6(self.type_, self.code, 0, mld)
buf = buffer(icmp.serialize(bytearray(), prev)) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, buf, 0)
(maxresp, address) = struct.unpack_from( (maxresp, address) = struct.unpack_from(
@ -1491,19 +1492,19 @@ class Test_mldv2_report(unittest.TestCase):
mld_csum = icmpv6_csum(prev, self.buf) mld_csum = icmpv6_csum(prev, self.buf)
icmp = icmpv6.icmpv6(self.type_, self.code, 0, self.mld) icmp = icmpv6.icmpv6(self.type_, self.code, 0, self.mld)
buf = icmp.serialize(bytearray(), prev) buf = six.binary_type(icmp.serialize(bytearray(), prev))
(type_, code, csum) = struct.unpack_from(icmp._PACK_STR, str(buf)) (type_, code, csum) = struct.unpack_from(icmp._PACK_STR, str(buf))
(record_num, ) = struct.unpack_from( (record_num, ) = struct.unpack_from(
self.mld._PACK_STR, str(buf), icmp._MIN_LEN) self.mld._PACK_STR, str(buf), icmp._MIN_LEN)
offset = icmp._MIN_LEN + self.mld._MIN_LEN offset = icmp._MIN_LEN + self.mld._MIN_LEN
rec1 = icmpv6.mldv2_report_group.parser(buffer(buf[offset:])) rec1 = icmpv6.mldv2_report_group.parser(buf[offset:])
offset += len(rec1) offset += len(rec1)
rec2 = icmpv6.mldv2_report_group.parser(buffer(buf[offset:])) rec2 = icmpv6.mldv2_report_group.parser(buf[offset:])
offset += len(rec2) offset += len(rec2)
rec3 = icmpv6.mldv2_report_group.parser(buffer(buf[offset:])) rec3 = icmpv6.mldv2_report_group.parser(buf[offset:])
offset += len(rec3) offset += len(rec3)
rec4 = icmpv6.mldv2_report_group.parser(buffer(buf[offset:])) rec4 = icmpv6.mldv2_report_group.parser(buf[offset:])
eq_(type_, self.type_) eq_(type_, self.type_)
eq_(code, self.code) eq_(code, self.code)
@ -1800,7 +1801,7 @@ class Test_mldv2_report_group(unittest.TestCase):
def test_serialize(self): def test_serialize(self):
buf = self.mld.serialize() buf = self.mld.serialize()
res = struct.unpack_from( res = struct.unpack_from(
icmpv6.mldv2_report_group._PACK_STR, buffer(buf)) icmpv6.mldv2_report_group._PACK_STR, six.binary_type(buf))
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
@ -1811,9 +1812,9 @@ class Test_mldv2_report_group(unittest.TestCase):
self.setUp_with_srcs() self.setUp_with_srcs()
buf = self.mld.serialize() buf = self.mld.serialize()
res = struct.unpack_from( res = struct.unpack_from(
icmpv6.mldv2_report_group._PACK_STR, buffer(buf)) icmpv6.mldv2_report_group._PACK_STR, six.binary_type(buf))
(src1, src2, src3) = struct.unpack_from( (src1, src2, src3) = struct.unpack_from(
'16s16s16s', buffer(buf), icmpv6.mldv2_report_group._MIN_LEN) '16s16s16s', six.binary_type(buf), icmpv6.mldv2_report_group._MIN_LEN)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
eq_(res[2], self.num) eq_(res[2], self.num)
@ -1826,9 +1827,9 @@ class Test_mldv2_report_group(unittest.TestCase):
self.setUp_with_aux() self.setUp_with_aux()
buf = self.mld.serialize() buf = self.mld.serialize()
res = struct.unpack_from( res = struct.unpack_from(
icmpv6.mldv2_report_group._PACK_STR, buffer(buf)) icmpv6.mldv2_report_group._PACK_STR, six.binary_type(buf))
(aux, ) = struct.unpack_from( (aux, ) = struct.unpack_from(
'%ds' % (self.aux_len * 4), buffer(buf), '%ds' % (self.aux_len * 4), six.binary_type(buf),
icmpv6.mldv2_report_group._MIN_LEN) icmpv6.mldv2_report_group._MIN_LEN)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
@ -1840,11 +1841,11 @@ class Test_mldv2_report_group(unittest.TestCase):
self.setUp_with_srcs_and_aux() self.setUp_with_srcs_and_aux()
buf = self.mld.serialize() buf = self.mld.serialize()
res = struct.unpack_from( res = struct.unpack_from(
icmpv6.mldv2_report_group._PACK_STR, buffer(buf)) icmpv6.mldv2_report_group._PACK_STR, six.binary_type(buf))
(src1, src2, src3) = struct.unpack_from( (src1, src2, src3) = struct.unpack_from(
'16s16s16s', buffer(buf), icmpv6.mldv2_report_group._MIN_LEN) '16s16s16s', six.binary_type(buf), icmpv6.mldv2_report_group._MIN_LEN)
(aux, ) = struct.unpack_from( (aux, ) = struct.unpack_from(
'%ds' % (self.aux_len * 4), buffer(buf), '%ds' % (self.aux_len * 4), six.binary_type(buf),
icmpv6.mldv2_report_group._MIN_LEN + 16 * 3) icmpv6.mldv2_report_group._MIN_LEN + 16 * 3)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)

View File

@ -18,6 +18,7 @@
import unittest import unittest
import inspect import inspect
import logging import logging
import six
from struct import pack, unpack_from, pack_into from struct import pack, unpack_from, pack_into
from nose.tools import ok_, eq_, raises from nose.tools import ok_, eq_, raises
@ -86,7 +87,7 @@ class Test_igmp(unittest.TestCase):
prev = None prev = None
buf = self.g.serialize(data, prev) buf = self.g.serialize(data, prev)
res = unpack_from(igmp._PACK_STR, buffer(buf)) res = unpack_from(igmp._PACK_STR, six.binary_type(buf))
eq_(res[0], self.msgtype) eq_(res[0], self.msgtype)
eq_(res[1], self.maxresp) eq_(res[1], self.maxresp)
@ -253,7 +254,7 @@ class Test_igmpv3_query(unittest.TestCase):
prev = None prev = None
buf = self.g.serialize(data, prev) buf = self.g.serialize(data, prev)
res = unpack_from(igmpv3_query._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_query._PACK_STR, six.binary_type(buf))
eq_(res[0], self.msgtype) eq_(res[0], self.msgtype)
eq_(res[1], self.maxresp) eq_(res[1], self.maxresp)
@ -269,8 +270,8 @@ class Test_igmpv3_query(unittest.TestCase):
prev = None prev = None
buf = self.g.serialize(data, prev) buf = self.g.serialize(data, prev)
res = unpack_from(igmpv3_query._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_query._PACK_STR, six.binary_type(buf))
(src1, src2, src3) = unpack_from('4s4s4s', buffer(buf), (src1, src2, src3) = unpack_from('4s4s4s', six.binary_type(buf),
igmpv3_query._MIN_LEN) igmpv3_query._MIN_LEN)
eq_(res[0], self.msgtype) eq_(res[0], self.msgtype)
@ -515,7 +516,7 @@ class Test_igmpv3_report(unittest.TestCase):
prev = None prev = None
buf = self.g.serialize(data, prev) buf = self.g.serialize(data, prev)
res = unpack_from(igmpv3_report._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report._PACK_STR, six.binary_type(buf))
eq_(res[0], self.msgtype) eq_(res[0], self.msgtype)
eq_(res[1], checksum(self.buf)) eq_(res[1], checksum(self.buf))
@ -525,17 +526,17 @@ class Test_igmpv3_report(unittest.TestCase):
self.setUp_with_records() self.setUp_with_records()
data = bytearray() data = bytearray()
prev = None prev = None
buf = self.g.serialize(data, prev) buf = six.binary_type(self.g.serialize(data, prev))
res = unpack_from(igmpv3_report._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report._PACK_STR, buf)
offset = igmpv3_report._MIN_LEN offset = igmpv3_report._MIN_LEN
rec1 = igmpv3_report_group.parser(buffer(buf[offset:])) rec1 = igmpv3_report_group.parser(buf[offset:])
offset += len(rec1) offset += len(rec1)
rec2 = igmpv3_report_group.parser(buffer(buf[offset:])) rec2 = igmpv3_report_group.parser(buf[offset:])
offset += len(rec2) offset += len(rec2)
rec3 = igmpv3_report_group.parser(buffer(buf[offset:])) rec3 = igmpv3_report_group.parser(buf[offset:])
offset += len(rec3) offset += len(rec3)
rec4 = igmpv3_report_group.parser(buffer(buf[offset:])) rec4 = igmpv3_report_group.parser(buf[offset:])
eq_(res[0], self.msgtype) eq_(res[0], self.msgtype)
eq_(res[1], checksum(self.buf)) eq_(res[1], checksum(self.buf))
@ -813,7 +814,7 @@ class Test_igmpv3_report_group(unittest.TestCase):
def test_serialize(self): def test_serialize(self):
buf = self.g.serialize() buf = self.g.serialize()
res = unpack_from(igmpv3_report_group._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report_group._PACK_STR, six.binary_type(buf))
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
@ -823,8 +824,8 @@ class Test_igmpv3_report_group(unittest.TestCase):
def test_serialize_with_srcs(self): def test_serialize_with_srcs(self):
self.setUp_with_srcs() self.setUp_with_srcs()
buf = self.g.serialize() buf = self.g.serialize()
res = unpack_from(igmpv3_report_group._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report_group._PACK_STR, six.binary_type(buf))
(src1, src2, src3) = unpack_from('4s4s4s', buffer(buf), (src1, src2, src3) = unpack_from('4s4s4s', six.binary_type(buf),
igmpv3_report_group._MIN_LEN) igmpv3_report_group._MIN_LEN)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
@ -837,8 +838,8 @@ class Test_igmpv3_report_group(unittest.TestCase):
def test_serialize_with_aux(self): def test_serialize_with_aux(self):
self.setUp_with_aux() self.setUp_with_aux()
buf = self.g.serialize() buf = self.g.serialize()
res = unpack_from(igmpv3_report_group._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report_group._PACK_STR, six.binary_type(buf))
(aux, ) = unpack_from('%ds' % (self.aux_len * 4), buffer(buf), (aux, ) = unpack_from('%ds' % (self.aux_len * 4), six.binary_type(buf),
igmpv3_report_group._MIN_LEN) igmpv3_report_group._MIN_LEN)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)
@ -849,10 +850,10 @@ class Test_igmpv3_report_group(unittest.TestCase):
def test_serialize_with_srcs_and_aux(self): def test_serialize_with_srcs_and_aux(self):
self.setUp_with_srcs_and_aux() self.setUp_with_srcs_and_aux()
buf = self.g.serialize() buf = self.g.serialize()
res = unpack_from(igmpv3_report_group._PACK_STR, buffer(buf)) res = unpack_from(igmpv3_report_group._PACK_STR, six.binary_type(buf))
(src1, src2, src3) = unpack_from('4s4s4s', buffer(buf), (src1, src2, src3) = unpack_from('4s4s4s', six.binary_type(buf),
igmpv3_report_group._MIN_LEN) igmpv3_report_group._MIN_LEN)
(aux, ) = unpack_from('%ds' % (self.aux_len * 4), buffer(buf), (aux, ) = unpack_from('%ds' % (self.aux_len * 4), six.binary_type(buf),
igmpv3_report_group._MIN_LEN + 12) igmpv3_report_group._MIN_LEN + 12)
eq_(res[0], self.type_) eq_(res[0], self.type_)
eq_(res[1], self.aux_len) eq_(res[1], self.aux_len)