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: cannot enforce usage of netgo for DNS resolution #33904

Closed
subsr97 opened this issue Aug 28, 2019 · 5 comments
Closed

net: cannot enforce usage of netgo for DNS resolution #33904

subsr97 opened this issue Aug 28, 2019 · 5 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@subsr97
Copy link

subsr97 commented Aug 28, 2019

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

$ go version
go version go1.12.9 linux/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/test/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/test/work"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build754748271=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Tried to enforce the usage of netgo for DNS resolution with the netgo tag.

In some machines, even when netgo tag is used during compilation and when having GODEBUG=netdns=go in the environment, cgo is being used for DNS resolution.

These machines have options edns0 in their /etc/resolv.conf.

I'm trying to enforce the usage of netgo for DNS resolution to overcome an issue similar to this one. The known workaround is to use netgo for DNS resolution.

lookuphost_example.go - https://play.golang.org/p/CgzqQXsSgvd
Build Command: go build -tags netdns lookuphost_example.go

What did you expect to see?

I expected Go to use netdns for DNS resolution.

➜  export GODEBUG=netdns=2

➜  ./lookuphost_example 
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder(www.google.com) = files,dns
[216.58.197.36 2404:6800:4007:807::2004]

What did you see instead?

In machines with options edns0 in /etc/resolv.conf, it still uses cgo for DNS resolution.

$ export GODEBUG=netdns=2

$ ./lookuphost_example 
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder(www.google.com) = cgo
[216.58.197.36 2404:6800:4007:807::2004]

After removing the edns0 entry from resolv.conf, the issue is resolved.

$ ./lookuphost_example 
go package net: dynamic selection of DNS resolver
go package net: hostLookupOrder(www.google.com) = files,dns
[216.58.197.36 2404:6800:4007:807::2004]
@ianlancetaylor ianlancetaylor changed the title Cannot enforce usage of netgo for DNS resolution net: cannot enforce usage of netgo for DNS resolution Aug 28, 2019
@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 28, 2019
@ianlancetaylor ianlancetaylor added this to the Go1.14 milestone Aug 28, 2019
@tomocy
Copy link
Contributor

tomocy commented Sep 3, 2019

Can I tackle this?

@ianlancetaylor
Copy link
Contributor

@tomocy You are certainly welcome to look at this issue. The first step is to understand what is happening. There are cases of resolv.conf entries that we don't handle, and for those cases we deliberately fall back to calling C code because anything else would be incorrect. That may be what is happening here. In that case we need to decide whether there is anything to change here.

@tomocy
Copy link
Contributor

tomocy commented Sep 22, 2019

I inspected and I also think that this behavior is caused by dnsConfig.unknownOpt which is set true because edns0 option is unknown there.
This therefore lets (*conf).hostLookupOrder return hostLookupCgo here.

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
@mateusz834
Copy link
Member

@subsr97 Can you still reproduce this issue? (I am using go 1.19)

[mateusz@arch tmp ]$ cat /etc/nsswitch.conf
(...)
hosts: mymachines resolve [!UNAVAIL=return] files myhostname dns
(...)
[mateusz@arch tmp ]$ cat /etc/resolv.conf
nameserver 127.0.0.53
options edns0 trust-ad
search .
[mateusz@arch tmp ]$ cat main.go
package main

import (
	"fmt"
	"net"
)

func main() {
	ns, err := net.LookupHost("www.google.com")
	if err != nil {
		panic(err)
	}
	fmt.Println(ns)
}
[mateusz@arch tmp ]$ go build  -tags netgo main.go
[mateusz@arch tmp ]$ GODEBUG=netdns=2 ./main
go package net: confVal.netCgo = false  netGo = true
go package net: built with netgo build tag; using Go's DNS resolver
go package net: hostLookupOrder(www.google.com) = files,dns
[2a00:1450:401b:80e::2004 216.58.215.68]

@seankhliao seankhliao added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Dec 9, 2022
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Jan 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

7 participants