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: net.ParseIP always return ipv6 #65131

Open
lysShub opened this issue Jan 17, 2024 · 2 comments
Open

net: net.ParseIP always return ipv6 #65131

lysShub opened this issue Jan 17, 2024 · 2 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Comments

@lysShub
Copy link

lysShub commented Jan 17, 2024

Go version

go version go1.21.3 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lys\AppData\Local\go-build
set GOENV=C:\Users\lys\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\gopath\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\gopath
set GOPRIVATE=
set GOPROXY=https://goproxy.io,direct
set GOROOT=D:\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.3
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config

What did you do?

import (
	"fmt"
	"net"
	"net/netip"
)

func main() {
	ip1 := net.ParseIP("127.0.0.1")
	fmt.Println([]byte(ip1))

	nip1, _ := netip.AddrFromSlice(ip1)
	fmt.Println(nip1.Is4())

	conn, _ := net.DialUDP("udp", nil, &net.UDPAddr{IP: net.ParseIP("8.8.8.8"), Port: 53})
	ip2 := (conn.LocalAddr().(*net.UDPAddr)).IP
	fmt.Println([]byte(ip2))
}

What did you see happen?

[0 0 0 0 0 0 0 0 0 0 255 255 127 0 0 1]
false
[192 168 0 111]

What did you expect to see?

[127 0 0 1]
false
[192 168 0 111]

@mateusz834
Copy link
Member

mateusz834 commented Jan 17, 2024

This is intentional, [0 0 0 0 0 0 0 0 0 0 255 255 127 0 0 1] is a v4InV6 address.

go/src/net/ip.go

Lines 27 to 36 in 8e658ee

// An IP is a single IP address, a slice of bytes.
// Functions in this package accept either 4-byte (IPv4)
// or 16-byte (IPv6) slices as input.
//
// Note that in this documentation, referring to an
// IP address as an IPv4 address or an IPv6 address
// is a semantic property of the address, not just the
// length of the byte slice: a 16-byte slice can still
// be an IPv4 address.
type IP []byte

EDIT: but maybe it should be documented better?

@thediveo
Copy link

thediveo commented Jan 17, 2024

Oh yes, this should be documented better! 🩹 🤕

I've been bitten in the past on this, more so, when combining with 3rd party modules. I'm basically now having always code in place that converts back to the IPv4 4-octets representation upon detecting this situation, using this as my canonical IPv4 representation anywhere, especially before comparing type IPs, coming from different sources, such as stdlib and, for instance, RTNETLINK...

@mateusz834 mateusz834 added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation 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

3 participants