question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: IPNetwork Subtract

See original GitHub issue

Looking at the source code, I see a method called: TrySubstractNetwork. I attempted to use a slightly modified version of this function and it was extremely slow.

I am trying to take two lists IncludedRanages and ExcludedRanges and have something consolidate adjacent and overlapping ranges/networks. Is this something easily added?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lduchosalcommented, Sep 14, 2021

@RobThree thanks, I’ll dig into your proposal.

1reaction
RobThreecommented, Sep 13, 2021

Rather than trying to subtract ip address via range, I found that adding subnet via binary elimination might be faster. The idea is to split network into two until all the split networks are free of the subtract target. These subnets can be farther split to subtract multiple networks. Finally the split parts free from subtract networks can be joined together.

Exactly. This is how it’s supposed to be done and can be very fast since all you do is split recursively. I have implemented the algorithm here (disclaimer, stating the obvious: author here).

For an example:

var network = NetworkHelper.Parse("192.168.0.0/16");
var desired = NetworkHelper.Parse("192.168.10.16/28");
var result = network.Extract(desired);

// Result:
// 192.168.0.0/21
// 192.168.8.0/23
// 192.168.10.0/28
// 192.168.10.16/28
// 192.168.10.32/27
// 192.168.10.64/26
// 192.168.10.128/25
// 192.168.11.0/24
// 192.168.12.0/22
// 192.168.16.0/20
// 192.168.32.0/19
// 192.168.64.0/18
// 192.168.128.0/17

You can also pass “0.0.0.0/28” to ‘extract’ any /28 from the given network. Works with IPv6 as well. What may help is using a visual subnet calculator like this one (no affiliation) to help visualize the concept. @lduchosal’s exampe would be done as:

var network = NetworkHelper.Parse("0.0.0.0/0");
var desired = NetworkHelper.Parse("10.0.0.1/32");

var result = network.Extract(desired);
var resulttext = string.Join("\n", result.Select(n => $"{n.Prefix}/{n.PrefixLength}"));
Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · lduchosal/ipnetwork
IPNetwork command line and C# library take care of complex network, IP, IPv4, IPv6, netmask, ... Feature request: IPNetwork Subtract enhancement help wanted....
Read more >
How to diff a range of IP addresses within ...
Given an IP range, how can code to subtract an IP address or IP address range from that range? Example 1: original_range =...
Read more >
netaddr Documentation
IPNetwork objects are used to represent subnets, networks or VLANs ... Please raise bugs and feature requests against this as you find them....
Read more >
Source code for netaddr.ip
[docs] def __sub__(self, num): """ Subtract the numerical value of this IP address from num providing the result as a new IPAddress object....
Read more >
FREE Advanced Subnet Calculator - IP Calculator
Handy and easy-to-use Subnet Calculator. Use our FREE online Subnet Calculator to create lists of addresses for subnets.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found