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

Comments

@lzap
Copy link

@lzap 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

@andybons andybons commented Mar 27, 2020

@andybons andybons added this to the Unplanned milestone Mar 27, 2020
@cespare
Copy link
Contributor

@cespare 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 lzap commented May 11, 2020

Okay, thanks.

@lzap lzap closed this May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.