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: Resolve*([::]) gives IPv4 zero, not IPv6 #20911

Closed
heschi opened this issue Jul 5, 2017 · 3 comments
Closed

net: Resolve*([::]) gives IPv4 zero, not IPv6 #20911

heschi opened this issue Jul 5, 2017 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@heschi
Copy link
Contributor

heschi commented Jul 5, 2017

https://go-review.googlesource.com/45088 changed the behavior such that ResolveUDPAddr("[::]") followed by DialUDP on the result dials IPv4 localhost, not IPv6 localhost, which then fails when IPv4 is not configured. This is because ResolveUDPAddr unconditionally prefers IPv4 addresses when they're present in the addrList returned by internetAddrList. Looking at the code, I expect ResolveTCPAddr will have the same problem.

package main
import (
	"net"
	"fmt"
	"os"
	"strings"
)

func main() {
	addr, err := net.ResolveUDPAddr("udp", "[::]:9999")
	if err != nil {
		panic(err)
	}
	conn, err := net.DialUDP("udp", nil, addr)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s (%#v) %s (%#v)\n", addr, addr, conn.RemoteAddr(), conn.RemoteAddr())
	if strings.Contains(conn.RemoteAddr().String(), "127.0.0.1") {
		fmt.Printf("Fail\n")
		os.Exit(1)
	}
}

At d8a7990:
[::]:9999 (&net.UDPAddr{IP:net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, Port:9999, Zone:""}) [::1]:9999 (&net.UDPAddr{IP:net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}, Port:9999, Zone:""})

At 78cf0e5:

0.0.0.0:9999 (&net.UDPAddr{IP:net.IP{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0}, Port:9999, Zone:""}) 127.0.0.1:9999 (&net.UDPAddr{IP:net.IP{0x7f, 0x0, 0x0, 0x1}, Port:9999, Zone:""})

cc @bradfitz

@bradfitz bradfitz added this to the Go1.9 milestone Jul 5, 2017
@bradfitz bradfitz added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 5, 2017
@bradfitz bradfitz self-assigned this Jul 5, 2017
@bradfitz
Copy link
Contributor

bradfitz commented Jul 5, 2017

This is because ResolveUDPAddr unconditionally prefers IPv4 addresses when they're present in the addrList returned by internetAddrList.

Wow, okay.

Thanks for finding that. That's certainly not documented at https://golang.org/pkg/net/#ResolveUDPAddr

This is causing breakages somewhere I assume?

@heschi
Copy link
Contributor Author

heschi commented Jul 5, 2017

That's just my read of the code, of course, but it seems pretty clear:
return addrs.first(isIPv4).(*UDPAddr), nil
where "isIPv4" is the predicate used to pick the preferred address from the list.

Yeah, a few internal breakages.

@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 6, 2017
@gopherbot gopherbot removed the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 6, 2017
@gopherbot
Copy link

CL https://golang.org/cl/47554 mentions this issue.

@golang golang locked and limited conversation to collaborators Jul 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants