job / aggregate6

IPv4 and IPv6 prefix aggregation tool
BSD 2-Clause "Simplified" License
145 stars 19 forks source link

Suggestion: calculating aggregated prefix list with "holes"/exceptions #18

Open toreanderson opened 1 year ago

toreanderson commented 1 year ago

In case you're taking feature requests, it would be useful to generate an aggregated prefix list but with some sub-prefixes removed:

Mock-up to explain what I mean:

$ echo 192.0.2.0/25 192.0.2.128/26 192.0.2.192/27 192.0.2.224/27 > prefixes.txt
$ echo 192.0.2.3/32 192.0.2.128/30 > exceptions.txt
$ aggregate6 -e exceptions.txt prefixes.txt
192.0.2.0/31
192.0.2.2/32
192.0.2.4/30
192.0.2.8/29
192.0.2.16/28
192.0.2.32/27
192.0.2.64/26
192.0.2.132/30
192.0.2.136/29
192.0.2.144/28
192.0.2.160/27
192.0.2.192/26

If on the other hand you're not taking feature requests, feel free to close this issue. In any case, thank you for this very useful tool!

movatica commented 1 year ago

I use this:

def remove_prefix(aggregated_tree, prefix):
    # simple case: prefix itself or children of it are in the tree
    # those can simply be deleted
    # this will also include the prefix itself
    children = aggregated_tree.search_covered(prefix)

    if children:
        for child in children:
            aggregated_tree.delete(child.prefix)

    # complex case: a supernet is contained in the tree
    # this has to be split up
    parent = aggregated_tree.search_worst(parent)

    if parent:
        aggregated_tree.delete(parent.prefix)
        siblings = ip_network(parent.prefix).address_exclude(ip_network(prefix))

        for sibling in siblings:
            aggregated_tree.add(str(sibling))

First, you run aggregate for both prefixes.txt, and exceptions.txt, then for all entries in exceptions.txt you run the remove_prefix function on prefixes.txt. Could be integrated into aggregate6, or added as a second script.