Giter VIP home page Giter VIP logo

lduchosal / ipnetwork Goto Github PK

View Code? Open in Web Editor NEW
469.0 21.0 97.0 3.73 MB

IPNetwork command line and C# library take care of complex network, IP, IPv4, IPv6, netmask, CIDR, subnet, subnetting, supernet, and supernetting calculation for .NET developers. It works with IPv4 as well as IPv6, is written in C#, has a light and clean API, and is fully unit-tested

License: BSD 2-Clause "Simplified" License

Batchfile 0.02% C# 99.98%
ipv4 cidr netmask csharp ipv6 subnet

ipnetwork's Issues

Supernet not working correctly

Greetings,

Although the example "192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23" works find. I found that the below doesn't and give a /24 when it should be /22.

IPNetwork ipnetwork1 = IPNetwork.Parse("200.16.0.0/24");
IPNetwork ipnetwork2 = IPNetwork.Parse("200.16.3.0/24");
IPNetwork[] ipnetwork3 = IPNetwork.Supernet(new[] { ipnetwork1, ipnetwork2 });
MessageBox.Show(String.Format("{0} + {1} = {2}", ipnetwork1, ipnetwork2, ipnetwork3[0]));

Hope you're able to fix this.
Thanks.

Feature request: IPNetwork Subtract

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?

NETFramework,Version=v4.5.2

Could not install package 'IPNetwork2 2.1.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author. 0

AssemblyFileVersion is incorrect

In src/System.Net.IPNetwork/Properties/AssemblyInfo.cs, the version numbers are inconsistent-

[assembly: AssemblyVersion("2.1.1.0")]
[assembly: AssemblyFileVersion("2.1.0.0")] // should be 2.1.1.0, as above

The result is that the resulting System.Net.IPNetwork.dll lists 2.1.0.0 as the file/product version number.

This can cause issues with builds referencing this package, when the version number of the package does not align with the version number of the DLL.

ListIpAddresses with usable filter does not exclude non-usable IPAddresses in Enumerator

Tested with both

var cidrRange = "192.168.0.0/24"
var ipNetwork = IPNetwork.Parse(cidrRange);
var ipAddresses = ipNetwork.ListIPAddress(FilterEnum.Usable);

ipAddresses.Current // Gives 192.168.0.0

and

var cidrRange = "192.168.0.0/24"
var ipNetwork = IPNetwork.Parse(cidrRange);
var ipAddresses = ipNetwork.ListIPAddress(FilterEnum.Usable).AsEnumerable().GetEnumerator();

ipAddresses.Current // Gives 192.168.0.0

I have worked around this issue using a for-loop and the index accessor on the IPAddressCollection, but i would think this is not nessacary?

Example 9 is broken

Greetings,

Example 9 seems to be broken if you use 192.168.0.0/24 + 192.168.1.0/24 = 192.168.0.0/23, then it's correct.

But if you use another IP, say "192.168.0.0/24 + 192.168.10.0/24 = 192.168.0.0/24". This should have been CIDR /20, not 24 for the supernet.

IPv6 addresses are not parsed correctly

"::1" is parsed as "::/64, and then match any IPv4 address.

For example, this returns True: IPNetwork.Contains(IPNetwork.Parse("::1"), IPAddress.Parse("8.8.8.8"))

I think there are two issues here:

  1. IPNetwork objects should contain an AddressFamily property and IPNetwork.Contains() always return False if the address families are not the same.
  2. If netmask isn't specified IPNetwork.Parse() should set the maximum netmask (/32 for IPv4 and /128 for IPv6).

Feature Request: IPNetwork.FromSingleAddress(IPAddress)

I have an IPAddress value. Is there a simple way to produce an IPNetwork object that represents this single IP? (Implied /32 or /128 as applicable.)

A static function of IPNetwork, or an extension function adding ".AsNetwork" to IPAddress would do the job.

Many thanks.

Find next valid IP Address (IPv4 and IPv6)

