Skip to content

Commit

Permalink
use ord instead of struct.unpack for byte data
Browse files Browse the repository at this point in the history
Signed-off-by: Peng Xiao <xiaoquwl@gmail.com>
  • Loading branch information
xiaopeng163 committed Mar 15, 2018
1 parent bc26039 commit 6baad51
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 124 deletions.
2 changes: 1 addition & 1 deletion yabgp/message/attribute/extcommunity.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def parse(cls, value):
asn, an = struct.unpack('!HI', value_tmp)
ext_community.append([bgp_cons.BGP_EXT_REDIRECT_VRF, '%s:%s' % (asn, an)])
elif comm_code == bgp_cons.BGP_EXT_TRA_MARK:
mark = struct.unpack('!B', value_tmp[-1])[0]
mark = ord(value_tmp[-1:])
ext_community.append([bgp_cons.BGP_EXT_TRA_MARK, mark])

# Transitive Opaque
Expand Down
5 changes: 2 additions & 3 deletions yabgp/message/attribute/linkstate/link/adj_seg_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
import binascii

from yabgp.tlv import TLV
Expand Down Expand Up @@ -59,7 +58,7 @@ class AdjSegID(TLV):

@classmethod
def unpack(cls, data, pro_id):
flags = struct.unpack('!B', data[0])[0]
flags = ord(data[0:1])
flag = {}
if pro_id in [1, 2]:
flag['F'] = flags >> 7
Expand All @@ -74,6 +73,6 @@ def unpack(cls, data, pro_id):
flag['L'] = (flags << 2) % 256 >> 7
flag['G'] = (flags << 3) % 256 >> 7
flag['P'] = (flags << 4) % 256 >> 7
weight = struct.unpack('!B', data[1])[0]
weight = ord(data[1:2])
return cls(value={"flags": flag, "weight": weight, "sid_index_label": int(binascii.b2a_hex(data[4:]), 16)})
# return cls(value=int(binascii.b2a_hex(data[4:]), 16))
2 changes: 1 addition & 1 deletion yabgp/message/attribute/linkstate/link/igp_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ def unpack(cls, data):
if len(data) == 2:
return cls(value=struct.unpack('!H', data)[0])
elif len(data) == 1:
return cls(value=struct.unpack('!B', data)[0])
return cls(value=ord(data[0:1]))
elif len(data) == 3:
return cls(value=int(binascii.b2a_hex(data), 16))
7 changes: 4 additions & 3 deletions yabgp/message/attribute/linkstate/link/lan_adj_seg_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
# under the License.

import binascii
import struct

import netaddr

from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

Expand Down Expand Up @@ -54,7 +55,7 @@ class LanAdjSegID(TLV):

