示例#1
0
 def __init__(self, sample, terminal, community=None):
     self.sample = sample
     self.terminal = terminal
     bitmask = self._sample[sample] | self._terminal[terminal]
     ExtendedCommunity.__init__(
         self, community if community is not None else pack("!BBLBB", 0x80, 0x07, 0, 0, bitmask)
     )
示例#2
0
文件: traffic.py 项目: itbj/exabgp
 def __init__(self, sample, terminal, community=None):
     self.sample = sample
     self.terminal = terminal
     bitmask = self._sample[sample] | self._terminal[terminal]
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             '!2sLBB', self._subtype(), 0, 0, bitmask))
示例#3
0
 def __init__(self, sequence, sticky=False, community=None):
     self.sequence = sequence
     self.sticky = sticky
     ExtendedCommunity.__init__(
         self, community if community else pack(
             '!2sBxI', self._subtype(
                 transitive=True), 1 if sticky else 0, sequence))
示例#4
0
    def __init__(self, order, reserved=0, community=None):
        self.order = order
        self.reserved = reserved

        ExtendedCommunity.__init__(
            self, community if community is not None else pack("!2sIH", self._subtype(), order, reserved)
        )
示例#5
0
 def __init__(self, encaps, control, mtu, reserved, community=None):
     self.encaps = encaps
     self.control = control
     self.mtu = mtu
     self.reserved = reserved
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBLH", 0x80, 0x0A, 0, self.tunnel_type))
示例#6
0
	def __init__ (self, tunnel_type, community=None):
		self.tunnel_type = tunnel_type
		ExtendedCommunity.__init__(
			self,community if community is not None else pack(
				"!BBLH",
				0x03,0x0C,
				0,self.tunnel_type
			)
		)
示例#7
0
 def __init__(self, encaps, control, mtu, reserved, community=None):
     self.encaps = encaps
     self.control = control
     self.mtu = mtu
     self.reserved = reserved
     # reserved is called preference in draft-ietf-l2vpn-vpls-multihoming-07
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!2sBBHH", self._subtype(), encaps, control, mtu, reserved))
示例#8
0
	def __init__ (self, asn, speed, community=None):
		self.asn = asn
		self.speed = speed
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!Hf",asn,speed
			)
		)
示例#9
0
 def __init__(self, trans, asn, target, direction, community=None):
     self.asn = asn
     self.target = target
     self.direction = direction
     self.transitive = trans
     new_target = (direction << 14) + target
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!2sLH", self._subtype(self.transitive), asn, new_target))
示例#10
0
	def __init__ (self, tunnel_type, community=None):
		self.tunnel_type = tunnel_type
		ExtendedCommunity.__init__(
			self,community if community is not None else pack(
				"!2sLH",
				self._packedTypeSubtype(),
				0,self.tunnel_type
			)
		)
示例#11
0
	def __init__ (self, copy, community=None):
		self.copy = copy
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sLH",
				self._subtype(),
				0,1 if copy else 0
			)
		)
示例#12
0
	def __init__ (self, dscp, community=None):
		self.dscp = dscp
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sLBB",
				self._subtype(),
				0,0,dscp
			)
		)
示例#13
0
	def __init__ (self, dscp, community=None):
		self.dscp = dscp
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!BBLBB",
				0x80,0x09,
				0,0,dscp
			)
		)
示例#14
0
	def __init__ (self, copy, community=None):
		self.copy = copy
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!BBLH",
				0x80,0x00,
				0,1 if copy else 0
			)
		)
示例#15
0
	def __init__ (self, copy, community=None):
		self.copy = copy
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sLH",
				self._subtype(),
				0,1 if copy else 0
			)
		)
示例#16
0
	def __init__ (self, dscp, community=None):
		self.dscp = dscp
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sLBB",
				self._subtype(),
				0,0,dscp
			)
		)
示例#17
0
	def __init__ (self, asn, rate, community=None):
		self.asn = asn
		self.rate = rate
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!BBHf",
				0x80,0x06,
				asn,rate
			)
		)
示例#18
0
	def __init__ (self, asn, rate, community=None):
		self.asn = asn
		self.rate = rate
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sHf",
				self._subtype(),
				asn,rate
			)
		)
示例#19
0
	def __init__ (self, asn, target, community=None):
		self.asn = asn
		self.target = target
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sHL",
				self._subtype(),
				asn,target
			)
		)
示例#20
0
	def __init__ (self, asn, target, community=None):
		self.asn = asn
		self.target = target
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!BBHL",
				0x80,0x08,
				asn,target
			)
		)
