Search code examples
ripcidr

Get the opposite of a CIDR Range


I'd like to make a list of CIDR ranges that represent the addresses outside of the CIDR range I specify.

As a simplified example:

If I have the IP ranges from 8.8.8.8 to 8.8.8.10, I would be able to express this with CIDR ranges 8.8.8.8/31 and 8.8.8.10/32. But how could I express the opposite of these ranges in CIDR notation?

I'm new to CIDR so if this kind of tool exists already in a popular format please excuse my question.

As an additional note, I would like to eventually implement this filtering in R so if you can express your answer using R that would be preferable but I'm really mostly interested in how to build the algorithm to solve this kind of problem.


Solution

  • Basically, if you have a range (X, Y) of IP addresses and want to represent all IP addresses that are not in (X, Y) then it can be expressed as two ranges: (0.0.0.0, X-1) and (Y+1, 255.255.255.255).

    Then you just convert the two ranges into CIDRs. A range may result in multiple CIDRs.

    In the specific example you want ranges: (0.0.0.0, 8.8.8.7) and (8.8.8.11, 255.255.255.255). I have no idea of how to do this in R but here is a handy calculator here: http://www.ipaddressguide.com/cidr

    (0.0.0.0, 8.8.8.7):

    • 0.0.0.0/5
    • 8.0.0.0/13
    • 8.8.0.0/21
    • 8.8.8.0/29

    (8.8.8.11, 255.255.255.255):

    • 8.8.8.11/32

    • 8.8.8.12/30

    • 8.8.8.16/28
    • 8.8.8.32/27
    • 8.8.8.64/26
    • 8.8.8.128/25
    • 8.8.9.0/24
    • 8.8.10.0/23
    • 8.8.12.0/22
    • 8.8.16.0/20
    • 8.8.32.0/19
    • 8.8.64.0/18
    • 8.8.128.0/17
    • 8.9.0.0/16
    • 8.10.0.0/15
    • 8.12.0.0/14
    • 8.16.0.0/12
    • 8.32.0.0/11
    • 8.64.0.0/10
    • 8.128.0.0/9
    • 9.0.0.0/8
    • 10.0.0.0/7
    • 12.0.0.0/6
    • 16.0.0.0/4
    • 32.0.0.0/3
    • 64.0.0.0/2
    • 128.0.0.0/1