def unpack (data, negotiated): communities = ExtendedCommunities() while data: if data and len(data) < 8: raise Notify(3,1,'could not decode extended community %s' % str([hex(ord(_)) for _ in data])) communities.add(ExtendedCommunity.unpack(data[:8],negotiated)) data = data[8:] return communities
def unpack (data, negotiated): communities = ExtendedCommunities() while data: if data and len(data) < 8: raise Notify(3,1,'could not decode extended community %s' % str([hex(ordinal(_)) for _ in data])) communities.add(ExtendedCommunity.unpack(data[:8],negotiated)) data = data[8:] return communities
def _extended_community(value): if value[:2].lower() == '0x': if not len(value) % 2: raise ValueError('invalid extended community %s' % value) try: raw = '' for i in range(2, len(value), 2): raw += chr(int(value[i:i + 2], 16)) except ValueError: raise ValueError('invalid extended community %s' % value) if len(raw) != 8: raise ValueError('invalid extended community %s' % value) return ExtendedCommunity.unpack(raw) elif value.count(':'): _known_community = { # header and subheader 'target': chr(0x00) + chr(0x02), 'target4': chr(0x02) + chr(0x02), 'origin': chr(0x00) + chr(0x03), 'origin4': chr(0x02) + chr(0x03), 'l2info': chr(0x80) + chr(0x0A), } _size_community = { 'target': 2, 'target4': 2, 'origin': 2, 'origin4': 2, 'l2info': 4, } components = value.split(':') command = 'target' if len(components) == 2 else components.pop(0) if command not in _known_community: raise ValueError( 'invalid extended community %s (only origin,target or l2info are supported) ' % command) if len(components) != _size_community[command]: raise ValueError( 'invalid extended community %s, expecting %d fields ' % (command, len(components))) header = _known_community[command] if command == 'l2info': # encaps, control, mtu, site return ExtendedCommunity.unpack( header + pack('!BBHH', *[int(_) for _ in components])) if command in ('target', 'origin'): # global admin, local admin ga, la = components if '.' in ga or '.' in la: gc = ga.count('.') lc = la.count('.') if gc == 0 and lc == 3: # ASN first, IP second return ExtendedCommunity.unpack(header + pack( '!HBBBB', int(ga), *[int(_) for _ in la.split('.')])) if gc == 3 and lc == 0: # IP first, ASN second return ExtendedCommunity.unpack( header + pack('!BBBBH', *[int(_) for _ in ga.split('.')] + [int(la)])) else: if command == 'target': if ga.upper().endswith('L'): return ExtendedCommunity.unpack( _known_community['target4'] + pack('!LH', int(ga[:-1]), int(la))) else: return ExtendedCommunity.unpack( header + pack('!HI', int(ga), int(la))) if command == 'origin': if ga.upper().endswith('L'): return ExtendedCommunity.unpack( _known_community['origin4'] + pack('!LH', int(ga), int(la))) else: return ExtendedCommunity.unpack( header + pack('!HI', int(ga), int(la))) if command == 'target4': return ExtendedCommunity.unpack( _known_community['target4'] + pack('!LH', int(ga[:-1]), int(la)), None) if command == 'orgin4': return ExtendedCommunity.unpack( _known_community['origin4'] + pack('!LH', int(ga[:-1]), int(la)), None) raise ValueError('invalid extended community %s' % command) else: raise ValueError('invalid extended community %s - lc+gc' % value)
from exabgp.bgp.message.update.attribute.community.extended.encapsulation import Encapsulation from exabgp.bgp.message.update.attribute.community.extended.l2info import L2Info from exabgp.bgp.message.update.attribute.community.extended.origin import OriginASNIP from exabgp.bgp.message.update.attribute.community.extended.origin import OriginIPASN from exabgp.bgp.message.update.attribute.community.extended.origin import OriginASN4Number from exabgp.bgp.message.update.attribute.community.extended.rt import RouteTargetASNIP from exabgp.bgp.message.update.attribute.community.extended.rt import RouteTargetIPASN from exabgp.bgp.message.update.attribute.community.extended.rt import RouteTargetASN4Number from exabgp.bgp.message.update.attribute.community.extended.traffic import TrafficRate from exabgp.bgp.message.update.attribute.community.extended.traffic import TrafficAction from exabgp.bgp.message.update.attribute.community.extended.traffic import TrafficRedirect from exabgp.bgp.message.update.attribute.community.extended.traffic import TrafficMark from exabgp.bgp.message.update.attribute.community.extended.traffic import TrafficNextHop ExtendedCommunity.register_extended(Encapsulation) ExtendedCommunity.register_extended(L2Info) ExtendedCommunity.register_extended(OriginASNIP) ExtendedCommunity.register_extended(OriginIPASN) ExtendedCommunity.register_extended(OriginASN4Number) ExtendedCommunity.register_extended(RouteTargetASNIP) ExtendedCommunity.register_extended(RouteTargetIPASN) ExtendedCommunity.register_extended(RouteTargetASN4Number) ExtendedCommunity.register_extended(TrafficRate) ExtendedCommunity.register_extended(TrafficAction) ExtendedCommunity.register_extended(TrafficRedirect) ExtendedCommunity.register_extended(TrafficMark) ExtendedCommunity.register_extended(TrafficNextHop)