示例#21
0
	def __init__ (self, asn, rate, community=None):
		self.asn = asn
		self.rate = rate
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sHf",
				self._subtype(),
				asn,rate
			)
		)
示例#22
0
	def __init__ (self, asn, target, community=None):
		self.asn = asn
		self.target = target
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sHL",
				self._subtype(),
				asn,target
			)
		)
示例#23
0
文件: chso.py 项目: Empia/exabgp
	def __init__ (self, order, reserved=0, community=None):
		self.order = order
		self.reserved = reserved

		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sIH",
				self._subtype(),
				order,reserved
			)
		)
示例#24
0
	def __init__ (self, sequence, sticky=False, community=None):
		self.sequence = sequence
		self.sticky = sticky
		ExtendedCommunity.__init__(
			self,
			community if community else pack(
				'!2sBxI',
				self._subtype(transitive=True),
				1 if sticky else 0,
				sequence
			)
		)
示例#25
0
	def __init__ (self, sample, terminal, community=None):
		self.sample = sample
		self.terminal = terminal
		bitmask = self._sample[sample] | self._terminal[terminal]
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				'!2sLBB',
				self._subtype(),
				0,0,bitmask
			)
		)
示例#26
0
	def __init__ (self, ip, copy, community=None):
		self.ip = ip
		self.copy = copy
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2s4sH",
				self._subtype(),
				ip.pack(),
				1 if copy else 0
			)
		)
示例#27
0
	def __init__ (self, trans, asn, target, direction, community=None):
		self.asn = asn
		self.target = target
		self.direction = direction
		self.transitive = trans
		new_target = (direction << 14) + target
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!2sLH",
				self._subtype(self.transitive),
				asn,new_target
			)
		)
示例#28
0
	def __init__ (self, encaps, control, mtu, reserved, community=None):
		self.encaps = encaps
		self.control = control
		self.mtu = mtu
		self.reserved = reserved
		# reserved is called preference in draft-ietf-l2vpn-vpls-multihoming-07
		ExtendedCommunity.__init__(
			self,
			community if community is not None else pack(
				"!BBBBHH",
				0x80,0x0A,
				encaps,control,
				mtu,reserved
			)
		)
示例#29
0
文件: __init__.py 项目: asnd/exabgp
	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
示例#30
0
	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
示例#31
0
 def __init__(self, asn, speed, community=None):
     self.asn = asn
     self.speed = speed
     ExtendedCommunity.__init__(
         self,
         community if community is not None else pack("!Hf", asn, speed))
示例#32
0
 def __init__(self, tunnel_type, community=None):
     self.tunnel_type = tunnel_type
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBLH", 0x03, 0x0C, 0, self.tunnel_type))
示例#33
0
 def __init__(self, dscp, community=None):
     self.dscp = dscp
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBLBB", 0x80, 0x09, 0, 0, dscp))
示例#34
0
 def __init__(self, copy, community=None):
     self.copy = copy
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBLH", 0x80, 0x00, 0, 1 if copy else 0))
示例#35
0
 def __init__(self, asn, rate, community=None):
     self.asn = asn
     self.rate = rate
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBHf", 0x80, 0x06, asn, rate))
示例#36
0
 def __init__(self, asn, target, community=None):
     self.asn = asn
     self.target = target
     ExtendedCommunity.__init__(
         self, community if community is not None else pack(
             "!BBHL", 0x80, 0x08, asn, target))
