Skip to content

Commit

Permalink
Revert ReplayDetector API Changes
Browse files Browse the repository at this point in the history
This reverts the following commits
* f2dffd8
* 3cefa92
* d490a2c
  • Loading branch information
Sean-Der committed Sep 2, 2023
1 parent e136b3a commit 6bdfa93
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 254 deletions.
35 changes: 11 additions & 24 deletions replaydetector/replaydetector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ package replaydetector
type ReplayDetector interface {
// Check returns true if given sequence number is not replayed.
// Call accept() to mark the packet is received properly.
// The return value of accept() indicates whether the accepted packet is
// has the latest observed sequence number.
Check(seq uint64) (accept func() bool, ok bool)
}

// nop is a no-op func that is returned in the case that Check() fails.
func nop() bool {
return false
Check(seq uint64) (accept func(), ok bool)
}

type slidingWindowDetector struct {
Expand All @@ -37,33 +30,30 @@ func New(windowSize uint, maxSeq uint64) ReplayDetector {
}
}

func (d *slidingWindowDetector) Check(seq uint64) (func() bool, bool) {
func (d *slidingWindowDetector) Check(seq uint64) (accept func(), ok bool) {
if seq > d.maxSeq {
// Exceeded upper limit.
return nop, false
return func() {}, false
}

if seq <= d.latestSeq {
if d.latestSeq >= uint64(d.windowSize)+seq {
return nop, false
return func() {}, false
}
if d.mask.Bit(uint(d.latestSeq-seq)) != 0 {
// The sequence number is duplicated.
return nop, false
return func() {}, false
}
}

return func() bool {
latest := seq == 0
return func() {
if seq > d.latestSeq {
// Update the head of the window.
d.mask.Lsh(uint(seq - d.latestSeq))
d.latestSeq = seq
latest = true
}
diff := (d.latestSeq - seq) % d.maxSeq
d.mask.SetBit(uint(diff))
return latest
}, true
}

Expand All @@ -85,10 +75,10 @@ type wrappedSlidingWindowDetector struct {
init bool
}

func (d *wrappedSlidingWindowDetector) Check(seq uint64) (func() bool, bool) {
func (d *wrappedSlidingWindowDetector) Check(seq uint64) (accept func(), ok bool) {
if seq > d.maxSeq {
// Exceeded upper limit.
return nop, false
return func() {}, false
}
if !d.init {
if seq != 0 {
Expand All @@ -109,24 +99,21 @@ func (d *wrappedSlidingWindowDetector) Check(seq uint64) (func() bool, bool) {

if diff >= int64(d.windowSize) {
// Too old.
return nop, false
return func() {}, false
}
if diff >= 0 {
if d.mask.Bit(uint(diff)) != 0 {
// The sequence number is duplicated.
return nop, false
return func() {}, false
}
}

return func() bool {
latest := false
return func() {
if diff < 0 {
// Update the head of the window.
d.mask.Lsh(uint(-diff))
d.latestSeq = seq
latest = true
}
d.mask.SetBit(uint(d.latestSeq - seq))
return latest
}, true
}
Loading

0 comments on commit 6bdfa93

Please sign in to comment.