• Brad Fitzpatrick's avatar
    net: don't return IPv4 unspecified addr for Resolve*Addr of [::] or [::]:n · a5179bd0
    Brad Fitzpatrick authored
    ResolveTCPAddr, ResolveUDPAddr, and ResolveIPAddr return at most one
    address. When given a name like "golang.org" to resolve that might
    have more than 1 address, the net package has historically preferred
    IPv4 addresses, with the assumption that many users don't yet have
    IPv6 connectivity and randomly selecting between an IPv4 address and
    an IPv6 address at runtime wouldn't be a good experience for IPv4-only
    users.
    
    In CL 45088 (78cf0e56) I modified the resolution of the
    unspecified/empty address to internally resolve to both IPv6 "::" and
    0.0.0.0 to fix issue #18806.
    
    That code has 3 other callers I hadn't considered, though: the
    Resolve*Addr functions. Since they preferred IPv4, any Resolve*Addr of
    "[::]:port" or "::" (for ResolveIPAddr) would internally resolve both
    "::" and 0.0.0.0 and then prefer 0.0.0.0, even though the user was
    looking up an IPv6 literal.
    
    Add tests and fix it, not by undoing the fix to #18806 but by
    selecting the preference function for Resolve*Addr more explicitly: we
    still prefer IPv4, but if the address being looked up was an IPv6
    literal, prefer IPv6.
    
    The tests are skipped on machines without IPv6.
    
    Fixes #20911
    
    Change-Id: Ib7036cc43182ae4118cd1390c254e17c04a251a3
    Reviewed-on: https://go-review.googlesource.com/47554
    Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
    Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
    a5179bd0
ipsock.go 8.88 KB