示例#37
0
def _extended_community(value):
    if value[:2].lower() == '0x':
        # we could raise if the length is not 8 bytes (16 chars)
        if len(value) % 2:
            raise ValueError('invalid extended community %s' % value)
        raw = ''.join(
            [chr(int(value[_:_ + 2], 16)) for _ in range(2, len(value), 2)])
        return ExtendedCommunity.unpack(raw)
    elif value.count(':'):
        components = value.split(':')
        command = 'target' if len(components) == 2 else components.pop(0)

        if command not in _HEADER:
            raise ValueError(
                'invalid extended community %s (only origin,target or l2info are supported) '
                % command)

        if len(components) != _SIZE[command]:
            raise ValueError(
                'invalid extended community %s, expecting %d fields ' %
                (command, len(components)))

        header = _HEADER.get(command, None)

        if header is None:
            raise ValueError('unknown extended community %s' % command)

        if command == 'l2info':
            # encaps, control, mtu, site
            return ExtendedCommunity.unpack(
                header + pack('!BBHH', *[int(_) for _ in components]))

        _ga, _la = components
        ga, la = _ga.upper(), _la.upper()

        if command in ('target', 'origin'):
            # global admin, local admin
            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)]))

        iga = int(ga[:-1]) if 'L' in ga else int(ga)
        ila = int(la[:-1]) if 'L' in la else int(la)

        if command == 'target':
            if ga.endswith('L') or iga > _SIZE_H:
                return ExtendedCommunity.unpack(
                    _HEADER['target4'] + pack('!LH', iga, ila), None)
            else:
                return ExtendedCommunity.unpack(header + pack('!HI', iga, ila),
                                                None)
        if command == 'origin':
            if ga.endswith('L') or iga > _SIZE_H:
                return ExtendedCommunity.unpack(
                    _HEADER['origin4'] + pack('!LH', iga, ila), None)
            else:
                return ExtendedCommunity.unpack(header + pack('!HI', iga, ila),
                                                None)

        if command == 'target4':
            return ExtendedCommunity.unpack(
                _HEADER['target4'] + pack('!LH', iga, ila), None)

        if command == 'orgin4':
            return ExtendedCommunity.unpack(
                _HEADER['origin4'] + pack('!LH', iga, ila), None)

        if command in ('redirect', ):
            return ExtendedCommunity.unpack(header + pack('!HL', iga, ila),
                                            None)

        raise ValueError('invalid extended community %s' % command)
    elif value == 'redirect-to-nexthop':
        header = _HEADER[value]
        return ExtendedCommunity.unpack(header + pack('!HL', 0, 0), None)
    else:
        raise ValueError('invalid extended community %s - lc+gc' % value)
示例#38
0
 def __eq__(self, other):
     return self.COMMUNITY_SUBTYPE == other.COMMUNITY_SUBTYPE and ExtendedCommunity.__eq__(
         self, other)
示例#39
0
    def _parse_extended_community(self, scope, data):
        SIZE_H = 0xFFFF

        if data[:2].lower() == '0x':
            try:
                raw = ''
                for i in range(2, len(data), 2):
                    raw += chr(int(data[i:i + 2], 16))
            except ValueError:
                raise ValueError('invalid extended community %s' % data)
            if len(raw) != 8:
                raise ValueError('invalid extended community %s' % data)
            return ExtendedCommunity.unpack(raw, None)
        elif data.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),
                'redirect': chr(0x80) + chr(0x08),
                'l2info': chr(0x80) + chr(0x0A),
            }

            _size_community = {
                'target': 2,
                'target4': 2,
                'origin': 2,
                'origin4': 2,
                'redirect': 2,
                'l2info': 4,
            }

            components = data.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]),
                    None)

            if command in ('target', 'origin'):
                # global admin, local admin
                _ga, _la = components
                ga, la = _ga.upper(), _la.upper()

                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('.')]),
                            None)
                    if gc == 3 and lc == 0:
                        # IP first, ASN second
                        return ExtendedCommunity.unpack(
                            header +
                            pack('!BBBBH', *
                                 [int(_) for _ in ga.split('.')] + [int(la)]),
                            None)
                else:
                    iga = int(ga[:-1]) if 'L' in ga else int(ga)
                    ila = int(la[:-1]) if 'L' in la else int(la)
                    if command == 'target':
                        if ga.endswith('L') or iga > SIZE_H:
                            return ExtendedCommunity.unpack(
                                _known_community['target4'] +
                                pack('!LH', iga, ila), None)
                        else:
                            return ExtendedCommunity.unpack(
                                header + pack('!HI', iga, ila), None)
                    if command == 'origin':
                        if ga.endswith('L') or iga > SIZE_H:
                            return ExtendedCommunity.unpack(
                                _known_community['origin4'] +
                                pack('!LH', iga, ila), None)
                        else:
                            return ExtendedCommunity.unpack(
                                header + pack('!HI', iga, ila), None)

            if command == 'target4':
                iga = int(ga[:-1]) if 'L' in ga else int(ga)
                ila = int(la[:-1]) if 'L' in la else int(la)
                return ExtendedCommunity.unpack(
                    _known_community['target4'] + pack('!LH', iga, ila), None)

            if command == 'orgin4':
                iga = int(ga[:-1]) if 'L' in ga else int(ga)
                ila = int(la[:-1]) if 'L' in la else int(la)
                return ExtendedCommunity.unpack(
                    _known_community['origin4'] + pack('!LH', iga, ila), None)

            if command in ('redirect', ):
                ga, la = components
                return ExtendedCommunity.unpack(
                    header + pack('!HL', int(ga), long(la)), None)

            raise ValueError('invalid extended community %s' % command)
        else:
            raise ValueError('invalid extended community %s - lc+gc' % data)
示例#40
0
文件: rt.py 项目: earies/exabgp
	def __eq__ (self, other):
		return \
			self.COMMUNITY_SUBTYPE == other.COMMUNITY_SUBTYPE and \
			ExtendedCommunity.__eq__(self,other)
