jsakamoto / ipaddressrange

.NET Class Library for range of IP address, both IPv4 and IPv6.
Mozilla Public License 2.0
368 stars 71 forks source link

Cidr subnets not working #58

Open BigPacket opened 4 years ago

BigPacket commented 4 years ago

Greetings,

Calculating the Cidr subnets from an IP range doesn't seem to work. Error: 192.168.0.0-192.168.0.254 is not a CIDR Subnet.

// Calculates Cidr subnets var rangeI = IPAddressRange.Parse("192.168.0.0-192.168.0.254"); rangeI.ToCidrString(); // is 192.168.0.0/24

jsakamoto commented 4 years ago

The CIDR range "192.168.0.0/24" means the IPv4 address range from "192.168.0.0" to "192.168.0.255" (not "192.168.0.254").

The CIDR range "192.168.0.0/24" contains the range "192.168.0.0-192.168.0.254", but these are not equivalent.

ToCidrString() returns the CIDR expression what is equivalent to represent the IPAddressRange object it self. This is by design.

Why did you think the IPv4 range "192.168.0.0-192.168.0.254" will fit valid CIDR range expression?

BigPacket commented 4 years ago

Perhaps I misunderstood what the actual method was for. I thought it was to find a subnet of a network range and return the CIDR notation. For example: 192.168.0.1 to 192.168.15.254 would be 192.168.0.0/20.

Torrents commented 4 years ago

BigPacket, this IpAddressRange repo is great, but it doesn't find a list of CIDR subnets from an address range (as far as I can tell). I had to find something else for that. I ended up finding this sample code that shows how to calculate that: https://blog.ip2location.com/knowledge-base/how-to-convert-ip-address-range-into-cidr/

BigPacket commented 4 years ago

Cool, thank you.

From: Torrents [mailto:notifications@github.com] Sent: Wednesday, February 26, 2020 7:19 PM To: jsakamoto/ipaddressrange ipaddressrange@noreply.github.com Cc: BigPacket heyitsthenetwork@gmail.com; Author author@noreply.github.com Subject: Re: [jsakamoto/ipaddressrange] Cidr subnets not working (#58)

BigPacket, this IpAddressRange repo is great, but it doesn't find a list of CIDR subnets from an address range. I had to find something else for that. I ended up finding this sample code that shows how to calculate that: https://blog.ip2location.com/knowledge-base/how-to-convert-ip-address-range-into-cidr/

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/jsakamoto/ipaddressrange/issues/58?email_source=notifications&email_token=AGAONS6QWBZPF5QSQF6KCELRE4BGZA5CNFSM4KZ4PET2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENCM3LQ#issuecomment-591711662 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AGAONS52KGII4MDDTKV3LWTRE4BGZANCNFSM4KZ4PETQ . https://github.com/notifications/beacon/AGAONS2SV5AAZYRRJDQZSATRE4BGZA5CNFSM4KZ4PET2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOENCM3LQ.gif

ekondur commented 1 year ago

Hi @jsakamoto I've tried your example directly here, it throws a format exception.

image

Torrents commented 1 year ago

Hi @ekondur, I think you misunderstood @jsakamoto's response in this thread. He specifically agreed that "192.168.0.0 - 192.168.0.254" is not equivalent to a CIDR range, and will throw an exception.

However "192.168.0.0 - 192.168.0.255" is equivalent to the CIDR range 192.168.0.0/24. image