@classmethod
def unpack(cls, data, pro_id):
flags = struct.unpack('!B', data[0])[0]
flags = ord(data[0:1])
flag = {}
if pro_id in [1, 2]:
flag['F'] = flags >> 7
Expand All @@ -71,7 +72,7 @@ def unpack(cls, data, pro_id):
flag['G'] = (flags << 3) % 256 >> 7
flag['P'] = (flags << 4) % 256 >> 7
nei_or_sys_id = str(netaddr.IPAddress(int(binascii.b2a_hex(data[4:10]), 16)))
weight = struct.unpack('!B', data[1])[0]
weight = ord(data[1:2])
sid_index_label = int(binascii.b2a_hex(data[10:]), 16)
return cls(value={
"flags": flag,
Expand Down
3 changes: 1 addition & 2 deletions yabgp/message/attribute/linkstate/link/mplsmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

Expand Down Expand Up @@ -45,7 +44,7 @@ class MplsMask(TLV):
def unpack(cls, value):
"""
"""
value = struct.unpack('!B', value)[0]
value = ord(value[0:1])
L = value >> 7
R = (value << 1) % 256 >> 7
return cls(value={'L': L, 'R': R})
3 changes: 1 addition & 2 deletions yabgp/message/attribute/linkstate/link/protection_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

Expand Down Expand Up @@ -43,7 +42,7 @@ class ProtectionType(TLV):
def unpack(cls, value):
"""
"""
value = struct.unpack('!B', value[0])[0]
value = ord(value[0])
if value == 0x01:
return cls(value='Extra Traffic')
if value == 0x02:
Expand Down
3 changes: 1 addition & 2 deletions yabgp/message/attribute/linkstate/node/nodeflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

Expand Down Expand Up @@ -48,7 +47,7 @@ class NodeFlags(TLV):
def unpack(cls, value):
"""
"""
valueByte = struct.unpack('!B', value)[0]
valueByte = ord(value[0:1])
O = valueByte >> 7
T = (valueByte << 1) % 256 >> 7
E = (valueByte << 2) % 256 >> 7
Expand Down
4 changes: 1 addition & 3 deletions yabgp/message/attribute/linkstate/node/sr_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct

from yabgp.tlv import TLV
from ..linkstate import LinkState

Expand All @@ -40,5 +38,5 @@ class SRAlgorithm(TLV):
def unpack(cls, data):
results = []
for value in data:
results.append(struct.unpack('!B', value)[0])
results.append(ord(value[0:1]))
return cls(value=results)
9 changes: 1 addition & 8 deletions yabgp/message/attribute/linkstate/node/sr_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SRCapabilities(TLV):
def unpack(cls, value):
"""
"""
flags = struct.unpack('!B', value[0])[0]
flags = ord(value[0:1])
F = flags >> 7
M = (flags << 1) % 256 >> 7
S = (flags << 2) % 256 >> 7
Expand All @@ -65,10 +65,3 @@ def unpack(cls, value):
value = value[7 + length:]
results.append({"sid_or_label": data, "range_size": range_size})
return cls(value={"flag": {"F": F, "M": M, "S": S, "D": D, "A": A}, "value": results})
# results = dict()
# if ord(value[0]) == 0x80:
# results['ipv4'] = True
# else:
# results['ipv6'] = True
# results['range-size'] = struct.unpack('!L', value[1:5])[0]
# return cls(value=results)
6 changes: 0 additions & 6 deletions yabgp/message/attribute/linkstate/node/srlb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ class SRLB(TLV):
def unpack(cls, value):
"""
"""
# flags = value[0]
# F = struct.unpack('!B', flags)[0] >> 7
# M = (struct.unpack('!B', flags)[0] << 1) >> 7
# S = (struct.unpack('!B', flags)[0] << 2) >> 7
# D = (struct.unpack('!B', flags)[0] << 3) >> 7
# A = (struct.unpack('!B', flags)[0] << 4) >> 7
value = value[2:]
results = []
while True:
Expand Down
3 changes: 1 addition & 2 deletions yabgp/message/attribute/linkstate/prefix/igpflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
from yabgp.message.attribute.linkstate.linkstate import LinkState
from yabgp.tlv import TLV

Expand Down Expand Up @@ -46,7 +45,7 @@ class IGPFlags(TLV):
def unpack(cls, value):
"""
"""
value = struct.unpack('!B', value)[0]
value = ord(value[0:1])
D = value >> 7
N = (value << 1) % 256 >> 7
L = (value << 2) % 256 >> 7
Expand Down
3 changes: 1 addition & 2 deletions yabgp/message/attribute/linkstate/prefix/prefix_sid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import struct
import binascii

from yabgp.tlv import TLV
Expand All @@ -40,5 +39,5 @@ class PrefixSID(TLV):

@classmethod
def unpack(cls, data):
algorithm = struct.unpack('!B', data[1])
algorithm = ord(data[1:2])
return cls(value={"algorithm": algorithm, "sid": int(binascii.b2a_hex(data[4:]), 16)})
6 changes: 3 additions & 3 deletions yabgp/message/attribute/nlri/ipv4_flowspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def parse(cls, value):
nlri_dict = {}
while value:
offset = 0
flowspec_type = struct.unpack('!B', value[0])[0]
flowspec_type = ord(value[0:1])
offset += 1
# decode all kinds of flow spec
if flowspec_type in [bgp_cons.BGPNLRI_FSPEC_DST_PFIX, bgp_cons.BGPNLRI_FSPEC_SRC_PFIX]:
Expand Down Expand Up @@ -104,7 +104,7 @@ def parse_prefix(data):
Encoding: <prefix-length (1 octet), prefix>
"""
prefix_len = struct.unpack('!B', data[0])[0]
prefix_len = ord(data[0:1])
octet_len = int(math.ceil(prefix_len / 8.0))
tmp = data[1:octet_len + 1]
prefix_data = [ord(i) for i in tmp]
Expand Down Expand Up @@ -135,7 +135,7 @@ def parse_operators(cls, data):
offset = 0
parse_operator_list = []
while data:
operator = cls.parse_operator_flag(struct.unpack('!B', data[0])[0])
operator = cls.parse_operator_flag(ord(data[0:1]))
offset += 1
operator_value = int(binascii.b2a_hex(data[1:1 + operator['LEN']]), 16)
offset += operator['LEN']
Expand Down
2 changes: 1 addition & 1 deletion yabgp/message/attribute/nlri/ipv6_unicast.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def parse(cls, nlri_data):
if isinstance(nlri_data[0], int):
prefix_bit_len = int(nlri_data[0])
else:
prefix_bit_len = struct.unpack('!B', nlri_data[0])[0]
prefix_bit_len = ord(nlri_data[0:1])
if prefix_bit_len % 8 == 0:
prefix_byte_len = prefix_bit_len / 8
else:
Expand Down
6 changes: 3 additions & 3 deletions yabgp/message/attribute/nlri/linkstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def parse_nlri(cls, data):
# +------------+----------------------------------+
# Table 3: Well-Known Instance Identifiers

proto_id = struct.unpack('!B', data[0])[0]
proto_id = ord(data[0:1])

identifier = struct.unpack('!I', data[5:9])[0]

Expand Down Expand Up @@ -136,10 +136,10 @@ def parse_nlri(cls, data):
value = value[2:]
elif _type == 264: # OSPF Route Type
descriptor['type'] = 'prefix_ospf_route_type'
descriptor['value'] = struct.unpack('!B', value)[0]
descriptor['value'] = ord(value[0:1])
elif _type == 265: # IP Reachability Information
descriptor['type'] = 'prefix'
mask = struct.unpack('!B', value[0])[0]
mask = ord(value[0:1])
if value[1:]:
ip_str = str(netaddr.IPAddress(int(binascii.b2a_hex(value[1:]), 16)))
else:
Expand Down
2 changes: 1 addition & 1 deletion yabgp/message/attribute/origin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def parse(cls, value):
unrecognized attribute (type,length, and vlaue)
:param value: raw binary value
"""
orgin = struct.unpack('!B', value)[0]
orgin = ord(value[0:1])
if orgin not in [cls.IGP, cls.EGP, cls.INCOMPLETE]:
raise excep.UpdateMessageError(
sub_error=bgp_cons.ERR_MSG_UPDATE_INVALID_ORIGIN,
Expand Down
6 changes: 3 additions & 3 deletions yabgp/tests/unit/message/attribute/nlri/test_bgpls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ def test_parse(self):
{
'type': 'link',
'protocol_id': 2,
'identifier': 0,
'instances_id': 0,
'descriptors': [
{
'type': 'local_node',
'value': {
'as': 65534,
'bgpls_id': '0.0.0.0',
'igp_id': '0.0.0.3'}},
'igp_router_id': '0.0.0.3'}},
{
'type': 'remote_node',
'value': {
'as': 65534,
'bgpls_id': '0.0.0.0',
'igp_id': '0.0.0.1'}},
'igp_router_id': '0.0.0.1'}},
{
'type': 'link_local_ipv4',
'value': '1.3.0.2'},
Expand Down
6 changes: 3 additions & 3 deletions yabgp/tests/unit/message/attribute/test_mpreachnlri.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,20 @@ def test_linkstate(self):
{
'type': 'link',
'protocol_id': 2,
'identifier': 0,
'instances_id': 0,
'descriptors': [
{
'type': 'local_node',
'value': {
'as': 65534,
'bgpls_id': '0.0.0.0',
'igp_id': '0.0.0.1'}},
'igp_router_id': '0.0.0.1'}},
{
'type': 'remote_node',
'value': {
'as': 65534,
'bgpls_id': '0.0.0.0',
'igp_id': '0.0.0.3'}},
'igp_router_id': '0.0.0.3'}},
{'type': 'link_local_ipv4', 'value': '1.3.0.1'},
{'type': 'link_remote_ipv4', 'value': '1.3.0.2'}]}]}
self.assertEqual(data_dict, MpReachNLRI.parse(data[4:]))
Expand Down
Loading

0 comments on commit 6baad51

Please sign in to comment.