Set the TC bit more aggressively in Truncate (#989)

* Set the TC bit more aggressively in Truncate

* Update Truncate documentation for TC bit changes
This commit is contained in:
Tom Thorogood 2019-06-25 01:29:43 +09:30 committed by Miek Gieben
parent d89f1e3d4b
commit 7f2bf8764a
2 changed files with 16 additions and 11 deletions

View File

@ -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
@ -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]

View File

@ -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")
}
}
@ -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 {
@ -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 {