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
heschik opened this issue Jul 5, 2017 · 3 comments

Comments

Projects
None yet
3 participants
@heschik
Copy link
Contributor

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 self-assigned this Jul 5, 2017

@bradfitz

This comment has been minimized.

Copy link
Member

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?

@heschik

This comment has been minimized.

Copy link
Contributor Author

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.

@gopherbot

This comment has been minimized.

Copy link

commented Jul 6, 2017

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

@gopherbot gopherbot closed this in a5179bd Jul 6, 2017

@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.
You can’t perform that action at this time.