Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse("10.0.0.1/1") #85

Open
cnbohu opened this issue Jul 28, 2024 · 6 comments
Open

Parse("10.0.0.1/1") #85

cnbohu opened this issue Jul 28, 2024 · 6 comments

Comments

@cnbohu
Copy link

cnbohu commented Jul 28, 2024

var ipRange = IPAddressRange.Parse("10.0.0.1/1")int iCount = ipRange.AsEnumerable().Count()

iCount is '-2147483648' ?

@jsakamoto
Copy link
Owner

jsakamoto commented Jul 29, 2024

Hi @cnbohu ,

iCount is '-2147483648' ?

It is because the number of IP addresses is over the max range of the type of int, which caused overflow. In short, it represents int.MaxValue + 1.

If you cast the return value of the Count() extension method to the UInt32 type, like below, you will get the correct number of the IP addresses in that range.

var ipRange = IPAddressRange.Parse("10.0.0.1/1") ;
var count = (UInt32)ipRange.AsEnumerable().Count()

Happy Coding ;)

@cnbohu
Copy link
Author

cnbohu commented Jul 30, 2024

Thx!
But,
Other errors are encountered in IPv6 “2408:8220:332:39f0::/60”

var ipRange = IPAddressRange.Parse("2408:8220:332:39f0::/60");
BigInteger iIpCount = (BigInteger)ipRange.AsEnumerable().LongCount();

program is unresponsive ...

@jsakamoto
Copy link
Owner

Hi @cnbohu,

program is unresponsive ...

Becuase that program will count about 300,000,000,000,000,000,000 objects (!). If it takes one microsecond to count one address, it will take more than 9 million years to finish counting this number of addresses...!

Instead, use the following expression to get the number of the IPv6 addresses.

var count = (UInt128)System.Math.Pow(2, 128 - 60 /* <- the length of the bit mask */ );

@cnbohu
Copy link
Author

cnbohu commented Jul 31, 2024

It is possible, to add a properties or methods, not generate objects,only calculate the count.

public BigInteger Total
{
    get
    {
        int max = this.Begin.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ? 32 : 128;
        var count = BigInteger.Pow(2, max - this._prefixLength);
        return count;
    }
}

@jsakamoto
Copy link
Owner

It is possible, to add a properties or methods, not generate objects,only calculate the count.

That's true!
In fact, we already have a code like you mentioned to implement the ICollection<T> interface.

https://github.com/jsakamoto/ipaddressrange/blob/master/IPAddressRange/Internals/IPv4RangeOperator.cs#L42

int ICollection<IPAddress>.Count => (int)((this.End - this.Begin) + 1);

(Actually, the implementation above is more generic, simple, and fast than using the power of two.)

That's the reason why your code ipRange.AsEnumerable().Count() returns value immediately. The IEnumerable<T>.Count extension method uses the ICollection<T>.Count method directly if the target object implements ICollection<T> instead of enumerating each element.

But unfortunately, the return type of the ICollection<T>.Count method is int. So, currently, this approach doesn't resolve all cases, like your case.

So many people don't care about the number of addresses in a range, so we haven't exposed such a feature on the IPAddressRange so far. But if you hope it, we might consider exposing something like the UInt128 Count property on the IPAddressRange.

@cnbohu
Copy link
Author

cnbohu commented Aug 1, 2024

In my project, need to get CIDR total IPs ,estimate(total * tasks) to customize the execution of scheduled jobs.

Like this: 9.9.0.0/16 => 65536 IP x 4 tasks = 262144 queries

I write the method to calculation,now function completed.

Thank for your work & reply!Hope your features will be available in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants