本文整理汇总了Python中exabgp.bgp.message.update.nlri.NLRI类的典型用法代码示例。如果您正苦于以下问题:Python NLRI类的具体用法?Python NLRI怎么用?Python NLRI使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NLRI类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_update
def check_update (neighbor, raw):
logger = Logger()
logger._option.parser = True
logger.parser('\ndecoding routes in configuration')
neighbor = neighbor[neighbor.keys()[0]]
path = {}
for f in NLRI.known_families():
if neighbor.add_path:
path[f] = neighbor.add_path
capa = Capabilities().new(neighbor,False)
capa[Capability.CODE.ADD_PATH] = path
capa[Capability.CODE.MULTIPROTOCOL] = neighbor.families()
# capa[Capability.CODE.FOUR_BYTES_ASN] = True
routerid_1 = str(neighbor.router_id)
routerid_2 = '.'.join(str((int(_)+1) % 250) for _ in str(neighbor.router_id).split('.',-1))
o1 = Open(4,neighbor.local_as,routerid_1,capa,180)
o2 = Open(4,neighbor.peer_as,routerid_2,capa,180)
negotiated = Negotiated(neighbor)
negotiated.sent(o1)
negotiated.received(o2)
# grouped = False
while raw:
if raw.startswith('\xff'*16):
kind = ord(raw[18])
size = (ord(raw[16]) << 16) + (ord(raw[17]))
injected,raw = raw[19:size],raw[size:]
if kind == 2:
logger.parser('the message is an update')
decoding = 'update'
else:
logger.parser('the message is not an update (%d) - aborting' % kind)
return False
else:
logger.parser('header missing, assuming this message is ONE update')
decoding = 'update'
injected,raw = raw,''
try:
# This does not take the BGP header - let's assume we will not break that :)
update = Update.unpack_message(injected,negotiated)
except KeyboardInterrupt:
raise
except Notify,exc:
logger.parser('could not parse the message')
logger.parser(str(exc))
return False
except Exception,exc:
logger.parser('could not parse the message')
logger.parser(str(exc))
return False
示例2: __eq__
def __eq__ (self, other):
return \
NLRI.__eq__(self,other) and \
self.CODE == other.CODE and \
self.rd == other.rd and \
self.etag == other.etag and \
self.ip == other.ip and \
self.iplen == other.iplen
示例3: unpack
def unpack (cls, data, negotiated):
nlris = []
# -- Reading AFI/SAFI
afi,safi = unpack('!HB',data[:3])
offset = 3
data = data[offset:]
if negotiated and (afi,safi) not in negotiated.families:
raise Notify(3,0,'presented a non-negotiated family %s %s' % (AFI.create(afi),SAFI.create(safi)))
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(afi,safi)
while data:
nlri,data = NLRI.unpack_nlri(afi,safi,data,IN.WITHDRAWN,addpath)
nlris.append(nlri)
return cls(afi,safi,nlris)
示例4: unpack
def unpack (cls, data, negotiated):
nlris = []
# -- Reading AFI/SAFI
afi,safi = unpack('!HB',data[:3])
offset = 3
data = data[offset:]
if negotiated and (afi,safi) not in negotiated.families:
raise Notify(3,0,'presented a non-negotiated family %s %s' % (AFI(afi),SAFI(safi)))
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(afi,safi)
while data:
length,nlri = NLRI.unpack(afi,safi,data,addpath,None,IN.WITHDRAWN)
nlris.append(nlri)
data = data[length:]
# logger.parser(LazyFormat("parsed withdraw mp nlri %s payload " % nlri,data[:length]))
return cls(afi,safi,nlris)
示例5: unpack_message
def unpack_message (cls, data, negotiated):
logger = Logger()
length = len(data)
# This could be speed up massively by changing the order of the IF
if length == 4 and data == '\x00\x00\x00\x00':
return EOR(AFI.ipv4,SAFI.unicast,IN.ANNOUNCED) # pylint: disable=E1101
if length == 11 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 NoNextHop, the packed IP otherwise (without the 3/4 bytes of attributes headers)
nexthop = attributes.get(Attribute.CODE.NEXT_HOP,NoNextHop)
# nexthop = NextHop.unpack(_nexthop.ton())
# XXX: NEXTHOP MUST NOT be the IP address of the receiving speaker.
nlris = []
while withdrawn:
nlri,left = NLRI.unpack_nlri(AFI.ipv4,SAFI.unicast,withdrawn,IN.WITHDRAWN,addpath)
logger.parser(LazyFormat("parsed withdraw nlri %s payload " % nlri,withdrawn[:len(nlri)]))
withdrawn = left
nlris.append(nlri)
while announced:
nlri,left = NLRI.unpack_nlri(AFI.ipv4,SAFI.unicast,announced,IN.ANNOUNCED,addpath)
nlri.nexthop = nexthop
logger.parser(LazyFormat("parsed announce nlri %s payload " % nlri,announced[:len(nlri)]))
announced = left
nlris.append(nlri)
# required for 'is' comparaison
UNREACH = [EMPTY_MPURNLRI,]
REACH = [EMPTY_MPRNLRI,]
unreach = attributes.pop(MPURNLRI.ID,UNREACH)
reach = attributes.pop(MPRNLRI.ID,REACH)
for mpr in unreach:
nlris.extend(mpr.nlris)
for mpr in reach:
nlris.extend(mpr.nlris)
if not attributes and not nlris:
# Careful do not use == or != as the comparaison does not work
if unreach is UNREACH and reach is REACH:
return EOR(AFI(AFI.ipv4),SAFI(SAFI.unicast))
if unreach is not UNREACH:
return EOR(unreach[0].afi,unreach[0].safi)
if reach is not REACH:
return EOR(reach[0].afi,reach[0].safi)
raise RuntimeError('This was not expected')
return Update(nlris,attributes)
示例6: check_neighbor
def check_neighbor (neighbors):
logger = Logger()
logger._option.parser = True
logger.parser('\ndecoding routes in configuration')
for name in neighbors.keys():
neighbor = neighbors[name]
path = {}
for f in NLRI.known_families():
if neighbor.add_path:
path[f] = neighbor.add_path
capa = Capabilities().new(neighbor,False)
if path:
capa[Capability.CODE.ADD_PATH] = path
capa[Capability.CODE.MULTIPROTOCOL] = neighbor.families()
routerid_1 = str(neighbor.router_id)
routerid_2 = '.'.join(str((int(_)+1) % 250) for _ in str(neighbor.router_id).split('.',-1))
o1 = Open(Version(4),ASN(neighbor.local_as),HoldTime(180),RouterID(routerid_1),capa)
o2 = Open(Version(4),ASN(neighbor.peer_as),HoldTime(180),RouterID(routerid_2),capa)
negotiated = Negotiated(neighbor)
negotiated.sent(o1)
negotiated.received(o2)
# grouped = False
for _ in neighbor.rib.outgoing.updates(False):
pass
for change1 in neighbor.rib.outgoing.sent_changes():
str1 = change1.extensive()
packed = list(Update([change1.nlri],change1.attributes).messages(negotiated))
pack1 = packed[0]
logger.parser('parsed route requires %d updates' % len(packed))
logger.parser('update size is %d' % len(pack1))
logger.parser('parsed route %s' % str1)
logger.parser('parsed hex %s' % od(pack1))
# This does not take the BGP header - let's assume we will not break that :)
try:
logger.parser('') # new line
pack1s = pack1[19:] if pack1.startswith('\xFF'*16) else pack1
update = Update.unpack_message(pack1s,negotiated)
change2 = Change(update.nlris[0],update.attributes)
str2 = change2.extensive()
pack2 = list(Update([update.nlris[0]],update.attributes).messages(negotiated))[0]
logger.parser('recoded route %s' % str2)
logger.parser('recoded hex %s' % od(pack2))
str1 = str1.replace('attribute [ 0x04 0x80 0x00000064 ]','med 100')
str1r = str1.lower().replace(' med 100','').replace(' local-preference 100','').replace(' origin igp','')
str2r = str2.lower().replace(' med 100','').replace(' local-preference 100','').replace(' origin igp','')
if 'next-hop self' in str1r:
if ':' in str1r:
str1r = str1r.replace('next-hop self','next-hop ::1')
else:
str1r = str1r.replace('next-hop self','next-hop %s' % neighbor.local_address)
if ' name ' in str1r:
parts = str1r.split(' ')
pos = parts.index('name')
str1r = ' '.join(parts[:pos] + parts[pos+2:])
skip = False
if str1r != str2r:
if 'attribute [' in str1r and ' 0x00 ' in str1r:
# we do not decode non-transitive attributes
logger.parser('skipping string check on update with non-transitive attribute(s)')
skip = True
else:
logger.parser('strings are different:')
logger.parser('[%s]' % (str1r))
logger.parser('[%s]' % (str2r))
return False
else:
logger.parser('strings are fine')
if skip:
logger.parser('skipping encoding for update with non-transitive attribute(s)')
elif pack1 != pack2:
logger.parser('encoding are different')
logger.parser('[%s]' % (od(pack1)))
logger.parser('[%s]' % (od(pack2)))
return False
else:
logger.parser('encoding is fine')
logger.parser('----------------------------------------')
logger.parser('JSON nlri %s' % change1.nlri.json())
logger.parser('JSON attr %s' % change1.attributes.json())
#.........这里部分代码省略.........
示例7: check_update
def check_update (neighbor, raw):
logger = Logger()
logger._option.parser = True
logger.parser('\ndecoding routes in configuration')
neighbor = neighbor[neighbor.keys()[0]]
path = {}
for f in NLRI.known_families():
if neighbor.add_path:
path[f] = neighbor.add_path
capa = Capabilities().new(neighbor,False)
capa[Capability.CODE.ADD_PATH] = path
capa[Capability.CODE.MULTIPROTOCOL] = neighbor.families()
# capa[Capability.CODE.FOUR_BYTES_ASN] = True
routerid_1 = str(neighbor.router_id)
routerid_2 = '.'.join(str((int(_)+1) % 250) for _ in str(neighbor.router_id).split('.',-1))
o1 = Open(Version(4),ASN(neighbor.local_as),HoldTime(180),RouterID(routerid_1),capa)
o2 = Open(Version(4),ASN(neighbor.peer_as),HoldTime(180),RouterID(routerid_2),capa)
negotiated = Negotiated(neighbor)
negotiated.sent(o1)
negotiated.received(o2)
# grouped = False
while raw:
if raw.startswith('\xff'*16):
kind = ord(raw[18])
size = (ord(raw[16]) << 16) + (ord(raw[17]))
injected,raw = raw[19:size],raw[size:]
if kind == 2:
logger.parser('the message is an update')
decoding = 'update'
else:
logger.parser('the message is not an update (%d) - aborting' % kind)
return False
else:
logger.parser('header missing, assuming this message is ONE update')
decoding = 'update'
injected,raw = raw,''
try:
# This does not take the BGP header - let's assume we will not break that :)
update = Update.unpack_message(injected,negotiated)
except KeyboardInterrupt:
raise
except Notify:
logger.parser('could not parse the message','error')
logger.parser(traceback.format_exc(),'error')
return False
except StandardError:
logger.parser('could not parse the message','error')
logger.parser(traceback.format_exc(),'error')
return False
logger.parser('') # new line
for number in range(len(update.nlris)):
change = Change(update.nlris[number],update.attributes)
logger.parser('decoded %s %s %s' % (decoding,change.nlri.action,change.extensive()))
logger.parser('update json %s' % Response.JSON(json_version).update(neighbor,'in',update,'',''))
return True
示例8: families
def families ():
return NLRI.known_families()
示例9: __init__
def __init__(self, action=OUT.UNSET, addpath=None):
NLRI.__init__(self, AFI.bgpls, SAFI.bgp_ls, action)
self._packed = b''
示例10: __eq__
def __eq__ (self, other):
return \
NLRI.__eq__(self,other) and \
self.CODE == other.CODE and \
self.pack() == other.pack()
示例11: unpack_message
def unpack_message (cls, data, negotiated):
logger = Logger()
logger.debug(LazyFormat('parsing UPDATE',data),'parser')
length = len(data)
# This could be speed up massively by changing the order of the IF
if length == 4 and data == b'\x00\x00\x00\x00':
return EOR(AFI.ipv4,SAFI.unicast) # pylint: disable=E1101
if length == 11 and data.startswith(EOR.NLRI.PREFIX):
return EOR.unpack_message(data,negotiated)
withdrawn, _attributes, announced = cls.split(data)
if not withdrawn:
logger.debug('withdrawn NLRI none','parser')
attributes = Attributes.unpack(_attributes,negotiated)
if not announced:
logger.debug('announced NLRI none','parser')
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(AFI.ipv4,SAFI.unicast)
# empty string for NoNextHop, the packed IP otherwise (without the 3/4 bytes of attributes headers)
nexthop = attributes.get(Attribute.CODE.NEXT_HOP,NoNextHop)
# nexthop = NextHop.unpack(_nexthop.ton())
# XXX: NEXTHOP MUST NOT be the IP address of the receiving speaker.
nlris = []
while withdrawn:
nlri,left = NLRI.unpack_nlri(AFI.ipv4,SAFI.unicast,withdrawn,IN.WITHDRAWN,addpath)
logger.debug('withdrawn NLRI %s' % nlri,'parser')
withdrawn = left
nlris.append(nlri)
while announced:
nlri,left = NLRI.unpack_nlri(AFI.ipv4,SAFI.unicast,announced,IN.ANNOUNCED,addpath)
nlri.nexthop = nexthop
logger.debug('announced NLRI %s' % nlri,'parser')
announced = left
nlris.append(nlri)
unreach = attributes.pop(MPURNLRI.ID,None)
reach = attributes.pop(MPRNLRI.ID,None)
if unreach is not None:
nlris.extend(unreach.nlris)
if reach is not None:
nlris.extend(reach.nlris)
if not attributes and not nlris:
# Careful do not use == or != as the comparaison does not work
if unreach is None and reach is None:
return EOR(AFI.ipv4,SAFI.unicast)
if unreach is not None:
return EOR(unreach.afi,unreach.safi)
if reach is not None:
return EOR(reach.afi,reach.safi)
raise RuntimeError('This was not expected')
return Update(nlris,attributes)
示例12: __init__
def __init__(self, packed, nexthop=NoNextHop, action=OUT.UNSET, addpath=None):
NLRI.__init__(self, AFI.l2vpn, SAFI.evpn, action)
self.nexthop = nexthop
self._packed = packed
示例13: __init__
def __init__ (self, packed, nexthop, action, path=None):
NLRI.__init__(self, AFI.l2vpn, SAFI.evpn)
self.nexthop = IP.unpack(nexthop) if nexthop else NoNextHop
self.action = action
self._packed = packed
示例14: unpack
def unpack (cls, data, negotiated):
nlris = []
# -- Reading AFI/SAFI
_afi,_safi = unpack('!HB',data[:3])
afi,safi = AFI.create(_afi),SAFI.create(_safi)
offset = 3
# we do not want to accept unknown families
if negotiated and (afi,safi) not in negotiated.families:
raise Notify(3,0,'presented a non-negotiated family %s/%s' % (afi,safi))
# -- Reading length of next-hop
len_nh = ordinal(data[offset])
offset += 1
if (afi,safi) not in Family.size:
raise Notify(3,0,'unsupported %s %s' % (afi,safi))
length,rd = Family.size[(afi,safi)]
if len_nh not in length:
raise Notify(3,0,'invalid %s %s next-hop length %d expected %s' % (afi,safi,len_nh,' or '.join(str(_) for _ in length)))
size = len_nh - rd
# XXX: FIXME: GET IT FROM CACHE HERE ?
nhs = data[offset+rd:offset+rd+size]
nexthops = [nhs[pos:pos+16] for pos in range(0,len(nhs),16)]
# chech the RD is well zero
if rd and sum([int(ordinal(_)) for _ in data[offset:8]]) != 0:
raise Notify(3,0,"MP_REACH_NLRI next-hop's route-distinguisher must be zero")
offset += len_nh
# Skip a reserved bit as somone had to bug us !
reserved = ordinal(data[offset])
offset += 1
if reserved != 0:
raise Notify(3,0,'the reserved bit of MP_REACH_NLRI is not zero')
# Is the peer going to send us some Path Information with the route (AddPath)
addpath = negotiated.addpath.receive(afi,safi)
# Reading the NLRIs
data = data[offset:]
if not data:
raise Notify(3,0,'No data to decode in an MPREACHNLRI but it is not an EOR %d/%d' % (afi,safi))
while data:
if nexthops:
for nexthop in nexthops:
nlri,left = NLRI.unpack_nlri(afi,safi,data,IN.ANNOUNCED,addpath)
nlri.nexthop = NextHop.unpack(nexthop)
nlris.append(nlri)
else:
nlri,left = NLRI.unpack_nlri(afi,safi,data,IN.ANNOUNCED,addpath)
nlris.append(nlri)
if left == data:
raise RuntimeError("sub-calls should consume data")
data = left
return cls(afi,safi,nlris)
示例15: __init__
def __init__ (self, afi, safi, action):
_NLRI.__init__(self,afi,safi,action)
self.action = action
self.afi = afi
self.safi = safi