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

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

Open
BishoyDemian opened this issue Jul 9, 2020 · 8 comments
Open

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

BishoyDemian opened this issue Jul 9, 2020 · 8 comments

Comments

@BishoyDemian
Copy link

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

@agowa
Copy link

agowa commented Aug 19, 2020

I'd say this is intentional.
Usable returns the first free address. And within a /32 there is just no address "usable", as the zero address is the netmask.
It also correctly shows that there is a total of 1 IPs in the subnet (this might be what you want to use instead for your usecase).
But however why First and Last Usable IP are set at all might qualify as a bug.

If usable was 1 it would lead to an inconsistency, as within /24 you also only have 254 usable addresses and not 256. It does not mean that .0 and .255 are invalid addresses, they are just not usable for devices.

@lduchosal
Copy link
Owner

I'd also say that this is intended behaviour.

However, having 0 usable adresses and still having FirstUsable and LastUsable other than NULL is misleading.

That would definitively break the test suite if we NULL FirstUsable and LastUsable when usable = 0.
That would definitively break the test suite if we thrown Exception on FirstUsable and LastUsable when usable = 0.

I'd vote for :

  • Increment Major version to 2.5
  • return NULL on FirstUsable and LastUsable when usable = 0.
  • Change test suite to reflect modifications
  • Add breaking changes to documentation

Any help would be appreciated.

@fravelgue
Copy link

Maybe, I don't understand correctly. But It should be an IP network with an IP Address available 192.168.1.1 (in example)

https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks

@lduchosal
Copy link
Owner

Thanks you fravelgue for the reference

This is what we have today :

Address Mask Total Usable Network Broadcast FirstUsable LastUsable
1.0.0.1/32 255.255.255.255 1 0 1.0.0.1 1.0.0.1 1.0.0.1 1.0.0.1
1.0.0.1/31 255.255.255.254 2 0 1.0.0.1 1.0.0.2 1.0.0.1 1.0.0.2

According to : https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing#IPv4_CIDR_blocks
We should have :

Address Mask Total Usable Network Broadcast FirstUsable LastUsable Typical use
1.0.0.1/32 255.255.255.255 1 1 1.0.0.1 1.0.0.1 1.0.0.1 1.0.0.1 Host route
1.0.0.1/31 255.255.255.254 2 2 1.0.0.1 1.0.0.2 1.0.0.1 1.0.0.2 Point-to-point links (RFC 3021)

Would that be OK for everyone ?

@BishoyDemian
Copy link
Author

BishoyDemian commented Aug 28, 2020 via email

@agowa
Copy link

agowa commented Aug 28, 2020

We should have :

Address Mask Total Usable Network Broadcast FirstUsable LastUsable Typical use
1.0.0.1/32 255.255.255.255 1 1 1.0.0.1 1.0.0.1 1.0.0.1 1.0.0.1 Host route
1.0.0.1/31 255.255.255.254 2 2 1.0.0.1 1.0.0.2 1.0.0.1 1.0.0.2 Point-to-point links (RFC 3021)
Would that be OK for everyone ?

No that causes weird issues when subnetting. It should be 0 usable addresses for both and FirstUsable and LastUsable should be $null
The /32 network contains one address, but it is not usable, as Usable address means neither network nor broadcast.
With the same reason the /31 network contains two addresses, but also none of them is usable.
Instead of introducing special cases into the "Usable" attribute (e.g. /24 also only return 254 and not 256) there should be a "First" and "Last" Address attribute that ignores network and broadcast addresses and represent the boundary of the network and not of the assignable host space.

Therefore it should be:

Address Mask Total Usable Network Broadcast FirstUsable LastUsable First Last
1.0.0.1/32 255.255.255.255 1 0 1.0.0.1 $null $null $null 1.0.0.1 1.0.0.1
1.0.0.1/31 255.255.255.254 2 0 1.0.0.1 1.0.0.2 $null $null 1.0.0.1 1.0.0.2

Source for that:

In common usage, the first address in a subnet, all binary zero in the host identifier, is reserved for referring to the network itself, while the last address, all binary one in the host identifier, is used as a broadcast address for the network; this reduces the number of addresses available for hosts by 2. As a result, a /31 network, with one binary digit in the host identifier, would be unusable, as such a subnet would provide no available host addresses after this reduction.

@GF-Huang
Copy link

GF-Huang commented Feb 2, 2021

This issue has a label bug and still not close. Is it still not fixed and not published to nuget?

@lduchosal
Copy link
Owner

Dear @GF-Huang ,

The problem is that the Usable attribute is not in sync with FirstUsable and LastUsable.
@agowa338 solution seems like a good way to solve this problem with little breaking change.

I would gladly accept pull request with full unit test coverage to release a nuget version 2.6...

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

No branches or pull requests

5 participants