示例#41
0
	def __init__ (self,encaps,control,mtu,reserved,community=None):
		self.encaps = encaps
		self.control = control
		self.mtu = mtu
		self.reserved = reserved
		ExtendedCommunity.__init__(self,community if community is not None else pack("!BBLH",0x80,0x0A,0,self.tunnel_type))
示例#42
0
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),
            'origin': chr(0x00) + chr(0x03),
            'l2info': chr(0x80) + chr(0x0A),
        }

        _size_community = {
            'target': 2,
            'origin': 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':
                    return ExtendedCommunity.unpack(
                        header + pack('!HI', int(ga), int(la)))
                if command == 'origin':
                    return ExtendedCommunity.unpack(
                        header + pack('!IH', int(ga), int(la)))

        raise ValueError('invalid extended community %s' % command)
    else:
        raise ValueError('invalid extended community %s - lc+gc' % value)
示例#43
0
文件: parser.py 项目: eiffelcn/exabgp
def _extended_community (value):
	if value[:2].lower() == '0x':
		# we could raise if the length is not 8 bytes (16 chars)
		if len(value) % 2:
			raise ValueError('invalid extended community %s' % value)
		raw = ''.join([chr(int(value[_:_+2],16)) for _ in range(2,len(value),2)])
		return ExtendedCommunity.unpack(raw)
	elif value.count(':'):
		components = value.split(':')
		command = 'target' if len(components) == 2 else components.pop(0)

		if command not in _HEADER:
			raise ValueError('invalid extended community %s (only origin,target or l2info are supported) ' % command)

		if len(components) != _SIZE[command]:
			raise ValueError('invalid extended community %s, expecting %d fields ' % (command,len(components)))

		header = _HEADER.get(command,None)

		if header is None:
			raise ValueError('unknown extended community %s' % command)

		if command == 'l2info':
			# encaps, control, mtu, site
			return ExtendedCommunity.unpack(header+pack('!BBHH',*[int(_) for _ in components]))

		_ga,_la = components
		ga,la = _ga.upper(),_la.upper()

		if command in ('target','origin'):
			# global admin, local admin
			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)]))

		iga = int(ga[:-1]) if 'L' in ga else int(ga)
		ila = int(la[:-1]) if 'L' in la else int(la)

		if command == 'target':
			if ga.endswith('L') or iga > _SIZE_H:
				return ExtendedCommunity.unpack(_HEADER['target4']+pack('!LH',iga,ila),None)
			else:
				return ExtendedCommunity.unpack(header+pack('!HI',iga,ila),None)
		if command == 'origin':
			if ga.endswith('L') or iga > _SIZE_H:
				return ExtendedCommunity.unpack(_HEADER['origin4']+pack('!LH',iga,ila),None)
			else:
				return ExtendedCommunity.unpack(header+pack('!HI',iga,ila),None)

		if command == 'target4':
			return ExtendedCommunity.unpack(_HEADER['target4']+pack('!LH',iga,ila),None)

		if command == 'orgin4':
			return ExtendedCommunity.unpack(_HEADER['origin4']+pack('!LH',iga,ila),None)

		if command in ('redirect',):
			return ExtendedCommunity.unpack(header+pack('!HL',iga,ila),None)

		raise ValueError('invalid extended community %s' % command)
	elif value == 'redirect-to-nexthop':
		header = _HEADER[value]
		return ExtendedCommunity.unpack(header+pack('!HL',0,0),None)
	else:
		raise ValueError('invalid extended community %s - lc+gc' % value)
示例#44
0
文件: parser.py 项目: PowerDNS/exabgp
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('!IH',int(ga),int(la)))

		raise ValueError('invalid extended community %s' % command)
	else:
		raise ValueError('invalid extended community %s - lc+gc' % value)
