Пример #1
0
def attribute(tokeniser):
    start = tokeniser()
    if start != '[':
        raise ValueError('invalid attribute, does not starts with [')

    code = tokeniser().lower()
    if not code.startswith('0x'):
        raise ValueError('invalid attribute, code is not 0x hexadecimal')
    try:
        code = int(code, 16)
    except ValueError:
        raise ValueError('invalid attribute, code is not 0x hexadecimal')

    flag = tokeniser().lower()
    if not flag.startswith('0x'):
        raise ValueError('invalid attribute, flag is not 0x hexadecimal')
    try:
        flag = int(flag, 16)
    except ValueError:
        raise ValueError('invalid attribute, flag is not 0x hexadecimal')

    data = tokeniser().lower()
    if not data.startswith('0x'):
        raise ValueError('invalid attribute, data is not 0x hexadecimal')
    if len(data) % 2:
        raise ValueError('invalid attribute, data is not 0x hexadecimal')
    data = concat_bytes_i(
        character(int(data[_:_ + 2], 16)) for _ in range(2, len(data), 2))

    end = tokeniser()
    if end != ']':
        raise ValueError('invalid attribute, does not ends with ]')

    return GenericAttribute(code, flag, data)
Пример #2
0
    def generic_attribute(self, scope, name, command, tokens):
        try:
            start = tokens.pop(0)
            code = tokens.pop(0).lower()
            flag = tokens.pop(0).lower()
            data = tokens.pop(0).lower()
            end = tokens.pop(0)

            if (start, end) != ('[', ']'):
                return self.error.set(self.syntax)

            if not code.startswith('0x'):
                return self.error.set(self.syntax)
            code = int(code[2:], 16)

            if not flag.startswith('0x'):
                return self.error.set(self.syntax)
            flag = int(flag[2:], 16)

            if not data.startswith('0x'):
                return self.error.set(self.syntax)
            raw = ''
            for i in range(2, len(data), 2):
                raw += chr(int(data[i:i + 2], 16))

            try:
                for ((ID, _),
                     klass) in Attribute.registered_attributes.iteritems():
                    if code == ID and flag == klass.FLAG:
                        scope[-1]['announce'][-1].attributes.add(
                            klass.unpack(raw, None))
                        return True
            except Exception:
                pass

            scope[-1]['announce'][-1].attributes.add(
                GenericAttribute(code, flag, raw))
            return True
        except (IndexError, ValueError):
            return self.error.set(self.syntax)
Пример #3
0
def attribute (tokeniser):
	start = tokeniser()
	if start != '[':
		raise ValueError('invalid attribute, does not starts with [')

	code = tokeniser().lower()
	if not code.startswith('0x'):
		raise ValueError('invalid attribute, code is not 0x hexadecimal')
	try:
		code = int(code[2:],16)
	except ValueError:
		raise ValueError('invalid attribute, code is not 0x hexadecimal')

	flag = tokeniser().lower()
	if not flag.startswith('0x'):
		raise ValueError('invalid attribute, flag is not 0x hexadecimal')
	try:
		flag = int(flag[2:],16)
	except ValueError:
		raise ValueError('invalid attribute, flag is not 0x hexadecimal')

	data = tokeniser().lower()
	if not data.startswith('0x'):
		raise ValueError('invalid attribute, data is not 0x hexadecimal')
	if not len(data) % 2:
		raise ValueError('invalid attribute, data is not 0x hexadecimal')
	data = ''.join(chr(int(data[_:_+2],16)) for _ in range(2,len(data),2))

	end = tokeniser()
	if end != ']':
		raise ValueError('invalid attribute, does not ends with ]')

	# XXX: FIXME: class Attribute should have an unpack function which does that
	from exabgp.bgp.message.update.attribute import GenericAttribute

	for ((ID,flag),klass) in Attribute.registered_attributes.iteritems():
		if code == ID and flag == klass.FLAG:
			return klass(data)
	return GenericAttribute(code,flag,data)