Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _detect_safi (ip):
if '.' in ip and int(ip.split('.')[0]) in Inet._multicast_range:
return SAFI.multicast
else:
return SAFI.unicast
# This could be speed up massively by changing the order of the IF
if length == 23:
return EOR(AFI.ipv4,SAFI.unicast,IN.ANNOUNCED) # pylint: disable=E1101
if length == 30 and data.startswith(EOR.NLRI.PREFIX):
return EOR.unpack_message(data,negotiated)
withdrawn, _attributes, announced = cls.split(data)
attributes = Attributes.unpack(_attributes,negotiated)
if not withdrawn:
logger.parser("no withdrawn NLRI")
if not announced:
logger.parser("no announced NLRI")
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(AFI(AFI.ipv4),SAFI(SAFI.unicast))
# empty string for NoIP, the packed IP otherwise (without the 3/4 bytes of attributes headers)
_nexthop = attributes.get(Attribute.CODE.NEXT_HOP,NoIP)
nexthop = _nexthop.packed
# XXX: NEXTHOP MUST NOT be the IP address of the receiving speaker.
nlris = []
while withdrawn:
length,nlri = NLRI.unpack(AFI.ipv4,SAFI.unicast,withdrawn,addpath,nexthop,IN.WITHDRAWN)
logger.parser(LazyFormat("parsed withdraw nlri %s payload " % nlri,withdrawn[:len(nlri)]))
withdrawn = withdrawn[length:]
nlris.append(nlri)
while announced:
length,nlri = NLRI.unpack(AFI.ipv4,SAFI.unicast,announced,addpath,nexthop,IN.ANNOUNCED)
else:
self._error = 'next-hop self can only be specified with a neighbor'
if self.debug: raise ValueError(self._error) # noqa
return False
nh = IP.unpack(la.pack())
else:
nh = IP.create(ip)
change = scope[-1]['announce'][-1]
nlri = change.nlri
afi = nlri.afi
safi = nlri.safi
nlri.nexthop = nh
if afi == AFI.ipv4 and safi in (SAFI.unicast,SAFI.multicast):
change.attributes.add(Attribute.unpack(NextHop.ID,NextHop.FLAG,nh.packed,None))
# NextHop(nh.ip,nh.packed) does not cache the result, using unpack does
# change.attributes.add(NextHop(nh.ip,nh.packed))
return True
except Exception:
self._error = self._str_route_error
if self.debug: raise Exception() # noqa
return False
def check (change,afi):
if change.nlri.nexthop is NoNextHop \
and change.nlri.action == OUT.ANNOUNCE \
and change.nlri.afi == afi \
and change.nlri.safi in (SAFI.unicast,SAFI.multicast):
return False
return True
def _set_family_inet6 (self,scope,tokens):
if self._family:
self._error = 'inet6 can not be used with all or minimal'
if self.debug: raise
return False
safi = tokens.pop(0)
if safi == 'unicast':
scope[-1]['families'].append((AFI(AFI.ipv6),SAFI(SAFI.unicast)))
elif safi == 'mpls-vpn':
scope[-1]['families'].append((AFI(AFI.ipv6),SAFI(SAFI.mpls_vpn)))
else:
return False
return True
' ipv6 flow-vpn;\n' \
' l2vpn vpls;\n' \
' l2vpn evpn;\n' \
'}'
convert = {
'ipv4': {
'unicast': (AFI(AFI.ipv4),SAFI(SAFI.unicast)),
'multicast': (AFI(AFI.ipv4),SAFI(SAFI.multicast)),
'nlri-mpls': (AFI(AFI.ipv4),SAFI(SAFI.nlri_mpls)),
'mpls-vpn': (AFI(AFI.ipv4),SAFI(SAFI.mpls_vpn)),
'flow': (AFI(AFI.ipv4),SAFI(SAFI.flow_ip)),
'flow-vpn': (AFI(AFI.ipv4),SAFI(SAFI.flow_vpn)),
},
'ipv6': {
'unicast': (AFI(AFI.ipv6),SAFI(SAFI.unicast)),
'nlri-mpls': (AFI(AFI.ipv6),SAFI(SAFI.nlri_mpls)),
'mpls-vpn': (AFI(AFI.ipv6),SAFI(SAFI.mpls_vpn)),
'flow': (AFI(AFI.ipv6),SAFI(SAFI.flow_ip)),
'flow-vpn': (AFI(AFI.ipv6),SAFI(SAFI.flow_vpn)),
},
'l2vpn': {
'vpls': (AFI(AFI.l2vpn),SAFI(SAFI.vpls)),
'evpn': (AFI(AFI.l2vpn),SAFI(SAFI.evpn)),
},
'bgpls': {
'bgp-ls': (AFI(AFI.bgpls),SAFI(SAFI.bgp_ls)),
'bgp-ls-vpn': (AFI(AFI.bgpls),SAFI(SAFI.bgp_ls_vpn)),
},
}
action = {
def pack (self):
if self.afi == AFI.ipv4 and self.safi == SAFI.unicast:
return '\x00\x00\x00\x00'
return self.PREFIX + self.afi.pack() + self.safi.pack()
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)
elif self._nexthopself:
la = self._nexthopself
else:
return self.error.set('next-hop self can only be specified with a neighbor')
nh = IP.unpack(la.pack())
else:
nh = IP.create(ip)
change = self.scope.content[-1]['announce'][-1]
nlri = change.nlri
afi = nlri.afi
safi = nlri.safi
nlri.nexthop = nh
if afi == AFI.ipv4 and safi in (SAFI.unicast,SAFI.multicast):
change.attributes.add(Attribute.unpack(NextHop.ID,NextHop.FLAG,nh.packed,None))
# NextHop(nh.ip,nh.packed) does not cache the result, using unpack does
# change.attributes.add(NextHop(nh.ip,nh.packed))
return True
except Exception:
return self.error.set(self.syntax)
def default (self,neighbor,restarted):
graceful = neighbor.graceful_restart
families = neighbor.families()
mp = MultiProtocol()
mp.extend(families)
self[Capabilities.MULTIPROTOCOL_EXTENSIONS] = mp
self[Capabilities.FOUR_BYTES_ASN] = neighbor.local_as
if neighbor.add_path:
ap_families = []
if (AFI(AFI.ipv4),SAFI(SAFI.unicast)) in families:
ap_families.append((AFI(AFI.ipv4),SAFI(SAFI.unicast)))
if (AFI(AFI.ipv4),SAFI(SAFI.nlri_mpls)) in families:
ap_families.append((AFI(AFI.ipv4),SAFI(SAFI.nlri_mpls)))
#if (AFI(AFI.ipv6),SAFI(SAFI.unicast)) in families:
# ap_families.append((AFI(AFI.ipv6),SAFI(SAFI.unicast)))
self[Capabilities.ADD_PATH] = AddPath(ap_families,neighbor.add_path)
if graceful:
if restarted:
self[Capabilities.GRACEFUL_RESTART] = Graceful(Graceful.RESTART_STATE,graceful,[(afi,safi,Graceful.FORWARDING_STATE) for (afi,safi) in families])
else:
self[Capabilities.GRACEFUL_RESTART] = Graceful(0x0,graceful,[(afi,safi,Graceful.FORWARDING_STATE) for (afi,safi) in families])
# MUST be the last key added
if neighbor.multisession:
self[Capabilities.MULTISESSION_BGP] = MultiSession([Capabilities.MULTIPROTOCOL_EXTENSIONS])
return self