示例#45
0
文件: route.py 项目: Shmuma/exabgp
	def _parse_extended_community (self, scope, data):
		SIZE_H = 0xFFFF

		if data[:2].lower() == '0x':
			try:
				raw = ''
				for i in range(2,len(data),2):
					raw += chr(int(data[i:i+2],16))
			except ValueError:
				raise ValueError('invalid extended community %s' % data)
			if len(raw) != 8:
				raise ValueError('invalid extended community %s' % data)
			return ExtendedCommunity.unpack(raw,None)
		elif data.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),
				'redirect': chr(0x80)+chr(0x08),
				'l2info':   chr(0x80)+chr(0x0A),
			}

			_size_community = {
				'target':   2,
				'target4':  2,
				'origin':   2,
				'origin4':  2,
				'redirect': 2,
				'l2info':   4,
			}

			components = data.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]),None)

			if command in ('target','origin'):
				# global admin, local admin
				_ga,_la = components
				ga,la = _ga.upper(),_la.upper()

				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('.')]),None)
					if gc == 3 and lc == 0:
						# IP first, ASN second
						return ExtendedCommunity.unpack(header+pack('!BBBBH',*[int(_) for _ in ga.split('.')]+[int(la)]),None)
				else:
					iga = int(ga[:-1]) if 'L' in ga else int(ga)
					ila = int(la[:-1]) if 'L' in la else int(la)
					if command == 'target':
						if ga.endswith('L') or iga > SIZE_H:
							return ExtendedCommunity.unpack(_known_community['target4']+pack('!LH',iga,ila),None)
						else:
							return ExtendedCommunity.unpack(header+pack('!HI',iga,ila),None)
					if command == 'origin':
						if ga.endswith('L') or iga > SIZE_H:
							return ExtendedCommunity.unpack(_known_community['origin4']+pack('!LH',iga,ila),None)
						else:
							return ExtendedCommunity.unpack(header+pack('!HI',iga,ila),None)

			if command == 'target4':
				iga = int(ga[:-1]) if 'L' in ga else int(ga)
				ila = int(la[:-1]) if 'L' in la else int(la)
				return ExtendedCommunity.unpack(_known_community['target4']+pack('!LH',iga,ila),None)

			if command == 'orgin4':
				iga = int(ga[:-1]) if 'L' in ga else int(ga)
				ila = int(la[:-1]) if 'L' in la else int(la)
				return ExtendedCommunity.unpack(_known_community['origin4']+pack('!LH',iga,ila),None)

			if command in ('redirect',):
				ga,la = components
				return ExtendedCommunity.unpack(header+pack('!HL',int(ga),long(la)),None)

			raise ValueError('invalid extended community %s' % command)
		else:
			raise ValueError('invalid extended community %s - lc+gc' % data)
示例#46
0
文件: parser.py 项目: ythomas1/exabgp
def _extended_community(value):
    if value[:2].lower() == "0x":
        # we could raise if the length is not 8 bytes (16 chars)
        if len(value) % 2:
            raise ValueError("invalid extended community %s" % value)
        raw = "".join([chr(int(value[_ : _ + 2], 16)) for _ in range(2, len(value), 2)])
        return ExtendedCommunity.unpack(raw)
    elif value.count(":"):
        components = value.split(":")
        command = "target" if len(components) == 2 else components.pop(0)

        if command not in _HEADER:
            raise ValueError("invalid extended community %s (only origin,target or l2info are supported) " % command)

        if len(components) != _SIZE[command]:
            raise ValueError("invalid extended community %s, expecting %d fields " % (command, len(components)))

        header = _HEADER.get(command, None)

        if header is None:
            raise ValueError("unknown extended community %s" % command)

        if command == "l2info":
            # encaps, control, mtu, site
            return ExtendedCommunity.unpack(header + pack("!BBHH", *[int(_) for _ in components]))

        _ga, _la = components
        ga, la = _ga.upper(), _la.upper()

        if command in ("target", "origin"):
            # global admin, local admin
            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)])
                    )

        iga = int(ga[:-1]) if "L" in ga else int(ga)
        ila = int(la[:-1]) if "L" in la else int(la)

        if command == "target":
            if ga.endswith("L") or iga > _SIZE_H:
                return ExtendedCommunity.unpack(_HEADER["target4"] + pack("!LH", iga, ila), None)
            else:
                return ExtendedCommunity.unpack(header + pack("!HI", iga, ila), None)
        if command == "origin":
            if ga.endswith("L") or iga > _SIZE_H:
                return ExtendedCommunity.unpack(_HEADER["origin4"] + pack("!LH", iga, ila), None)
            else:
                return ExtendedCommunity.unpack(header + pack("!HI", iga, ila), None)

        if command == "target4":
            return ExtendedCommunity.unpack(_HEADER["target4"] + pack("!LH", iga, ila), None)

        if command == "orgin4":
            return ExtendedCommunity.unpack(_HEADER["origin4"] + pack("!LH", iga, ila), None)

        if command in ("redirect",):
            return ExtendedCommunity.unpack(header + pack("!HL", iga, ila), None)

        raise ValueError("invalid extended community %s" % command)
    elif value == "redirect-to-nexthop":
        header = _HEADER[value]
        return ExtendedCommunity.unpack(header + pack("!HL", 0, 0), None)
    else:
        raise ValueError("invalid extended community %s - lc+gc" % value)