node.jsnetwork-programmingcidr

How to use code (Node.js) to subtract a subnet from a larger range


I want to take out small subnets from one or more given IP address ranges.

If I have e.g. in a list the net 192.168.0.0/16 and someone wants to take out the net 192.168.6.0/24, the program should give me a list with these addresses:

192.168.0.0/22        (192.168.0.0 - 192.168.3.255)
192.168.4.0/23        (192.168.4.0 - 192.168.5.255)
192.168.7.0/24        (192.168.7.0 - 192.168.7.255)
192.168.8.0/21        (192.168.8.0 - 192.168.15.255)
192.168.16.0/20       (192.168.16.0 - 192.168.31.255)
192.168.32.0/19       (192.168.32.0 - 192.168.63.255)
192.168.64.0/18       (192.168.64.0 - 192.168.127.255)
192.168.128.0/17      (192.168.128.0 - 192.168.255.255)

Is there any ready library for this? or what would be a good approach?

So far, I have only searched for solution approach or libraries. Maybe in the example above you should split the large mesh until it equals the size of the small one (/24), then remove the network and then try to merge the networks back into larger ones?


Solution

  • I found the npm package "cidr-tools". https://www.npmjs.com/package/cidr-tools This NPM Package have the functions "merge" and "exclude"