do not modify dns.Rcode when packing to wire format (#675)

* do not modify dns.Rcode when packing to wire format

When the message has an EDNS0 option in the additional section and
dns.Msg.Rcode is set to an extended rcode, dns.Msg.PackBuffer() modifies
dns.Msg.Rcode.
If you were to `Pack` the message and log it after, the Rcode would show
NOERROR.

Running the test before the change would error with:

```
=== RUN   TestPackNoSideEffect
--- FAIL: TestPackNoSideEffect (0.00s)
	msg_test.go:51: after pack: Rcode is expected to be BADVERS
```

after fixing dns.Msg.PackBuffer(), all tests are still passing.

Fixes #674

* address comments from PR#675
This commit is contained in:
chantra 2018-05-13 00:36:02 -07:00 committed by Miek Gieben
parent a93f3e4f6b
commit 1f2aa4c780
2 changed files with 23 additions and 2 deletions

3
msg.go
View File

@ -714,12 +714,11 @@ func (dns *Msg) PackBuffer(buf []byte) (msg []byte, err error) {
return nil, ErrExtendedRcode
}
opt.SetExtendedRcode(uint8(dns.Rcode >> 4))
dns.Rcode &= 0xF
}
// Convert convenient Msg into wire-like Header.
dh.Id = dns.Id
dh.Bits = uint16(dns.Opcode)<<11 | uint16(dns.Rcode)
dh.Bits = uint16(dns.Opcode)<<11 | uint16(dns.Rcode&0xF)
if dns.Response {
dh.Bits |= _QR
}

View File

@ -26,6 +26,28 @@ var (
})
)
func TestPackNoSideEffect(t *testing.T) {
m := new(Msg)
m.SetQuestion(Fqdn("example.com."), TypeNS)
a := new(Msg)
o := &OPT{
Hdr: RR_Header{
Name: ".",
Rrtype: TypeOPT,
},
}
o.SetUDPSize(DefaultMsgSize)
a.Extra = append(a.Extra, o)
a.SetRcode(m, RcodeBadVers)
a.Pack()
if a.Rcode != RcodeBadVers {
t.Errorf("after pack: Rcode is expected to be BADVERS")
}
}
func TestUnpackDomainName(t *testing.T) {
var cases = []struct {
label string