mirror of
https://github.com/rustdesk/hbb_common.git
synced 2026-11-30 09:45:12 +00:00
Widen the udp_seal replay window to 1024 numbers
Pushes take their packet numbers from one counter for the whole server, so on a server with a hundred thousand devices the 64 numbers below the highest seen cover about ten milliseconds, and a push the network delays by more is dropped as a replay. A 1024-bit ring covers about 150 ms at that rate, for 120 more bytes per window. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PkpVy2ufxgEjKd3SjXPnJ8
This commit is contained in:
1 parent
94a6d132dc
commit
d98e65051f
1 file changed
+33
-15
+33
-15
@@ -232,11 +232,14 @@ fn open_data(session: &[u8; 32], kind: u8, datagram: &[u8]) -> Option<(u64, Vec<
|
|||||||
open_aead(&key, pn_nonce(pn), ad, &datagram[HDR + ID + PN..]).map(|payload| (pn, payload))
|
open_aead(&key, pn_nonce(pn), ad, &datagram[HDR + ID + PN..]).map(|payload| (pn, payload))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The packet numbers a receiver has accepted under one key: the highest and the 63 below it.
|
const WINDOW: u64 = 1024;
|
||||||
|
|
||||||
|
/// The packet numbers a receiver has accepted under one key: the highest and the 1023 below it,
|
||||||
|
/// one bit each in a ring indexed by the number.
|
||||||
#[derive(Clone, Copy, Debug, Default)]
|
#[derive(Clone, Copy, Debug, Default)]
|
||||||
pub struct ReplayWindow {
|
pub struct ReplayWindow {
|
||||||
top: u64,
|
top: u64,
|
||||||
bits: u64,
|
bits: [u64; WINDOW as usize / 64],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ReplayWindow {
|
impl ReplayWindow {
|
||||||
@@ -247,22 +250,33 @@ impl ReplayWindow {
|
|||||||
None => return false,
|
None => return false,
|
||||||
};
|
};
|
||||||
if n > self.top {
|
if n > self.top {
|
||||||
let shift = n - self.top;
|
if n - self.top >= WINDOW {
|
||||||
self.bits = if shift >= 64 {
|
self.bits = Default::default();
|
||||||
1
|
|
||||||
} else {
|
} else {
|
||||||
(self.bits << shift) | 1
|
for m in self.top + 1..n {
|
||||||
};
|
self.set(m, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
self.top = n;
|
self.top = n;
|
||||||
return true;
|
} else if self.top - n >= WINDOW || self.get(n) {
|
||||||
}
|
|
||||||
let back = self.top - n;
|
|
||||||
if back >= 64 || (self.bits >> back) & 1 == 1 {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self.bits |= 1 << back;
|
self.set(n, true);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get(&self, n: u64) -> bool {
|
||||||
|
self.bits[(n % WINDOW / 64) as usize] >> (n % 64) & 1 == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set(&mut self, n: u64, seen: bool) {
|
||||||
|
let word = &mut self.bits[(n % WINDOW / 64) as usize];
|
||||||
|
if seen {
|
||||||
|
*word |= 1 << (n % 64);
|
||||||
|
} else {
|
||||||
|
*word &= !(1 << (n % 64));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -819,11 +833,15 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn the_replay_window_keeps_64_numbers() {
|
fn the_replay_window_keeps_1024_numbers() {
|
||||||
let mut w = ReplayWindow::default();
|
let mut w = ReplayWindow::default();
|
||||||
assert!(w.accept(0) && !w.accept(0));
|
assert!(w.accept(0) && !w.accept(0));
|
||||||
assert!(w.accept(100) && w.accept(37) && !w.accept(37) && !w.accept(36));
|
assert!(w.accept(2000) && w.accept(977) && !w.accept(977) && !w.accept(976));
|
||||||
assert!(w.accept(99) && !w.accept(100) && !w.accept(u64::MAX));
|
assert!(w.accept(1999) && !w.accept(2000) && !w.accept(u64::MAX));
|
||||||
|
assert!(
|
||||||
|
w.accept(3000) && w.accept(2001) && !w.accept(2001),
|
||||||
|
"a number the window moved past does not shadow one 1024 above it"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// A restart starts the key ids over, so a hello replayed into the same slot gets the same
|
// A restart starts the key ids over, so a hello replayed into the same slot gets the same
|
||||||
|
|||||||
Reference in new issue
Block a user