Skip to content

net: LookupIP returns only ipv4 on windows but both ipv4 and ipv6 on linux(for a network that only supports IPv4) #71340

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

Closed
patterniha opened this issue Jan 20, 2025 · 13 comments
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows

Comments

@patterniha
Copy link

patterniha commented Jan 20, 2025

Go version

go1.24rc2 both windows-amd64 and linux-arm64(android)

What did you do?

fmt.Println(net.LookupIP("cloudflare.com"))

What did you see happen?

output: [104.16.132.229 104.16.133.229]

in windows if my network only support IPv4, "net.LookupIP" incorrectly only return IPv4 !

but in Linux it doesn't matter your network support IPv6 or not and correctly always return both IPv4 and IPv6 even your network doesn't support IPv6.

probably the reason no one has noticed this until now is that in an IPv4 network, there is often no need for an IPv6, but "net.LookupIP" should be independent of whether network supports IPv6 or not, just like it is in Linux.

What did you expect to see?

expected output: [2606:4700::6810:84e5 2606:4700::6810:85e5 104.16.132.229 104.16.133.229]

@patterniha patterniha changed the title net.LookupIP returns only ipv4 on windows net.LookupIP returns only ipv4 on windows(linux is ok) Jan 20, 2025
@patterniha patterniha changed the title net.LookupIP returns only ipv4 on windows(linux is ok) net.LookupIP returns only ipv4 on windows and both ipv4 and ipv6 on linux Jan 20, 2025
@patterniha
Copy link
Author

Related Issues

* [net: LookupNetIP returns IPv4 in IPv6 instead of IPv4 #53554 (closed)](https://github.com/golang/go/issues/53554)

* [net: net.ParseIP  always return ipv6 #65131 (closed)](https://github.com/golang/go/issues/65131)

* [How to resolve IP address by it's domain? net.LookupIP function is incorrect. #37818 (closed)](https://github.com/golang/go/issues/37818)

* [net: LookupIP should use getaddrinfo on Windows #2315 (closed)](https://github.com/golang/go/issues/2315)

* [net: IPv6 lookup fails when IPv4 hosts entry is present #20327](https://github.com/golang/go/issues/20327)

* [net: IP.To4() in package net is not returning nil for IPv6 address. #15529 (closed)](https://github.com/golang/go/issues/15529)

* [net: LookupHost shows different results between GODEBUG=netdns=cgo and go #22902](https://github.com/golang/go/issues/22902)

* [net: LookupAddr returns a zero length slice when looking up its own IP address on a Windows DNS server #29600 (closed)](https://github.com/golang/go/issues/29600)

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

my issue is similiar to #2315.
but the problem still remains if os is windows and network only support ipv4.

@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Jan 20, 2025
@patterniha patterniha changed the title net.LookupIP returns only ipv4 on windows and both ipv4 and ipv6 on linux net.LookupIP returns only ipv4 on windows but both ipv4 and ipv6 on linux(for a network that only supports IPv4) Jan 20, 2025
@patterniha
Copy link
Author

patterniha commented Jan 20, 2025

probably the reason no one has noticed this until now is that in an IPv4 network, there is often no need for an IPv6, but "net.LookupIP" should be independent of whether network supports IPv6 or not, just like it is in Linux.

@seankhliao seankhliao changed the title net.LookupIP returns only ipv4 on windows but both ipv4 and ipv6 on linux(for a network that only supports IPv4) net: LookupIP returns only ipv4 on windows but both ipv4 and ipv6 on linux(for a network that only supports IPv4) Jan 20, 2025
@seankhliao seankhliao added OS-Windows NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 20, 2025
@seankhliao
Copy link
Member

is this using the go or cgo resolvers (run with GODEBUG=netdns=2 to check)

@patterniha
Copy link
Author

patterniha commented Jan 20, 2025

is this using the go or cgo resolvers (run with GODEBUG=netdns=2 to check)

I checked, but in any case only ipv4 is returned.

in same network and in Linux both ipv4 and ipv6 is returned.

@seankhliao
Copy link
Member

The pure go DNS resolver makes both AAAA and A queries, if that didn't return any ipv6 addresses, that points to a problem with the resolver you have on your windows setup, rather than a bug in Go.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 20, 2025
@patterniha
Copy link
Author

patterniha commented Jan 20, 2025

The pure go DNS resolver makes both AAAA and A queries, if that didn't return any ipv6 addresses, that points to a problem with the resolver you have on your windows setup, rather than a bug in Go.

I check everything, The problem is definitely related to go.

i run "nslookup cloudflare.com" in cmd and both IPv4 and IPv6 is returned.

Please check it yourself before close the issue.

@patterniha
Copy link
Author

The pure go DNS resolver makes both AAAA and A queries, if that didn't return any ipv6 addresses, that points to a problem with the resolver you have on your windows setup, rather than a bug in Go.

os: windows, network: only ipv4

command:
go run test.go

output:
[104.16.133.229 104.16.132.229]

//////////////

command:
set GODEBUG=netdns=2
go run test.go

output:
go package net: confVal.netCgo = false netGo = false
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder(cloudflare.com) = cgo
[104.16.133.229 104.16.132.229]

////////////
command:
nslookup cloudflare.com

output:
Server: home
Address: 192.168.1.1

Non-authoritative answer:
Name: cloudflare.com
Addresses: 2606:4700::6810:85e5
2606:4700::6810:84e5
104.16.132.229
104.16.133.229

////////////
test.go:
package main

import ("fmt"
"net")

func main () {

fmt.Println(net.LookupIP("cloudflare.com"))

}

@mateusz834
Copy link
Member

Also try with GODEBUG=netdns=go+2

@patterniha
Copy link
Author

patterniha commented Jan 20, 2025

Also try with GODEBUG=netdns=go+2

yes, with GODEBUG=netdns=go+2 both ipv4 and ipv6 is returned.

but without this, go should return both ipv4 and ipv6.

for test if you are using windows,right click on your network in Control Panel\Network and Internet\Network Connections and uncheck ipv6.

please reopen this issue thx.

@seankhliao
Copy link
Member

When using cgo, C GetAddrInfo is used with AF_UNSPEC.
For compatibility, we expect it to match the behavior in C.

@ianlancetaylor
Copy link
Contributor

When using the native cgo resolver, the Go package will return the values that the C library returns. This isn't something that we can change in Go. See the discussion at https://pkg.go.dev/net#hdr-Name_Resolution.

@patterniha
Copy link
Author

I got my answer, this is C problem, I should use go resolver, thank you all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. OS-Windows
Projects
None yet
Development

No branches or pull requests

5 participants