aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2015-08-18 22:50:12 -0400
committerRuss Cox <rsc@golang.org>2015-08-19 04:20:37 +0000
commit773b9b8452cfc498ef1aa0e0bbc456a56fd4fb0e (patch)
tree8d5e20c25e3665e1fd3aba120f01e5b46f150d17
parent18d27b2d754923854e05c3b37a2b799d9c063164 (diff)
downloadgo-773b9b8452cfc498ef1aa0e0bbc456a56fd4fb0e.tar.gz
go-773b9b8452cfc498ef1aa0e0bbc456a56fd4fb0e.zip
net: respect go vs cgo resolver selection in all lookup routines
This is especially important for LookupAddr, which used to be pure Go (lightweight, one goroutine per call) and without this CL is now unconditionally cgo (heavy, one thread per call). Fixes #12190. Change-Id: I43436a942bc1838b024225893e156f280a1e80cf Reviewed-on: https://go-review.googlesource.com/13698 Reviewed-by: Rob Pike <r@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
-rw-r--r--src/net/conf.go8
-rw-r--r--src/net/lookup_unix.go27
2 files changed, 22 insertions, 13 deletions
diff --git a/src/net/conf.go b/src/net/conf.go
index 01bb585ce6..c92e579d7e 100644
--- a/src/net/conf.go
+++ b/src/net/conf.go
@@ -19,7 +19,7 @@ type conf struct {
// forceCgoLookupHost forces CGO to always be used, if available.
forceCgoLookupHost bool
- netGo bool // "netgo" build tag in use (or no cgo)
+ netGo bool // go DNS resolution forced
netCgo bool // cgo DNS resolution forced
// machine has an /etc/mdns.allow file
@@ -112,6 +112,12 @@ func initConfVal() {
}
}
+// canUseCgo reports whether calling cgo functions is allowed
+// for non-hostname lookups.
+func (c *conf) canUseCgo() bool {
+ return c.hostLookupOrder("") == hostLookupCgo
+}
+
// hostLookupOrder determines which strategy to use to resolve hostname.
func (c *conf) hostLookupOrder(hostname string) (ret hostLookupOrder) {
if c.dnsDebugLevel > 1 {
diff --git a/src/net/lookup_unix.go b/src/net/lookup_unix.go
index 1c811d2683..a64da8bcb5 100644
--- a/src/net/lookup_unix.go
+++ b/src/net/lookup_unix.go
@@ -74,19 +74,21 @@ func lookupIP(host string) (addrs []IPAddr, err error) {
}
func lookupPort(network, service string) (int, error) {
- port, err, ok := cgoLookupPort(network, service)
- if !ok {
- port, err = goLookupPort(network, service)
+ if systemConf().canUseCgo() {
+ if port, err, ok := cgoLookupPort(network, service); ok {
+ return port, err
+ }
}
- return port, err
+ return goLookupPort(network, service)
}
func lookupCNAME(name string) (string, error) {
- cname, err, ok := cgoLookupCNAME(name)
- if !ok {
- cname, err = goLookupCNAME(name)
+ if systemConf().canUseCgo() {
+ if cname, err, ok := cgoLookupCNAME(name); ok {
+ return cname, err
+ }
}
- return cname, err
+ return goLookupCNAME(name)
}
func lookupSRV(service, proto, name string) (string, []*SRV, error) {
@@ -148,9 +150,10 @@ func lookupTXT(name string) ([]string, error) {
}
func lookupAddr(addr string) ([]string, error) {
- ptrs, err, ok := cgoLookupPTR(addr)
- if !ok {
- ptrs, err = goLookupPTR(addr)
+ if systemConf().canUseCgo() {
+ if ptrs, err, ok := cgoLookupPTR(addr); ok {
+ return ptrs, err
+ }
}
- return ptrs, err
+ return goLookupPTR(addr)
}