Skip to content

Commit

Permalink
Merge pull request #467 from vivint-smarthome/fix-457
Browse files Browse the repository at this point in the history
Properly proxy truncated responses
  • Loading branch information
sargun committed Sep 5, 2016
2 parents ea801fa + 60bc1f5 commit 7f9fe18
Show file tree
Hide file tree
Showing 44 changed files with 9,794 additions and 2,238 deletions.
12 changes: 12 additions & 0 deletions exchanger/exchanger.go
Expand Up @@ -26,6 +26,18 @@ func (f Func) Exchange(m *dns.Msg, addr string) (*dns.Msg, time.Duration, error)
// A Decorator adds a layer of behaviour to a given Exchanger.
type Decorator func(Exchanger) Exchanger

// IgnoreErrTruncated is a Decorator which causes dns.ErrTruncated to be ignored
var IgnoreErrTruncated Decorator = func(ex Exchanger) Exchanger {
return Func(func(m *dns.Msg, a string) (r *dns.Msg, rtt time.Duration, err error) {
r, rtt, err = ex.Exchange(m, a)
if err == dns.ErrTruncated {
// Ignore
err = nil
}
return
})
}

// Decorate decorates an Exchanger with the given Decorators.
func Decorate(ex Exchanger, ds ...Decorator) Exchanger {
decorated := ex
Expand Down
7 changes: 5 additions & 2 deletions resolver/resolver.go
Expand Up @@ -76,6 +76,7 @@ func exchangers(timeout time.Duration, protos ...string) map[string]exchanger.Ex
ReadTimeout: timeout,
WriteTimeout: timeout,
},
exchanger.IgnoreErrTruncated,
exchanger.ErrorLogging(logging.Error),
exchanger.Instrumentation(
logging.CurLog.NonMesosForwarded,
Expand Down Expand Up @@ -447,8 +448,10 @@ func truncate(m *dns.Msg, udp bool) *dns.Msg {
max = int(opt.UDPSize())
}

m.Truncated = m.Len() > max
if !m.Truncated {
furtherTruncation := m.Len() > max
m.Truncated = m.Truncated || furtherTruncation

if !furtherTruncation {
return m
}

Expand Down
13 changes: 11 additions & 2 deletions resolver/resolver_test.go
Expand Up @@ -418,17 +418,26 @@ func TestMultiError(t *testing.T) {
}

func TestTruncate(t *testing.T) {
tm := newTruncated()
tm := *newTruncated()
if !tm.Truncated {
t.Fatal("Message not truncated")
}
if l := tm.Len(); l > 512 {
t.Fatalf("Message to large: %d bytes", l)
t.Fatalf("Message too large: %d bytes", l)
}
tm2 := *truncate(&tm, true)
if !tm2.Truncated {
t.Fatal("Original truncation status was not preseved")
}
if tm2.Len() != tm.Len() {
t.Fatal("Further modification to already truncated message")
}

tm.Answer = append(tm.Answer, genA(1)...)
if l := tm.Len(); l < 512 {
t.Fatalf("Message to small after adding answers: %d bytes", l)
}

}

func BenchmarkTruncate(b *testing.B) {
Expand Down
30 changes: 16 additions & 14 deletions vendor/github.com/miekg/dns/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7f9fe18

Please sign in to comment.