diff options
author | Guanhao Yin <sopium@mysterious.site> | 2017-03-30 13:04:09 +0800 |
---|---|---|
committer | Guanhao Yin <sopium@mysterious.site> | 2017-03-30 13:04:09 +0800 |
commit | 9f72f9661682b9b5023cdf8929aee68d49cdbed9 (patch) | |
tree | b09217507bb3103d48e6d2b69ef07bce70fcb722 | |
parent | Init handshake if necessary when sending a keep-alive packet. Clear packet queue in PeerState::clear() (diff) | |
download | wireguard-rs-9f72f9661682b9b5023cdf8929aee68d49cdbed9.tar.xz wireguard-rs-9f72f9661682b9b5023cdf8929aee68d49cdbed9.zip |
Anti Replay: optimize for large skip
-rw-r--r-- | src/protocol/anti_replay.rs | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/protocol/anti_replay.rs b/src/protocol/anti_replay.rs index 775ba2e..e3803aa 100644 --- a/src/protocol/anti_replay.rs +++ b/src/protocol/anti_replay.rs @@ -18,8 +18,6 @@ // This is RFC 6479. -use std::cmp::min; - // Power of 2. const BITMAP_BITLEN: u64 = 2048; @@ -33,8 +31,8 @@ const BITMAP_LOC_MASK: u64 = SIZE_OF_INTEGER - 1; pub const WINDOW_SIZE: u64 = BITMAP_BITLEN - SIZE_OF_INTEGER; pub struct AntiReplay { - last: u64, bitmap: [u32; BITMAP_LEN], + last: u64, } impl Default for AntiReplay { @@ -78,12 +76,17 @@ impl AntiReplay { if seq > self.last { let index_cur = self.last >> REDUNDANT_BIT_SHIFTS; - let diff = min(index - index_cur, BITMAP_LEN as u64); - - for i in 0..diff { - let real_index = (index_cur + i + 1) & BITMAP_INDEX_MASK; - self.bitmap[real_index as usize] = 0; + let diff = index - index_cur; + + if diff >= BITMAP_LEN as u64 { + self.bitmap = [0; BITMAP_LEN]; + } else { + for i in 0..diff { + let real_index = (index_cur + i + 1) & BITMAP_INDEX_MASK; + self.bitmap[real_index as usize] = 0; + } } + self.last = seq; } |