I love your C# class here. Extremely useful. However, there is a missing piece in this class library that would be extremely helpful: get the next valid IP address. Is this something that you have thought about adding? I have a helper method I was able to come up with for IPv4 addresses that works really well. But IPv6 addresses are proving to be difficult to work with.

Update Web Site Examples

Greetings,

I just wanted to know if there was any plans to update the Web Site examples? For instance example 8 creates the following error: "System.Net.IPNetworkCollection' to 'System.Net.IPNetwork" with version version 2.4.0.

Why is 10.0.0.0 parsed as 10.0.0.0/8?

Today I noticed that if you dont specify the cidr block size it defaults to 8 bits when parsing IPNetworks. So for example IPNetwork.Parse("78.12.137.223") gives 78.0.0.0/8. Is this intended? It feels like it would be more natural to parse it as 78.12.137.223/32?

Issues with /31?

I haven't been able to figure out why these won't parse. I'm using IPNetwork to format networks from various threat feeds, and these /31 addresses result in error when translating from CIDR to range. These come from Firehol Level 1:

The error message formatting is from my code, but the portion after <IPNetwork> is the result from the exception raised by ipnetwork.

Transform failed at line 221 with value 31.184.196.74/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 323 with value 41.72.140.140/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 512 with value 46.165.220.142/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 521 with value 46.165.254.194/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 525 with value 46.165.254.212/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 749 with value 67.55.92.182/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 803 with value 69.43.168.214/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 1058 with value 81.169.145.160/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 1602 with value 91.219.31.14/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 1868 with value 94.73.155.10/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 1874 with value 94.152.8.56/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 2061 with value 104.227.137.34/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 2241 with value 119.160.223.114/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 2372 with value 137.74.254.64/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 3203 with value 186.71.61.90/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 4134 with value 193.90.12.220/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 6064 with value 198.50.234.210/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 6458 with value 209.99.40.222/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 6560 with value 213.192.1.170/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 6617 with value 217.20.116.130/31. <IPNetwork>: No useable IP addresses in CIDR.
Transform failed at line 6622 with value 217.20.116.150/31. <IPNetwork>: No useable IP addresses in CIDR.

Reference to type 'IPAddress' claims it is defined in 'System.Net.Primitives', but it could not be found

When I update from version 2.03 tot 2.1.0 I get below error while building, on the System.Net.IPNetwork.Contains method.

Reference to type 'IPAddress' claims it is defined in 'System.Net.Primitives', but it could not be found

