Comments (7)
I think this works?
using IPNetworkHelper;
using Microsoft.AspNetCore.HttpOverrides;
// We start with a single network
var networks = new List<IPNetwork>() {
NetworkHelper.Parse("95.0.0.0/8")
};
// Define which networks to extract
var extract = new[]
{
"95.126.0.0/17",
"95.127.0.0/19",
"95.127.162.0/23",
"95.127.176.0/21",
"95.127.184.0/23"
}.Select(x => NetworkHelper.Parse(x)); // Convert strings to IPNetworks
// For each network we want to extract
foreach (var e in extract)
{
// Find the target network in our networks that contains the network to be extracted
// (and make sure there's only one match, but that's not really necessary)
var target = networks.Where(n => n.Contains(e.Prefix)).Single();
// Remove the target network from the list
networks.Remove(target);
// Extract the network from the target and add the results to our networks list
networks.AddRange(target.Extract(e));
}
// Print results
Console.WriteLine($"Result:\n{string.Join("\n", networks.OrderBy(n => n, IPNetworkComparer.Default).Select(n => $"{n.Prefix}/{n.PrefixLength}"))}");
Output:
Result:
95.0.0.0/10
95.64.0.0/11
95.96.0.0/12
95.112.0.0/13
95.120.0.0/14
95.124.0.0/15
95.126.0.0/17
95.126.128.0/17
95.127.0.0/19
95.127.32.0/19
95.127.64.0/18
95.127.128.0/19
95.127.160.0/23
95.127.162.0/23
95.127.164.0/22
95.127.168.0/21
95.127.176.0/21
95.127.184.0/23
95.127.186.0/23
95.127.188.0/22
95.127.192.0/18
95.128.0.0/9
This can probably be solved recursively etc. but you get the gist.
from ipnetworkhelper.
I'll do you one better: You can update to 1.0.4 which now has an overload that does exactly this and essentially turns the above into a one-liner.
NetworkHelper.Parse(network).Extract(desiredNetworks);
So this does the trick:
using IPNetworkHelper;
// Define which networks to extract
var networks = new[]
{
"95.126.0.0/17",
"95.127.0.0/19",
"95.127.162.0/23",
"95.127.176.0/21",
"95.127.184.0/23"
}.Select(x => NetworkHelper.Parse(x)); // Convert strings to IPNetworks
// Extract networks
var result = NetworkHelper.Parse("95.0.0.0/8").Extract(networks);
// Print results
Console.WriteLine($"Result:\n{string.Join("\n", result.OrderBy(n => n, IPNetworkComparer.Default).Select(n => $"{n.Prefix}/{n.PrefixLength}"))}");
from ipnetworkhelper.
Hi Rob,
first of all thanks for the quick response.
I was doing some testing with real subnets I need to work with and in most cases works, but I have a few cases that is throwing this error:
"Network is larger than network"
sorry I'm not a network expert to understand how you are doing the subnetting but I don't see any overlaping in the original ip subnets I need to extract.
I paste you the code I'm using with 3 cases I'm getting this error. the real use case is passing all the subnets and extract from all internet (0.0.0.0/0) but I'm using /8 subnet in each particular case to find which one fails.
using IPNetworkHelper;
var networks = new[]
{
"37.10.128.0/17",
"37.12.128.0/18",
"37.13.64.0/18",
"37.13.128.0/17"
}.Select(x => NetworkHelper.Parse(x));
Console.WriteLine("****List of Subnets****\r\n");
foreach (var n in networks)
{
Console.WriteLine((n.Prefix + "/" + n.PrefixLength).PadRight(18) + "".PadRight(3) + n.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + n.GetLastIP().ToString().PadRight(16));
}
try {
var result = NetworkHelper.Parse("37.0.0.0/8").Extract(networks);
Console.WriteLine("\r\n****restriction List****\r\n");
foreach (var item in result)
{
Console.WriteLine((item.Prefix + "/" + item.PrefixLength).PadRight(18) + "".PadRight(3) + item.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + item.GetLastIP().ToString().PadRight(16));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
///////////////////////////////////
var networks = new[]
{
"95.124.0.0/16",
"95.125.80.0/21",
"95.125.128.0/18",
"95.126.0.0/17",
"95.127.0.0/19",
"95.127.162.0/23",
"95.127.164.0/23",
"95.127.170.0/23",
"95.127.172.0/22",
"95.127.176.0/21",
"95.127.184.0/23",
"95.127.192.0/18"
}.Select(x => NetworkHelper.Parse(x));
Console.WriteLine("****List of Subnets****\r\n");
foreach (var n in networks)
{
Console.WriteLine((n.Prefix + "/" + n.PrefixLength).PadRight(18) + "".PadRight(3) + n.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + n.GetLastIP().ToString().PadRight(16));
}
try {
var result = NetworkHelper.Parse("95.0.0.0/8").Extract(networks);
Console.WriteLine("\r\n****restriction List****\r\n");
foreach (var item in result)
{
Console.WriteLine((item.Prefix + "/" + item.PrefixLength).PadRight(18) + "".PadRight(3) + item.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + item.GetLastIP().ToString().PadRight(16));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
////////////////////////////////////////////////////////////
var networks = new[]
{
"176.80.0.0/16",
"176.82.0.0/16",
"176.83.0.0/16",
"176.87.0.0/18",
"176.87.64.0/19"
}.Select(x => NetworkHelper.Parse(x));
Console.WriteLine("****List of Subnets****\r\n");
foreach (var n in networks)
{
Console.WriteLine((n.Prefix + "/" + n.PrefixLength).PadRight(18) + "".PadRight(3) + n.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + n.GetLastIP().ToString().PadRight(16));
}
try {
var result = NetworkHelper.Parse("176.0.0.0/8").Extract(networks);
Console.WriteLine("\r\n****restriction List****\r\n");
foreach (var item in result)
{
Console.WriteLine((item.Prefix + "/" + item.PrefixLength).PadRight(18) + "".PadRight(3) + item.GetFirstIP().ToString().PadRight(16) + "".PadRight(3) + item.GetLastIP().ToString().PadRight(16));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
from ipnetworkhelper.
Just an update.
I was looking at the library code and in NetworkHelper.cs line 200:
if (desiredNetwork.PrefixLength <= network.PrefixLength)
if I remove the = works does not fail in the cases I shared. I'm not sure if this brokes any other logic but I will do further testing
if (desiredNetwork.PrefixLength < network.PrefixLength)
from ipnetworkhelper.
The list of networks you provide in the examples contains a subnet that will be created naturally by extracting the previous networks. I guess I could skip that network. Let me see what I can do.
from ipnetworkhelper.
Also one more thing, the original subnets that you want to extract are still part of the result. is this intended?
from ipnetworkhelper.
Also one more thing, the original subnets that you want to extract are still part of the result. is this intended?
Yes, that is intended; the result contains all networks that cover the original network after extracting one (or, since today, more) network(s) including the extracted one.
Version 1.0.5 (see f1d8300) now ignores (or 'skips') duplicate networks. (May take a few minutes for it to show up on NuGet)
from ipnetworkhelper.
Related Issues (1)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ipnetworkhelper.