Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create (cls,string,afi):
if afi == AFI.ipv4:
if string is cls.codes:
klass = cls(cls.codes[string],32)
klass.maximum = 32
return klass
maximum = 32
elif afi == AFI.ipv6:
if string in cls.codes:
raise ValueError('IPv4 mask used with an IPv6 address')
maximum = 128
else:
raise ValueError('invalid address family')
if not string.isdigit():
raise ValueError('invalid netmask %s' % string)
value = long(string)
if len(withdrawn) != lw:
raise Notify(3,1,'invalid withdrawn routes length, not enough data available')
la,attribute,announced = defix(data)
if len(attribute) != la:
raise Notify(3,1,'invalid total path attribute length, not enough data available')
if 2 + lw + 2+ la + len(announced) != length:
raise Notify(3,1,'error in BGP message length, not enough data for the size announced')
attributes = AttributesFactory(NLRIFactory,negotiated,attribute)
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(AFI(AFI.ipv4),SAFI(SAFI.unicast))
nho = attributes.get(AID.NEXT_HOP,None)
nh = nho.packed if nho else None
if not withdrawn:
logger.parser(LazyFormat("parsed no withdraw nlri",od,''))
nlris = []
while withdrawn:
length,nlri = NLRIFactory(AFI.ipv4,SAFI.unicast,withdrawn,addpath,nh,IN.withdrawn)
logger.parser(LazyFormat("parsed withdraw nlri %s payload " % nlri,od,withdrawn[:len(nlri)]))
withdrawn = withdrawn[length:]
nlris.append(nlri)
if not announced:
logger.parser(LazyFormat("parsed no announced nlri",od,''))
# -- Reading AFI/SAFI
afi,safi = unpack('!HB',data[:3])
offset = 3
# we do not want to accept unknown families
if (afi,safi) not in self.negotiated.families:
raise Notify(3,0,'presented a non-negotiated family %d/%d' % (afi,safi))
# -- Reading length of next-hop
len_nh = ord(data[offset])
offset += 1
rd = 0
# check next-hope size
if afi == AFI.ipv4:
if safi in (SAFI.unicast,SAFI.multicast):
if len_nh != 4:
raise Notify(3,0,'invalid ipv4 unicast/multicast next-hop length %d expected 4' % len_nh)
elif safi in (SAFI.mpls_vpn,):
if len_nh != 12:
raise Notify(3,0,'invalid ipv4 mpls_vpn next-hop length %d expected 12' % len_nh)
rd = 8
elif safi in (SAFI.flow_ip,):
if len_nh not in (0,4):
raise Notify(3,0,'invalid ipv4 flow_ip next-hop length %d expected 4' % len_nh)
elif safi in (SAFI.flow_vpn,):
if len_nh not in (0,4):
raise Notify(3,0,'invalid ipv4 flow_vpn next-hop length %d expected 4' % len_nh)
elif afi == AFI.ipv6:
if safi in (SAFI.unicast,):
if len_nh not in (16,32):
def multicast_v4 (tokeniser):
return ip_multicast(tokeniser,AFI.ipv4,SAFI.multicast)
def messages (self, negotiated, include_withdraw=True):
# sort the nlris
nlris = []
mp_nlris = {}
for nlri in sorted(self.nlris):
if nlri.family() in negotiated.families:
if nlri.afi == AFI.ipv4 and nlri.safi in [SAFI.unicast, SAFI.multicast]:
nlris.append(nlri)
else:
mp_nlris.setdefault(nlri.family(), {}).setdefault(nlri.action, []).append(nlri)
if not nlris and not mp_nlris:
return
attr = self.attributes.pack(negotiated, True)
# Withdraws/NLRIS (IPv4 unicast and multicast)
msg_size = negotiated.msg_size - 19 - 2 - 2 - len(attr) # 2 bytes for each of the two prefix() header
withdraws = b''
announced = b''
for nlri in nlris:
packed = nlri.pack(negotiated)
if len(announced + withdraws + packed) > msg_size:
def ipv4 (self):
return self.afi == AFI.ipv4
if 2 + lw + 2+ la + len(announced) != length:
raise Notify(3,1,'error in BGP message length, not enough data for the size announced')
attributes = AttributesFactory(NLRIFactory,negotiated,attribute)
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(AFI(AFI.ipv4),SAFI(SAFI.unicast))
nho = attributes.get(AID.NEXT_HOP,None)
nh = nho.packed if nho else None
if not withdrawn:
logger.parser(LazyFormat("parsed no withdraw nlri",od,''))
nlris = []
while withdrawn:
length,nlri = NLRIFactory(AFI.ipv4,SAFI.unicast,withdrawn,addpath,nh,IN.withdrawn)
logger.parser(LazyFormat("parsed withdraw nlri %s payload " % nlri,od,withdrawn[:len(nlri)]))
withdrawn = withdrawn[length:]
nlris.append(nlri)
if not announced:
logger.parser(LazyFormat("parsed no announced nlri",od,''))
while announced:
length,nlri = NLRIFactory(AFI.ipv4,SAFI.unicast,announced,addpath,nh,IN.announced)
logger.parser(LazyFormat("parsed announce nlri %s payload " % nlri,od,announced[:len(nlri)]))
announced = announced[length:]
nlris.append(nlri)
for nlri in attributes.mp_withdraw:
nlris.append(nlri)
def ipv4 (self,tokeniser):
self._check_conflict()
safi = tokeniser()
if safi == 'unicast':
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.unicast)))
elif safi == 'multicast':
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.multicast)))
elif safi == 'nlri-mpls':
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.nlri_mpls)))
elif safi == 'mpls-vpn':
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.mpls_vpn)))
elif safi in ('flow'):
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.flow_ip)))
elif safi == 'flow-vpn':
self.content.append((AFI(AFI.ipv4),SAFI(SAFI.flow_vpn)))
else:
raise Raised('unknow family safi %s' % safi)
self._drop_colon(tokeniser)
class Capabilities (dict):
_ADD_PATH = [
(AFI.ipv4,SAFI.unicast),
(AFI.ipv6,SAFI.unicast),
(AFI.ipv4,SAFI.nlri_mpls),
(AFI.ipv4,SAFI.mpls_vpn),
(AFI.ipv6,SAFI.mpls_vpn),
]
_NEXTHOP = [
(AFI.ipv4, SAFI.unicast, AFI.ipv6),
(AFI.ipv4, SAFI.multicast, AFI.ipv6),
(AFI.ipv4, SAFI.nlri_mpls, AFI.ipv6),
(AFI.ipv4, SAFI.mpls_vpn ,AFI.ipv6),
]
def announced (self, capability):
return capability in self
def __str__ (self):
r = []
for key in sorted(self.keys()):
r.append(str(self[key]))
return ', '.join(r)
def _protocol (self, neighbor):
families = neighbor.families()
mp = MultiProtocol()
mp.extend(families)
self[Capability.CODE.MULTIPROTOCOL] = mp