Use netip.ParseAddr instead of net.ParseIP for clean IPv4-mapped IPv6 handling #1670
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.
Hello. This PR cleans up a few quirks related to IPv4-mapped IPv6 address handling. It doesn't add any new functionality and the change should be backward compatible (at least, no tests broke).
TLDR: The PR replaces net.ParseIP and net.ParseCIDR with netip.ParseAddr and netip.ParsePrefix.
The old address parsing interface has some implicit behavior regarding IPv4-mapped IPv6 handling. Namely, any parsed IPv4 address is stored as IPv4-mapped IPv6 internally. At the same time, parsing explicit IPv4-mapped IPv6 address and converting it back to string will return the address in the IPv4 format. Essentially,
net.ParseIP(in).String() == in
is not always true.This probably doesn't matter for most applications, however in case of a DNS library, there are a plenty of places where we need to distinguish IPv4 and IPv6 explicitly. The address parsing interface in the netip package works without the magical conversions and therefore seems to be better suited for our use case.
I've replaced the use of the old functions everywhere in the code for consistency except for unit tests where I think it doesn't matter. I've also added a few more tests. Please, let me know what you think and if I missed something else worth updating.