From 84206d880d3e0be6ab83024dc7bd2ed7e9bd7f99 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sat, 9 Apr 2016 15:53:41 +0100 Subject: [PATCH] Put the correct extended rcode in OPT We were off by a value of 15. This fixes it. Hard to come up with a test as writing and reading it yourself will be consistent. Don't allows extended rcodes smaller than 16. And fix the tests as well. --- edns.go | 7 +++++-- edns_test.go | 26 +++++--------------------- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/edns.go b/edns.go index 0c47f6ea..914dda6a 100644 --- a/edns.go +++ b/edns.go @@ -97,12 +97,15 @@ func (rr *OPT) SetVersion(v uint8) { // ExtendedRcode returns the EDNS extended RCODE field (the upper 8 bits of the TTL). func (rr *OPT) ExtendedRcode() uint8 { - return uint8((rr.Hdr.Ttl & 0xFF000000) >> 24) + return uint8((rr.Hdr.Ttl&0xFF000000)>>24) + 15 } // SetExtendedRcode sets the EDNS extended RCODE field. func (rr *OPT) SetExtendedRcode(v uint8) { - rr.Hdr.Ttl = rr.Hdr.Ttl&0x00FFFFFF | (uint32(v) << 24) + if v < RcodeBadVers { // Smaller than 16.. Use the 4 bits you have! + return + } + rr.Hdr.Ttl = rr.Hdr.Ttl&0x00FFFFFF | (uint32(v-15) << 24) } // UDPSize returns the UDP buffer size. diff --git a/edns_test.go b/edns_test.go index 8ee82ab4..5fd75abb 100644 --- a/edns_test.go +++ b/edns_test.go @@ -8,41 +8,25 @@ func TestOPTTtl(t *testing.T) { e.Hdr.Rrtype = TypeOPT if e.Do() { - t.Fail() + t.Errorf("DO bit should be zero") } e.SetDo() if !e.Do() { - t.Fail() + t.Errorf("DO bit should be non-zero") } - oldTtl := e.Hdr.Ttl - if e.Version() != 0 { - t.Fail() + t.Errorf("version should be non-zero") } e.SetVersion(42) if e.Version() != 42 { - t.Fail() - } - - e.SetVersion(0) - if e.Hdr.Ttl != oldTtl { - t.Fail() - } - - if e.ExtendedRcode() != 0 { - t.Fail() + t.Errorf("set 42, expected %d, got %d", 42, e.Version()) } e.SetExtendedRcode(42) if e.ExtendedRcode() != 42 { - t.Fail() - } - - e.SetExtendedRcode(0) - if e.Hdr.Ttl != oldTtl { - t.Fail() + t.Errorf("set 42, expected %d, got %d", 42-15, e.ExtendedRcode()) } }