Skip to content

Commit 8197318

Browse files
committed
code review fixes
1 parent edaa76c commit 8197318

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/net/dnsclient_unix.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ import (
2626
"internal/x/net/dns/dnsmessage"
2727
)
2828

29+
const (
30+
// to be used as a useTCP parameter to exchange
31+
useTCPOnly = true
32+
useUDPOrTCP = false
33+
)
34+
2935
var (
3036
errLameReferral = errors.New("lame referral")
3137
errCannotUnmarshalDNSMessage = errors.New("cannot unmarshal DNS message")
@@ -131,14 +137,14 @@ func dnsStreamRoundTrip(c Conn, id uint16, query dnsmessage.Question, b []byte)
131137
}
132138

133139
// exchange sends a query on the connection and hopes for a response.
134-
func (r *Resolver) exchange(ctx context.Context, server string, q dnsmessage.Question, timeout time.Duration, usetcp bool) (dnsmessage.Parser, dnsmessage.Header, error) {
140+
func (r *Resolver) exchange(ctx context.Context, server string, q dnsmessage.Question, timeout time.Duration, useTCP bool) (dnsmessage.Parser, dnsmessage.Header, error) {
135141
q.Class = dnsmessage.ClassINET
136142
id, udpReq, tcpReq, err := newRequest(q)
137143
if err != nil {
138144
return dnsmessage.Parser{}, dnsmessage.Header{}, errCannotMarshalDNSMessage
139145
}
140146
var networks []string
141-
if usetcp {
147+
if useTCP {
142148
networks = []string{"tcp"}
143149
} else {
144150
networks = []string{"udp", "tcp"}
@@ -247,7 +253,7 @@ func (r *Resolver) tryOneName(ctx context.Context, cfg *dnsConfig, name string,
247253
for j := uint32(0); j < sLen; j++ {
248254
server := cfg.servers[(serverOffset+j)%sLen]
249255

250-
p, h, err := r.exchange(ctx, server, q, cfg.timeout, cfg.usetcp)
256+
p, h, err := r.exchange(ctx, server, q, cfg.timeout, cfg.useTCP)
251257
if err != nil {
252258
dnsErr := &DNSError{
253259
Err: err.Error(),

src/net/dnsclient_unix_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestDNSTransportFallback(t *testing.T) {
8080
for _, tt := range dnsTransportFallbackTests {
8181
ctx, cancel := context.WithCancel(context.Background())
8282
defer cancel()
83-
_, h, err := r.exchange(ctx, tt.server, tt.question, time.Second, false)
83+
_, h, err := r.exchange(ctx, tt.server, tt.question, time.Second, useUDPOrTCP)
8484
if err != nil {
8585
t.Error(err)
8686
continue
@@ -136,7 +136,7 @@ func TestSpecialDomainName(t *testing.T) {
136136
for _, tt := range specialDomainNameTests {
137137
ctx, cancel := context.WithCancel(context.Background())
138138
defer cancel()
139-
_, h, err := r.exchange(ctx, server, tt.question, 3*time.Second, false)
139+
_, h, err := r.exchange(ctx, server, tt.question, 3*time.Second, useUDPOrTCP)
140140
if err != nil {
141141
t.Error(err)
142142
continue
@@ -1563,7 +1563,7 @@ func TestDNSDialTCP(t *testing.T) {
15631563
}
15641564
r := Resolver{PreferGo: true, Dial: fake.DialContext}
15651565
ctx := context.Background()
1566-
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, false)
1566+
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, useUDPOrTCP)
15671567
if err != nil {
15681568
t.Fatal("exhange failed:", err)
15691569
}
@@ -1643,7 +1643,7 @@ func TestDNSUseTCP(t *testing.T) {
16431643
r := Resolver{PreferGo: true, Dial: fake.DialContext}
16441644
ctx, cancel := context.WithCancel(context.Background())
16451645
defer cancel()
1646-
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, true)
1646+
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, useTCPOnly)
16471647
if err != nil {
16481648
t.Fatal("exchange failed:", err)
16491649
}

src/net/dnsconfig_unix.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type dnsConfig struct {
3232
err error // any error that occurs during open of resolv.conf
3333
mtime time.Time // time of resolv.conf modification
3434
soffset uint32 // used by serverOffset
35-
usetcp bool // force usage of TCP for DNS resolutions
35+
useTCP bool // force usage of TCP for DNS resolutions
3636
}
3737

3838
// See resolv.conf(5) on a Linux machine.
@@ -117,7 +117,11 @@ func dnsReadConfig(filename string) *dnsConfig {
117117
case s == "rotate":
118118
conf.rotate = true
119119
case s == "use-vc":
120-
conf.usetcp = true
120+
// Linux glibc option:
121+
// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
122+
// "Sets RES_USEVC in _res.options.
123+
// This option forces the use of TCP for DNS resolutions."
124+
conf.useTCP = true
121125
default:
122126
conf.unknownOpt = true
123127
}

src/net/dnsconfig_unix_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ var dnsReadConfigTests = []struct {
107107
want: &dnsConfig{
108108
servers: defaultNS,
109109
ndots: 1,
110-
usetcp: true,
110+
useTCP: true,
111111
timeout: 5 * time.Second,
112112
attempts: 2,
113113
search: []string{"domain.local."},

0 commit comments

Comments
 (0)