System.Net.IPAddress userIpAddress = System.Net.IPAddress.Parse(strUserIpAddress);
if (System.Net.IPNetwork.Contains(System.Net.IPNetwork.Parse(strIpRange), userIpAddress){....}

Is there are dependency missing during the update or am I doing something wrong?

IPNetwork.TryParse on IP return a Network with CIDR not 32

Hello, Doing IPNetwork.TryParse("172.0.0.1", out IPNetwork net), you get a network with CIDR 16. I'm not sure about that but I thought you should get an unique IP or maybe an exception.
Could you explain the reason about TryGuessCidr method?

Thanks,

Adding ipnetwork2 as a dependency breaks nuget package generation

Hi,

Basically, adding ipnetwork2 as a dependency breaks nuget package generation with the following error: Part URI is not valid per rules defined in the Open Packaging Conventions specification.

Simple project to reproduce: ClassLibrary1.zip. Just run nuget pack .\ClassLibrary1\ClassLibrary1.csproj in the solution folder (after build ofc) to reproduce.

Maybe this is specific to .net framework 4.6.2 but I kinda doubt it (need to try though). I will do a quick test on 4.7 and core as well now and see how that behaves.

Cheers!

IPNetwork.Subnet lists invalid subnets

The first subnet seems to be ignoring the network mask.
In the code below, a 14.x.x.x subnet should not be returned.

IPNetwork network = IPNetwork.Parse("15.0.0.0/8");
var subnets = IPNetwork.Subnet(network, (byte)12);
foreach (var subnet in subnets)
{
  Debug.Print("Subnet " + subnet.ToString());
}

Prints:

14.240.0.0/12
15.0.0.0/12
15.16.0.0/12
...

Compilation of exe

Hello good afternoon.

I downloaded the code, and am trying to compile the application.

The application is compiled successfully, however, I am not finding the compiled program (exe).

I also ran tests with nant, and with Visual Studio.

Apparently the package is correct, however, it is not generating the exe.

 [exec]
 [exec] Build succeeded.
 [exec]     0 Warning(s)
 [exec]     0 Error(s)
 [exec]
 [exec] Time Elapsed 00:00:04.13

Could you check, please?

\src\System.Net.IPNetwork.ConsoleApplication.NetCore\bin\Debug\netcoreapp2.0
\src\System.Net.IPNetwork.ConsoleApplication.NetCore\bin\Release\netcoreapp2.0

CIDR list issue

Hi,

I'm not entirely sure if I'm doing something wrong here, but I had an expectation that FilterEnum.Usable would not exclude addresses that are perfectly valid. For example this code:

IPNetwork network = IPNetwork.Parse("51.195.164.144/28");
var addresses = network.ListIPAddress(FilterEnum.Usable);

Excludes both 51.195.164.144 and 51.195.164.159. Is this a bug or intended behaviour?

The Parse method parses an invalid IPv4 addresses.

I found a case that the IPNetwork.Parse method accepts an invalid IPv4 addresses.

Code

var ip1 = IPNetwork.Parse("122.13.19901");
var ip2 = IPNetwork.Parse("122.13.19901", 32);

Expect

The Parse method should throw exceptions in the above two cases.

Actual

The ip1 becomes 122.0.0.0/8.
The ip2 becomes 122.13.77.189/32.

Version

IPNetwork2 v2.5.307

NuGet package violates licence terms

The LICENCE file in this repository states:

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

Can you please include the necessary licence text in the NuGet package?

Thanks.

Feature Request - First and Last on IPNetwork

Hi,

Great library. I wanted the actual first and last IP addresses in a range, so it would be helpful if there was a .Last() and .First() method as well as .LastUsable() and .FirstUsable().

I had to use a different library in the end.

Thanks!

.NET Core support?

If it's possible, could you please migrate it over .NETCoreApp or more likely .NETStandard? Would be nice if this library would be useable from .NET Core applications.

IPv6 Support

The IPv6 support is wrong. IPv6 doesn't have the concept of broadcast, so there are no broadcast addresses. Unlike IPv4, IPv6 uses the subnet (zero) address as the first usable address, and the last usable address is what you are calling the broadcast address. The usable addresses for a standard /64 subnet run from subnet:: to subnet::ffff:ffff:ffff:ffff.

With a few exceptions, IPv6 subnets will be /64. You can use /128 for loopbacks and addresses, /126 or /127 for point to point links, /96 for IPv4 mapped addresses (;;ffff;a.b.c.d), and link-local networks (fe80::) are /10. All subnets for connecting devices (PCs, servers, etc.) should be /64, else you will break some IPv6 functionality. The IPv6 address format is network:subnet:interface.

IPv6 defaults to 64 Cidr

Hi,
I was testing this package and came across this weird behavior:

var rangeToCheck = IPNetwork.Parse("2001:db8:abcd:0012:0000:0000:0000:0000/112");
var ipToCheck = IPNetwork.Parse("2001:db8:abcd:0012:0000:0000:0000:0001");

rangeToCheck.Contains(ipToCheck) == false;

is this a default ipv6 behavior or is this a bug? as far as I can debug, the "IpToCheck" defaults to cidr of 64 instead of 128.

Thanks

How to get next address

I'm trying to figure out how to get the next IP address in an easy way. So for example, I have IP address 10.230.234.56. How do I with easy en fast coding get IP address 10.230.234.56. Here an example of a slow search.

var myip = "10.230.234.56";
var net = IPNetwork.Parse(myip, 10);
var list = IPNetwork.ListIPAddress(net);
IPAddress next;
foreach (var ip in list)
{
    if (ip.ToString().Equals(myip))
    {
        list.MoveNext();
        next = list.Current;
    }
}

First usable and last usable ip not calculated correctly.

The first and last usable ip values do no appear to be calculated correctly. According to Wikipedia, a 31-bit CIDR should have two usable addresses.

### Example 1

Entered cidr value:
10.2.2.3/31

Range returned by this mxtoolbox calculator:
10.3.3.2 - 10.3.3.3

Number of addresses from toolbox:
2 - definitely correct according to Wikipedia ^

Range returned by this ipnetwork package:
10.2.2.2 - 10.2.2.2

Number of addresses from ipnetwork:
1 - incorrect

### Example 2

Entered cidr value:
10.2.2.3/27

Range returned by this mxtoolbox calculator:
10.2.2.0 - 10.2.2.31

Number of addresses from toolbox:
32

Range returned by the ipnetwork package:
10.2.2.1 - 10.2.2.30

Number of addresses from ipnetwork:
30

IP Range to CIDR notation

Great library. I'm trying to use it to generate a single CIDR notation that encompasses a range of IP addresses.

This method seems to work fine 90% of the time, but fails sometimes. For example, passing in startIP = '172.64.0.0' and endIP = '172.71.255.255' should yield '172.64.0.0/13' according to https://www.ipaddressguide.com/cidr , but IPNetwork throws the exception

System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. (Parameter 'network')'

Am I missing something? Is there a better way to do this? Thanks

public static string IPrangeToCIDRnotation(string startIP, string endIP)
        {
            IPNetwork network = IPNetwork.Parse(startIP);
            IPNetwork network2 = IPNetwork.Parse(endIP);
            IPNetwork finalRange = network.Supernet(network2);
            return (finalRange.Network + "/" + finalRange.Cidr);
        }

Feature request: ListUsableIPAddress()

There seems to be a useful IPNetwork.ListIPAddress() method that allows one to iterate IP addresses in the network. However, this appears to include all IP addresses, even non-usable. I would like to have an equivalent method that allows me to iterate usable IP addresses, as well.

CIDR ranges /32 and /31 don't give correct count

CIDR ranges /32, and /31 when given to the library to parse and then querying range.Usable, I get back a (0) where the .FirstUsable and .LastUsable properties are correctly showing a valid IP Address.

image

.NET Core2 Support

Just installed the package, but doesn't seem to work for Core 2. I'm getting the type or namespace could not be found error.

Some of the IPv6 address cannot be parsed

Some of the valid IPv6 address and ranges cannon be parsed by the library.
Example of valid address:
2FFF:FFFF:123::/60 - INVALID
2FFF:FFFE:123::/60 - INVALID
2FFF:FFFC:123::/60 - INVALID
2FFF:FFFA:123::/60 - INVALID
2FFF:FFF9:123::/60 - VALID
FFFF:FFF1:123::/60 - INVALID

IPNetwork.ListIPAddress() returns empty items

Created the following code:

var net = IPNetwork.Parse("192.168.1.0", 24);
var list = IPNetwork.ListIPAddress(net);

Expected list to contain all IP addresses within network net. The list list contains 256 empty items.

FOSSA link

  • The FOSSA link goes to a "Sign up your company" page, which doesn't seem like the intended purpose

Supernet - Bug or default behavior?

When i try...

Subnet1 = 192.168.0.0/24
Subnet2 = 192.168.2.0/24

var subnet1 = IPNetwork.Parse(Subnet1);
var subnet2 = IPNetwork.Parse(Subnet2);

var subnet = subnet1.Supernet(subnet2);

an exception is thrown...

image

The output i expect should be 192.168.0.0/22 or am i wrong?

Using this as input works fine:

Subnet1 = 192.168.0.0/24
Subnet2 = 192.168.1.0/24

Result --> 192.168.0.0/23

Feature Request: ipnetwork.Inverse

Hello,

Would it be possible to implement an "ipnetwork.Inverse" to get the inverse netmask?
Let's say the netmask is 255.255.255.0, the inverse would be 0.0.0.255.

Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.