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

net: ParseCIDR returns IP of size 16 and not 4 for IPv4 #38097

Closed
lzap opened this issue Mar 26, 2020 · 3 comments
Closed

net: ParseCIDR returns IP of size 16 and not 4 for IPv4 #38097

lzap opened this issue Mar 26, 2020 · 3 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@lzap
Copy link

lzap commented Mar 26, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Fedora 31

What did you do?

ParseCIDR does not return IP of expected size, at least according to the contract https://golang.org/pkg/net/#IP.To16 where it reads "If ip is not an IP address (it is the wrong length), To16 returns nil." Although the implementation of the IP address should be hidden, functions To4 and To16 actually do not follow the documentation. I want to use them to determine if the parsed IP address is IPv4 or IPv6, looks like it can't be done in any other way.

Reproducer: https://play.golang.org/p/eEiQOZkOWnu

@andybons andybons changed the title ParseCIDR returns IP of size 16 and not 4 for IPv4 net: ParseCIDR returns IP of size 16 and not 4 for IPv4 Mar 27, 2020
@andybons
Copy link
Member

@bradfitz @mikioh

@andybons andybons added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 27, 2020
@andybons andybons added this to the Unplanned milestone Mar 27, 2020
@cespare
Copy link
Contributor

cespare commented Mar 27, 2020

In general IPv4 address may be represented as IPv6.

I want to use them to determine if the parsed IP address is IPv4 or IPv6, looks like it can't be done in any other way.

You can: the way to do it is to check whether it's an IPv4 address or not (so see if To4 returns nil). If it doesn't, you have an IPv4 address. If it does, you have an IPv6 address which does not represent an IPv4 address. Example:

https://play.golang.org/p/eY-VUtGE5Jr

Although the implementation of the IP address should be hidden, functions To4 and To16 actually do not follow the documentation.

They do. The address you have is the right length to be an IP address.

@lzap
Copy link
Author

lzap commented May 11, 2020

Okay, thanks.

@lzap lzap closed this as completed May 11, 2020
@golang golang locked and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants