Skip to content

Commit

Permalink
Put the correct extended rcode in OPT
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
miekg committed Apr 9, 2016
1 parent 7e024ce commit 84206d8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
7 changes: 5 additions & 2 deletions edns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 5 additions & 21 deletions edns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 84206d8

Please sign in to comment.