golang/go

net: IPv6 lookup fails when IPv4 hosts entry is present

Open

#20,327 opened on May 11, 2017

View on GitHub
 (7 comments) (0 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsFixhelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.6 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/luke/work"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"

What did you do?

Add a IPv4 entry to the host file for example.com (do not add an IPv6 entry):

$ grep example.com /etc/hosts
1.2.3.4 example.com

Run the following code:

package main

import (
    "fmt"
    "net"
)

func main() {
    fmt.Println(net.ResolveIPAddr("ip6", "example.com"))
}

What did you expect to see?

When explicitly requesting an IPv6 address you should be returned an IPv6 if available via DNS even if there is an IPv4 address in the hosts file.

What did you see instead?

No addresses were returned:

$ GODEBUG=netdns=go go run dns.go 
<nil> no suitable address found
$ GODEBUG=netdns=cgo go run dns.go 
<nil> no suitable address found

This is because the request is passed from ipsock without specifying the desired address family: https://github.com/golang/go/blob/master/src/net/ipsock.go#L259

Then dnsclient_unix performs a lookup against hosts, it finds an entry (IPv4 in this case) then returns this: https://github.com/golang/go/blob/master/src/net/dnsclient_unix.go#L491-L494

ipsock then filters the returned addresses and only returns the IPs of the requested family: https://github.com/golang/go/blob/master/src/net/ipsock.go#L265-L272

Contributor guide