Exemplo n.º 1
0
def redirect(tokeniser):
    data = tokeniser()
    if data.count(':') == 1:
        prefix, suffix = data.split(':', 1)
        if prefix.count('.'):
            raise ValueError(
                'this format has been deprecaded as it does not make sense and it is not supported by other vendors'
            )

        asn = int(prefix)
        route_target = int(suffix)

        if asn >= pow(2, 32):
            raise ValueError('asn is a 32 bits number, value too large %s' %
                             asn)
        if asn >= pow(2, 16):
            if route_target >= pow(2, 16):
                raise ValueError(
                    'asn is a 32 bits number, route target can only be 16 bit %s'
                    % route_target)
            return NoNextHop, ExtendedCommunities().add(
                TrafficRedirectASN4(asn, route_target))
        if route_target >= pow(2, 32):
            raise ValueError(
                'route target is a 32 bits number, value too large %s' %
                route_target)
        return NoNextHop, ExtendedCommunities().add(
            TrafficRedirect(asn, route_target))
    else:
        return IP.create(data), ExtendedCommunities().add(
            TrafficNextHop(False))
Exemplo n.º 2
0
def redirect(tokeniser):
    data = tokeniser()
    count = data.count(':')
    if count == 0:
        return IP.create(data), ExtendedCommunities().add(
            TrafficNextHopSimpson(False))
    if count == 1:
        prefix, suffix = data.split(':', 1)
        if prefix.count('.'):
            raise ValueError(
                'this format has been deprecated as it does not make sense and it is not supported by other vendors'
            )

        asn = int(prefix)
        route_target = int(suffix)

        if asn >= pow(2, 32):
            raise ValueError('asn is a 32 bits number, value too large %s' %
                             asn)
        if asn >= pow(2, 16):
            if route_target >= pow(2, 16):
                raise ValueError(
                    'asn is a 32 bits number, route target can only be 16 bit %s'
                    % route_target)
            return NoNextHop, ExtendedCommunities().add(
                TrafficRedirectASN4(asn, route_target))
        if route_target >= pow(2, 32):
            raise ValueError(
                'route target is a 32 bits number, value too large %s' %
                route_target)
        return NoNextHop, ExtendedCommunities().add(
            TrafficRedirect(asn, route_target))
    else:
        explicit_v6 = ']:' in data

        # ipv4
        if not explicit_v6 and data.count(':') == 1:
            return IP.create(data), ExtendedCommunities().add(
                TrafficNextHopSimpson(False))

        # ipv6 using []: notation
        if explicit_v6:
            ip, asn = data.split(']:')
            ip = ip.replace('[', '', 1)
            # FIXME: should this be 2^16 ??
            if asn >= pow(2, 32):
                raise ValueError(
                    'asn is a 32 bits number, value too large %s' % asn)
            return IP.create(ip), ExtendedCommunities().add(
                TrafficRedirectIPv6(ip, asn))

        raise ValueError(
            'it looks like you tried to use an IPv6 but did not enclose it in []'
        )
Exemplo n.º 3
0
    def redirect(self, scope, name, command, tokens):
        try:
            if tokens[0].count(':') == 1:
                prefix, suffix = tokens[0].split(':', 1)
                if prefix.count('.'):
                    raise ValueError(
                        'this format has been deprecaded as it does not make sense and it is not supported by other vendors'
                    )
                else:
                    asn = int(prefix)
                    route_target = int(suffix)
                    if asn >= pow(2, 16):
                        raise ValueError(
                            'asn is a 32 bits number, it can only be 16 bit %s'
                            % route_target)
                    if route_target >= pow(2, 32):
                        raise ValueError(
                            'route target is a 32 bits number, value too large %s'
                            % route_target)
                    scope[-1]['announce'][-1].attributes[
                        Attribute.CODE.EXTENDED_COMMUNITY].add(
                            TrafficRedirect(asn, route_target))
                    return True
            else:
                change = scope[-1]['announce'][-1]
                if change.nlri.nexthop is not NoNextHop:
                    return self.error.set(self.syntax)

                nh = IP.create(tokens.pop(0))
                change.nlri.nexthop = nh
                change.attributes[Attribute.CODE.EXTENDED_COMMUNITY].add(
                    TrafficNextHop(False))
                return True

        except (IndexError, ValueError):
            return self.error.set(self.syntax)