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:
parent
a93f3e4f6b
commit
1f2aa4c780
3
msg.go
3
msg.go
|
@ -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
|
||||
}
|
||||
|
|
22
msg_test.go
22
msg_test.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue