Skip to content

Commit

Permalink
remove vendoring of bitstring, fix SID parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed May 13, 2020
1 parent c413d15 commit 207959a
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 4,446 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Version 5.0.0:
patch: tomjshine
* reported: the RIB code so withdraw message before any announce are sent
this does change the RIB behaviour sending withdrawal when it was not previously
* Fix: parsing of SID in BGP-LS

Version 4.2.6:
* Fix: prevent the deletion of IP addresses not added by the healthchecker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
Copyright (c) 2014-2017 Exa Networks. All rights reserved.
"""

from exabgp.vendoring.bitstring import BitArray
from struct import unpack

from exabgp.bgp.message.notification import Notify
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE

Expand All @@ -26,8 +27,7 @@ def unpack(cls, data, length):
if length != 4:
raise Notify(3, 5, "Unable to decode attribute. Incorrect Size")
else:
b = BitArray(bytes=data)
colormask = b.unpack('uintbe:32')
colormask = unpack('!L', data[:4])[0]
return cls(colormask=colormask)

def json(self, compact=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from struct import unpack

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.notification import Notify

from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE
Expand Down Expand Up @@ -51,8 +50,7 @@ def unpack(cls, data, length):
return cls(igpmetric=igpmetric)
elif len(data) == 3:
# ISIS wide metrics
b = BitArray(bytes=data)
igpmetric = b.unpack('uintbe:24')
igpmetric = unpack('!L', bytes([0]) + data)[0]
return cls(igpmetric=igpmetric)
else:
raise Notify(3, 5, "Incorrect IGP Metric Size")
Expand Down
6 changes: 2 additions & 4 deletions lib/exabgp/bgp/message/update/attribute/bgpls/link/sradj.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from struct import unpack
from exabgp.util import hexstring

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE, LsGenericFlags

# draft-gredler-idr-bgp-ls-segment-routing-ext-03
Expand Down Expand Up @@ -62,12 +61,11 @@ def unpack(cls, data, length):
# Range Size: 3 octet value indicating the number of labels in
# the range.
if int(flags.flags['V']) and int(flags.flags['L']):
b = BitArray(bytes=data[:3])
sid = b.unpack('uintbe:24')[0:1]
sid = unpack('!L', bytes([0]) + data[:3])[0]
data = data[3:]
sids.append(sid)
elif (not flags.flags['V']) and (not flags.flags['L']):
sid = unpack('!I', data[:4])[0:1]
sid = unpack('!I', data[:4])[0]
data = data[4:]
sids.append(sid)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from struct import unpack
from exabgp.util import hexstring

from exabgp.vendoring.bitstring import BitArray
from exabgp.protocol.iso import ISO
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE, LsGenericFlags

Expand Down Expand Up @@ -72,8 +71,7 @@ def unpack(cls, data, length):
# Range Size: 3 octet value indicating the number of labels in
# the range.
if int(flags.flags['V']) and int(flags.flags['L']):
b = BitArray(bytes=data[:3])
sid = b.unpack('uintbe:24')[0]
sid = unpack('!L', bytes([0]) + data[:3])[0]
data = data[3:]
sids.append(sid)
elif (not flags.flags['V']) and (not flags.flags['L']):
Expand Down
10 changes: 4 additions & 6 deletions lib/exabgp/bgp/message/update/attribute/bgpls/linkstate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import itertools
from struct import unpack

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.notification import Notify
from exabgp.bgp.message.update.attribute.attribute import Attribute

Expand Down Expand Up @@ -128,17 +127,16 @@ def __init__(self, flags):
def unpack(cls, data, pattern):
pad = pattern.count('RSV')
repeat = len(pattern) - pad
flag_array = binascii.b2a_hex(data)
hex_rep = hex(int(flag_array, 16))
bit_array = BitArray(hex_rep)
hex_rep = int(binascii.b2a_hex(data), 16)
bits = f'{hex_rep:08b}'
valid_flags = [
''.join(''.join(item), ''.join(itertools.repeat('0', pad)))
for item in itertools.product('01', repeat=repeat)
]
valid_flags.append('0000')
if bit_array.bin in valid_flags:
if bits in valid_flags:
flags = dict(zip(pattern, [0,] * len(pattern)))
flags.update(dict((k, int(v)) for k, v in zip(pattern, bit_array.bin)))
flags.update(dict((k, int(v)) for k, v in zip(pattern, bits)))
else:
raise Notify(3, 5, "Invalid SR flags mask")
return cls(flags=flags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
Copyright (c) 2014-2017 Exa Networks. All rights reserved.
"""

