Skip to content

Commit

Permalink
code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbus committed Jan 7, 2019
1 parent edaa76c commit 8197318
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/net/dnsclient_unix.go
Expand Up @@ -26,6 +26,12 @@ import (
"internal/x/net/dns/dnsmessage"
)

const (
// to be used as a useTCP parameter to exchange
useTCPOnly = true
useUDPOrTCP = false
)

var (
errLameReferral = errors.New("lame referral")
errCannotUnmarshalDNSMessage = errors.New("cannot unmarshal DNS message")
Expand Down Expand Up @@ -131,14 +137,14 @@ func dnsStreamRoundTrip(c Conn, id uint16, query dnsmessage.Question, b []byte)
}

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

p, h, err := r.exchange(ctx, server, q, cfg.timeout, cfg.usetcp)
p, h, err := r.exchange(ctx, server, q, cfg.timeout, cfg.useTCP)
if err != nil {
dnsErr := &DNSError{
Err: err.Error(),
Expand Down
8 changes: 4 additions & 4 deletions src/net/dnsclient_unix_test.go
Expand Up @@ -80,7 +80,7 @@ func TestDNSTransportFallback(t *testing.T) {
for _, tt := range dnsTransportFallbackTests {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, h, err := r.exchange(ctx, tt.server, tt.question, time.Second, false)
_, h, err := r.exchange(ctx, tt.server, tt.question, time.Second, useUDPOrTCP)
if err != nil {
t.Error(err)
continue
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestSpecialDomainName(t *testing.T) {
for _, tt := range specialDomainNameTests {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, h, err := r.exchange(ctx, server, tt.question, 3*time.Second, false)
_, h, err := r.exchange(ctx, server, tt.question, 3*time.Second, useUDPOrTCP)
if err != nil {
t.Error(err)
continue
Expand Down Expand Up @@ -1563,7 +1563,7 @@ func TestDNSDialTCP(t *testing.T) {
}
r := Resolver{PreferGo: true, Dial: fake.DialContext}
ctx := context.Background()
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, false)
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, useUDPOrTCP)
if err != nil {
t.Fatal("exhange failed:", err)
}
Expand Down Expand Up @@ -1643,7 +1643,7 @@ func TestDNSUseTCP(t *testing.T) {
r := Resolver{PreferGo: true, Dial: fake.DialContext}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, true)
_, _, err := r.exchange(ctx, "0.0.0.0", mustQuestion("com.", dnsmessage.TypeALL, dnsmessage.ClassINET), time.Second, useTCPOnly)
if err != nil {
t.Fatal("exchange failed:", err)
}
Expand Down
8 changes: 6 additions & 2 deletions src/net/dnsconfig_unix.go
Expand Up @@ -32,7 +32,7 @@ type dnsConfig struct {
err error // any error that occurs during open of resolv.conf
mtime time.Time // time of resolv.conf modification
soffset uint32 // used by serverOffset
usetcp bool // force usage of TCP for DNS resolutions
useTCP bool // force usage of TCP for DNS resolutions
}

// See resolv.conf(5) on a Linux machine.
Expand Down Expand Up @@ -117,7 +117,11 @@ func dnsReadConfig(filename string) *dnsConfig {
case s == "rotate":
conf.rotate = true
case s == "use-vc":
conf.usetcp = true
// Linux glibc option:
// http://man7.org/linux/man-pages/man5/resolv.conf.5.html
// "Sets RES_USEVC in _res.options.
// This option forces the use of TCP for DNS resolutions."
conf.useTCP = true
default:
conf.unknownOpt = true
}
Expand Down
2 changes: 1 addition & 1 deletion src/net/dnsconfig_unix_test.go
Expand Up @@ -107,7 +107,7 @@ var dnsReadConfigTests = []struct {
want: &dnsConfig{
servers: defaultNS,
ndots: 1,
usetcp: true,
useTCP: true,
timeout: 5 * time.Second,
attempts: 2,
search: []string{"domain.local."},
Expand Down

0 comments on commit 8197318

Please sign in to comment.