golang/go

net: IPv6 lookup fails when IPv4 hosts entry is present

开放

#20,327 创建于 2017年5月11日

 (7 条评论) (0 个反应) (0 位负责人)Go (19,008 个派生)batch import
NeedsFixhelp wanted

仓库指标

星标
 (133,883 个星标)
PR 合并指标
 (30 天内没有已合并 PR)

描述

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

贡献者指南