from exabgp.vendoring.bitstring import BitArray

from exabgp.bgp.message.notification import Notify

from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE

# 0 1 2 3
Expand All @@ -34,9 +30,7 @@ def __repr__(self):

@classmethod
def unpack(cls, data, length):

b = BitArray(bytes=data)
return cls(areaid=b.hex)
return cls(areaid=int(data.hex(), 16))

def json(self, compact=None):
return '"area-id": "%s"' % str(self.areaid)
8 changes: 3 additions & 5 deletions lib/exabgp/bgp/message/update/attribute/bgpls/node/srcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import json
from struct import unpack

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE, LsGenericFlags
from exabgp.bgp.message.notification import Notify

Expand Down Expand Up @@ -59,8 +58,8 @@ def unpack(cls, data, length):
while data:
# Range Size: 3 octet value indicating the number of labels in
# the range.
b = BitArray(bytes=data[:3])
range_size = b.unpack('uintbe:24')[0]
range_size = unpack('!L', bytes([0]) + data[:3])[0]

# SID/Label: If length is set to 3, then the 20 rightmost bits
# represent a label. If length is set to 4, then the value
# represents a 32 bit SID.
Expand All @@ -69,8 +68,7 @@ def unpack(cls, data, length):
raise Notify(3, 5, "Invalid sub-TLV type: {}".format(t))
v = data[7 : l + 7]
if l == 3:
b = BitArray(bytes=v)
sid = b.unpack('uintbe:24')[0]
sid = unpack('!L', bytes([0]) + data[:3])[0]
elif l == 4:
sid = unpack('!I', v)[0]
sids.append((range_size, sid))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from struct import unpack

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.notification import Notify

from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from struct import unpack
from exabgp.util import hexstring

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.notification import Notify
from exabgp.bgp.message.update.attribute.bgpls.linkstate import LINKSTATE, LsGenericFlags

Expand Down Expand Up @@ -61,8 +60,7 @@ def unpack(cls, data, length):
raw = []
while data:
if flags.flags['V'] and flags.flags['L']:
b = BitArray(bytes=data[:3])
sid = b.unpack('uintbe:24')[0]
sid = unpack('!L', bytes([0]) + data[:3])[0]
data = data[3:]
sids.append(sid)
elif (not flags.flags['V']) and (not flags.flags['L']):
Expand Down
8 changes: 3 additions & 5 deletions lib/exabgp/bgp/message/update/attribute/sr/srgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"""
import json
from struct import pack
from struct import unpack

from exabgp.vendoring.bitstring import BitArray
from exabgp.bgp.message.update.attribute.sr.prefixsid import PrefixSid

# 0 1 2 3
Expand Down Expand Up @@ -67,10 +67,8 @@ def unpack(cls, data, length):
# the SRGB field MAY appear multiple times. If the SRGB field
# appears multiple times, the SRGB consists of multiple ranges.
while data:
b = BitArray(bytes=data[:3])
base = b.unpack('uintbe:24')[0]
b = BitArray(bytes=data[3:6])
srange = b.unpack('uintbe:24')[0]
base = unpack('!L', bytes([0]) + data[:3])[0]
srange = unpack('!L', bytes([0]) + data[3:6])[0]
srgbs.append((base, srange))
data = data[6:]
return cls(srgbs=srgbs)
Expand Down
5 changes: 1 addition & 4 deletions lib/exabgp/protocol/iso/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
# =========================================================================== ISO
#

from exabgp.vendoring.bitstring import BitArray


class ISO(object):
def __init__(self, sysid, selector=None, area_id=None, afi=49):
Expand All @@ -23,8 +21,7 @@ def __init__(self, sysid, selector=None, area_id=None, afi=49):

@classmethod
def unpack_sysid(cls, data):
b = BitArray(bytes=data)
return b.hex
return data.hex()

def json(self, compact=None):
return self.sysid
Loading

0 comments on commit 207959a

Please sign in to comment.