Skip to content

Commit

Permalink
Set the TC bit more aggressively in Truncate (#989)
Browse files Browse the repository at this point in the history
* Set the TC bit more aggressively in Truncate

* Update Truncate documentation for TC bit changes
  • Loading branch information
tmthrgd authored and miekg committed Jun 24, 2019
1 parent d89f1e3 commit 7f2bf87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
15 changes: 10 additions & 5 deletions msg_truncate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ package dns
// record adding as many records as possible without exceeding the
// requested buffer size.
//
// The TC bit will be set if any answer records were excluded from the
// message. This indicates to that the client should retry over TCP.
// The TC bit will be set if any records were excluded from the message.
// This indicates to that the client should retry over TCP.
//
// According to RFC 2181, the TC bit should only be set if not all of the
// "required" RRs can be included in the response. Unfortunately, we have
// no way of knowing which RRs are required so we set the TC bit if any RR
// had to be omitted from the response.
//
// The appropriate buffer size can be retrieved from the requests OPT
// record, if present, and is transport specific otherwise. dns.MinMsgSize
Expand Down Expand Up @@ -71,9 +76,9 @@ func (dns *Msg) Truncate(size int) {
l, numExtra = truncateLoop(dns.Extra, size, l, compression)
}

// According to RFC 2181, the TC bit should only be set if not all
// of the answer RRs can be included in the response.
dns.Truncated = len(dns.Answer) > numAnswer
// See the function documentation for when we set this.
dns.Truncated = len(dns.Answer) > numAnswer ||
len(dns.Ns) > numNS || len(dns.Extra) > numExtra

dns.Answer = dns.Answer[:numAnswer]
dns.Ns = dns.Ns[:numNS]
Expand Down
12 changes: 6 additions & 6 deletions msg_truncate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestRequestTruncateExtra(t *testing.T) {
if want, got := MinMsgSize, reply.Len(); want < got {
t.Errorf("message length should be bellow %d bytes, got %d bytes", want, got)
}
if reply.Truncated {
t.Errorf("truncated bit should not be set")
if !reply.Truncated {
t.Errorf("truncated bit should be set")
}
}

Expand All @@ -64,8 +64,8 @@ func TestRequestTruncateExtraEdns0(t *testing.T) {
if want, got := size, reply.Len(); want < got {
t.Errorf("message length should be bellow %d bytes, got %d bytes", want, got)
}
if reply.Truncated {
t.Errorf("truncated bit should not be set")
if !reply.Truncated {
t.Errorf("truncated bit should be set")
}
opt := reply.Extra[len(reply.Extra)-1]
if opt.Header().Rrtype != TypeOPT {
Expand Down Expand Up @@ -96,8 +96,8 @@ func TestRequestTruncateExtraRegression(t *testing.T) {
if want, got := size, reply.Len(); want < got {
t.Errorf("message length should be bellow %d bytes, got %d bytes", want, got)
}
if reply.Truncated {
t.Errorf("truncated bit should not be set")
if !reply.Truncated {
t.Errorf("truncated bit should be set")
}
opt := reply.Extra[len(reply.Extra)-1]
if opt.Header().Rrtype != TypeOPT {
Expand Down

0 comments on commit 7f2bf87

Please sign in to comment.