Fix IPAddr equality to compare network masks #85
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why Address Mask is Essential for IPAddr Equality
In networking, an IP address without a prefix/netmask is fundamentally incomplete.
Two IP addresses should only be compared using the address, mask, and version because the mask determines which bits identify the network and which bits identify the host.
Without the mask, two addresses like
2001:db8::1/32and2001:db8::1/48may appear identical, but they represent different scopes—different network ranges, not a full subnet versus a single host.Comparing only the address and version ignores this important context, leading to inaccurate network identities.
This is relevant for access control and routing.
The Problem:
These represent entirely different networks:
2001:db8::1/48is a network with 2^80 usable host addresses2001:db8::1/32is a network with 2^96 usable host addressesThe Solution:
By including
@mask_addrin equality, we ensure that twoIPAddrobjects are equal only when they represent the same network scope, not just the same base address.IPv6 Example:
This change fixes the issue by including
@mask_addrin the equality check. As a result, equality now reflects both the address value and the applied network mask, which better matches user expectations and common networking semantics.The test suite has been updated to ensure comparisons now account for prefix length differences.
This aligns with RFC 4291's definition that IPv6 addresses are meaningless without their prefix length context, and matches the behavior network engineers expect when comparing network identities, making
IPAddr#==semantically correct according to networking standards rather than just comparing raw integer values.