1

I have the following issue : i need to convert range of IP addresses into range of CIDR addresses without losses of addresses. For example, if i have the range 1.1.1.3 - 1.1.1.7, i can convert it into

 1.1.1.1/29

using IPNetwork library, but if i will convert 1.1.1.1/29 to range again i get 1.1.1.1 - 1.1.1.6. How could i divide ranges to get a few ranges in CIDR format without losses ? Will be good if you can show me any C# code to perform it.

Vladyslav Furdak
  • 1,765
  • 2
  • 22
  • 46
  • Does this answer your question? [How can I convert IP range to Cidr in C#?](https://stackoverflow.com/questions/13508231/how-can-i-convert-ip-range-to-cidr-in-c) – Mohammad Nikravan Jun 29 '21 at 23:39

1 Answers1

0

using the IPNetwork2 nuget package, you could subnet into /32 and iterate over the result ips :

Example 8 of documentation :

IPNetwork wholeInternet = IPNetwork.Parse("1.1.1.1/29");
IPNetwork ips = IPNetwork.Subnet(wholeInternet, 32);

Console.WriteLine("All  :");

foreach (IPNetwork ip in ips)
{
    Console.WriteLine("{0}", ip);
}

Output

All  :
1.1.1.0
1.1.1.1
1.1.1.2
1.1.1.3
1.1.1.4
1.1.1.5
1.1.1.6
1.1.1.7
LukeSkywalker
  • 341
  • 2
  • 7