Skip to content

Commit

Permalink
OPT: add Z and SetZ methods (#1246)
Browse files Browse the repository at this point in the history
* OPT: add Z and SetZ methods

Fixes: #1169

Signed-off-by: Miek Gieben <[email protected]>

* code review feedback

Change the mask and use and not (&^)

Signed-off-by: Miek Gieben <[email protected]>

* Code review changes

Signed-off-by: Miek Gieben <[email protected]>
  • Loading branch information
miekg authored May 5, 2021
1 parent 2a9acc8 commit cce7f43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
10 changes: 10 additions & 0 deletions edns.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ func (rr *OPT) SetDo(do ...bool) {
}
}

// Z returns the Z part of the OPT RR as a uint16 with only the 15 least significant bits used.
func (rr *OPT) Z() uint16 {
return uint16(rr.Hdr.Ttl & 0x7FFF)
}

// SetZ sets the Z part of the OPT RR, note only the 15 least significant bits of z are used.
func (rr *OPT) SetZ(z uint16) {
rr.Hdr.Ttl = rr.Hdr.Ttl&^0x7FFF | uint32(z&0x7FFF)
}

// EDNS0 defines an EDNS0 Option. An OPT RR can have multiple options appended to it.
type EDNS0 interface {
// Option returns the option code for the option.
Expand Down
25 changes: 25 additions & 0 deletions edns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,28 @@ func TestEDNS0_UL(t *testing.T) {
}
}
}

func TestZ(t *testing.T) {
e := &OPT{}
e.Hdr.Name = "."
e.Hdr.Rrtype = TypeOPT
e.SetVersion(8)
e.SetDo()
if e.Z() != 0 {
t.Errorf("expected Z of 0, got %d", e.Z())
}
e.SetZ(5)
if e.Z() != 5 {
t.Errorf("expected Z of 5, got %d", e.Z())
}
e.SetZ(0xFFFF)
if e.Z() != 0x7FFF {
t.Errorf("expected Z of 0x7FFFF, got %d", e.Z())
}
if e.Version() != 8 {
t.Errorf("expected version to still be 8, got %d", e.Version())
}
if !e.Do() {
t.Error("expected DO to be set")
}
}

0 comments on commit cce7f43

Please sign in to comment.