From 5f2d7c7013441d793bbc7c69be370e93b3709876 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 20 Dec 2017 10:51:13 +0000 Subject: [PATCH] Unpack: return header (#608) When we unpack a message and only have the header consider it a valid message and don't return an error. --- client_test.go | 6 +++--- msg.go | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/client_test.go b/client_test.go index a3bbfdb8..cc419831 100644 --- a/client_test.go +++ b/client_test.go @@ -458,12 +458,12 @@ func TestTruncatedMsg(t *testing.T) { t.Errorf("error should not be ErrTruncated from question cutoff unpack: %v", err) } - // Finally, if we only have the header, we should still return an error + // Finally, if we only have the header, we don't return an error. buf1 = buf[:12] r = new(Msg) - if err = r.Unpack(buf1); err == nil || err != ErrTruncated { - t.Errorf("error not ErrTruncated from header-only unpack: %v", err) + if err = r.Unpack(buf1); err != nil { + t.Errorf("from header-only unpack should not return an error: %v", err) } } diff --git a/msg.go b/msg.go index afce1763..9419efd9 100644 --- a/msg.go +++ b/msg.go @@ -811,8 +811,13 @@ func (dns *Msg) Unpack(msg []byte) (err error) { dns.CheckingDisabled = (dh.Bits & _CD) != 0 dns.Rcode = int(dh.Bits & 0xF) + // If we are at the end of the message we should return *just* the + // header. This can still be useful to the caller. 9.9.9.9 sends these + // when responding with REFUSED for instance. if off == len(msg) { - return ErrTruncated + // reset sections before returning + dns.Question, dns.Answer, dns.Ns, dns.Extra = nil, nil, nil, nil + return nil } // Optimistically use the count given to us in the header