0

I'd like to locate a library or tool to facilitate enumeration of IP address blocks within ranges. Say I have any two non-overlapping ranges, and I want to find out what's the minimum set of IP address blocks that fully covers the interstitial space between them.

Here are some examples:

Between 0.0.0.0/8 and 10.0.0.0/8 (edges not included) lie: 1.0.0.0/8, 2.0.0.0/7, 4.0.0.0/6 and 8.0.0.0/7, and that occupies all the space and it is the minimum expression.

Between 10.0.0.0/8 and 127.0.0.0/8 (edges not included) lie: 11.0.0.0/8, 12.0.0.0/6, 16.0.0.0/4, 32.0.0.0/3, 64.0.0.0/3, 96.0.0.0/4, 112.0.0.0/5, 120.0.0.0/6, 124.0.0.0/7 and 126.0.0.0/8, and that occupies all the space and it is the minimum expression.

Between 127.0.0.0/8 and 169.254.0.0/16 (edges not included) lie: 128.0.0.0/3, 160.0.0.0/5, 168.0.0.0/8, 169.0.0.0/9, 169.128.0.0/10, 169.192.0.0/11, 169.224.0.0/12, 169.240.0.0/13, 169.248.0.0/14 and 169.252.0.0/15, and that occupies all the space and it is the minimum expression.

At the moment I'm calculating this by hand, but it does get tedious and error-prone. I'd like to hear perhaps of some function, in any programming language, probably belonging to a subnet calculator, or failing that a web page, to help calculate this. I thought I'd ask before I write my own function.

jchevali
  • 171
  • 10

2 Answers2

0

I've managed an answer to my own question in Python:

from netaddr import *

iprange_to_cidrs(IPAddress(IPSet(['0.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['10.0.0.0/8']).iprange().first - 1))
iprange_to_cidrs(IPAddress(IPSet(['10.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['127.0.0.0/8']).iprange().first - 1))
iprange_to_cidrs(IPAddress(IPSet(['127.0.0.0/8']).iprange().last + 1), IPAddress(IPSet(['169.254.0.0/16']).iprange().first - 1))

Simpler ranges consisting of just one CIDR can also be resolved by means of iprange_to_cidrs(), as shown in a related answer.

Community
  • 1
  • 1
jchevali
  • 171
  • 10
0

You can do this in Java with the following code using the IPAddress Java library. This code works for both IPv4 and IPv6. Disclaimer: I am the project manager of that library.

static void print(String lower, String upper) {
    IPAddress lowerAddr = new IPAddressString(lower).getAddress();
    IPAddress upperAddr = new IPAddressString(upper).getAddress();
    System.out.println("Between " + lowerAddr + " and " + upperAddr + ": " +
        Arrays.asList(spanInBetween(lowerAddr, upperAddr)));
}
    
static IPAddress[] spanInBetween(IPAddress lower, IPAddress upper) {
    return lower.getUpper().increment(1).
        spanWithPrefixBlocks(upper.increment(-1));
}

Output:

Between 0.0.0.0/8 and 10.0.0.0/8: [1.0.0.0/8, 2.0.0.0/7, 4.0.0.0/6, 8.0.0.0/7]
Between 10.0.0.0/8 and 127.0.0.0/8: [11.0.0.0/8, 12.0.0.0/6, 16.0.0.0/4, 32.0.0.0/3, 64.0.0.0/3, 96.0.0.0/4, 112.0.0.0/5, 120.0.0.0/6, 124.0.0.0/7, 126.0.0.0/8]
Between 127.0.0.0/8 and 169.254.0.0/16: [128.0.0.0/3, 160.0.0.0/5, 168.0.0.0/8, 169.0.0.0/9, 169.128.0.0/10, 169.192.0.0/11, 169.224.0.0/12, 169.240.0.0/13, 169.248.0.0/14, 169.252.0.0/15]
Sean F
  • 4,344
  • 16
  • 30