Skip to content

Commit 53dd0d7

Browse files
zhangboyangodeke-em
authored andcommitted
net: make go resolver aware of network parameter
Currently, the go resolver always send two DNS queries (A and AAAA) even if tcp4/udp4/ip4 or tcp6/udp6/ip6 is used. This can cause unwanted latencies when making IPv4-only or IPv6-only connections. This change make go resolver aware of network parameter. Now, only one A query is sent when tcp4/udp4/ip4 is used, and vice versa for tcp6/udp6/ip6. Fixes #45024 Change-Id: I815f909e6df5f7242cfc900f7dfecca628c3a2c8 GitHub-Last-Rev: 3d30c48 GitHub-Pull-Request: #45016 Reviewed-on: https://go-review.googlesource.com/c/go/+/301709 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Trust: Emmanuel Odeke <[email protected]>
1 parent f4b9183 commit 53dd0d7

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

src/net/dnsclient_unix.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ func (r *Resolver) goLookupHostOrder(ctx context.Context, name string, order hos
532532
return
533533
}
534534
}
535-
ips, _, err := r.goLookupIPCNAMEOrder(ctx, name, order)
535+
ips, _, err := r.goLookupIPCNAMEOrder(ctx, "ip", name, order)
536536
if err != nil {
537537
return
538538
}
@@ -558,13 +558,13 @@ func goLookupIPFiles(name string) (addrs []IPAddr) {
558558

559559
// goLookupIP is the native Go implementation of LookupIP.
560560
// The libc versions are in cgo_*.go.
561-
func (r *Resolver) goLookupIP(ctx context.Context, host string) (addrs []IPAddr, err error) {
561+
func (r *Resolver) goLookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) {
562562
order := systemConf().hostLookupOrder(r, host)
563-
addrs, _, err = r.goLookupIPCNAMEOrder(ctx, host, order)
563+
addrs, _, err = r.goLookupIPCNAMEOrder(ctx, network, host, order)
564564
return
565565
}
566566

567-
func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order hostLookupOrder) (addrs []IPAddr, cname dnsmessage.Name, err error) {
567+
func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, network, name string, order hostLookupOrder) (addrs []IPAddr, cname dnsmessage.Name, err error) {
568568
if order == hostLookupFilesDNS || order == hostLookupFiles {
569569
addrs = goLookupIPFiles(name)
570570
if len(addrs) > 0 || order == hostLookupFiles {
@@ -585,7 +585,13 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order
585585
error
586586
}
587587
lane := make(chan result, 1)
588-
qtypes := [...]dnsmessage.Type{dnsmessage.TypeA, dnsmessage.TypeAAAA}
588+
qtypes := []dnsmessage.Type{dnsmessage.TypeA, dnsmessage.TypeAAAA}
589+
switch ipVersion(network) {
590+
case '4':
591+
qtypes = []dnsmessage.Type{dnsmessage.TypeA}
592+
case '6':
593+
qtypes = []dnsmessage.Type{dnsmessage.TypeAAAA}
594+
}
589595
var queryFn func(fqdn string, qtype dnsmessage.Type)
590596
var responseFn func(fqdn string, qtype dnsmessage.Type) result
591597
if conf.singleRequest {
@@ -730,7 +736,7 @@ func (r *Resolver) goLookupIPCNAMEOrder(ctx context.Context, name string, order
730736
// goLookupCNAME is the native Go (non-cgo) implementation of LookupCNAME.
731737
func (r *Resolver) goLookupCNAME(ctx context.Context, host string) (string, error) {
732738
order := systemConf().hostLookupOrder(r, host)
733-
_, cname, err := r.goLookupIPCNAMEOrder(ctx, host, order)
739+
_, cname, err := r.goLookupIPCNAMEOrder(ctx, "ip", host, order)
734740
return cname.String(), err
735741
}
736742

src/net/dnsclient_unix_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,14 +601,14 @@ func TestGoLookupIPOrderFallbackToFile(t *testing.T) {
601601
name := fmt.Sprintf("order %v", order)
602602

603603
// First ensure that we get an error when contacting a non-existent host.
604-
_, _, err := r.goLookupIPCNAMEOrder(context.Background(), "notarealhost", order)
604+
_, _, err := r.goLookupIPCNAMEOrder(context.Background(), "ip", "notarealhost", order)
605605
if err == nil {
606606
t.Errorf("%s: expected error while looking up name not in hosts file", name)
607607
continue
608608
}
609609

610610
// Now check that we get an address when the name appears in the hosts file.
611-
addrs, _, err := r.goLookupIPCNAMEOrder(context.Background(), "thor", order) // entry is in "testdata/hosts"
611+
addrs, _, err := r.goLookupIPCNAMEOrder(context.Background(), "ip", "thor", order) // entry is in "testdata/hosts"
612612
if err != nil {
613613
t.Errorf("%s: expected to successfully lookup host entry", name)
614614
continue

src/net/lookup_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (r *Resolver) lookupHost(ctx context.Context, host string) (addrs []string,
9090

9191
func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []IPAddr, err error) {
9292
if r.preferGo() {
93-
return r.goLookupIP(ctx, host)
93+
return r.goLookupIP(ctx, network, host)
9494
}
9595
order := systemConf().hostLookupOrder(r, host)
9696
if order == hostLookupCgo {
@@ -100,7 +100,7 @@ func (r *Resolver) lookupIP(ctx context.Context, network, host string) (addrs []
100100
// cgo not available (or netgo); fall back to Go's DNS resolver
101101
order = hostLookupFilesDNS
102102
}
103-
ips, _, err := r.goLookupIPCNAMEOrder(ctx, host, order)
103+
ips, _, err := r.goLookupIPCNAMEOrder(ctx, network, host, order)
104104
return ips, err
105105
}
106106

src/net/netgo_unix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestGoLookupIP(t *testing.T) {
2424
if err != nil {
2525
t.Error(err)
2626
}
27-
if _, err := DefaultResolver.goLookupIP(ctx, host); err != nil {
27+
if _, err := DefaultResolver.goLookupIP(ctx, "ip", host); err != nil {
2828
t.Error(err)
2929
}
3030
}

0 commit comments

Comments
 (0)