From 4f6fef677742f4362c23b4a5f751180db01832a9 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 5 Feb 2014 21:47:26 +0000 Subject: [PATCH] If the TC bit is set return a valid but smaller message If the TC bit is set in a message, we will probably try to parse half a message, which will fail. To fix this just return a message header and the question section and don't parse the rest. --- msg.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/msg.go b/msg.go index bec28fae..23c64793 100644 --- a/msg.go +++ b/msg.go @@ -1205,7 +1205,7 @@ func packBase32(s []byte) ([]byte, error) { return buf, nil } -// PackRR packs a resource record rr into msg[off:]. +// PackRR packs a resource record rr into msg[off:]. // See PackDomainName for documentation about the compression. func PackRR(rr RR, msg []byte, off int, compression map[string]int, compress bool) (off1 int, err error) { if rr == nil { @@ -1436,6 +1436,15 @@ func (dns *Msg) Unpack(msg []byte) (err error) { return err } } + // If we have seen a TC bit being set, we return here, without + // an error, because technically it isn't an error. So we return + // without parsing, the potentially corrupt packet. + if dns.Truncated { + dns.Answer = nil + dns.Ns = nil + dns.Extra = nil + return nil + } for i := 0; i < len(dns.Answer); i++ { dns.Answer[i], off, err = UnpackRR(msg, off) if err != nil {