diff options
author | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-09-21 17:22:03 +0200 |
---|---|---|
committer | Mathias Hall-Andersen <mathias@hall-andersen.dk> | 2019-09-21 17:22:03 +0200 |
commit | 5cc108349968fbaa6998220631eb749276e64f45 (patch) | |
tree | de426ba6593a453503ec0a69349c9874e32db229 /src/timers.rs | |
parent | WIP: TUN IO worker (diff) | |
download | wireguard-rs-5cc108349968fbaa6998220631eb749276e64f45.tar.xz wireguard-rs-5cc108349968fbaa6998220631eb749276e64f45.zip |
Added zero_key to peer
Diffstat (limited to 'src/timers.rs')
-rw-r--r-- | src/timers.rs | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/src/timers.rs b/src/timers.rs new file mode 100644 index 0000000..0d69c3f --- /dev/null +++ b/src/timers.rs @@ -0,0 +1,65 @@ +use std::sync::atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}; +use std::sync::Arc; +use std::time::Duration; + +use hjul::{Runner, Timer}; + +use crate::router::Callbacks; + +const ZERO_DURATION: Duration = Duration::from_micros(0); + +pub struct TimersInner { + handshake_pending: AtomicBool, + handshake_attempts: AtomicUsize, + + retransmit_handshake: Timer, + send_keepalive: Timer, + zero_key_material: Timer, + new_handshake: Timer, + + // stats + rx_bytes: AtomicU64, + tx_bytes: AtomicU64, +} + +impl TimersInner { + pub fn new(runner: &Runner) -> Timers { + Arc::new(TimersInner { + handshake_pending: AtomicBool::new(false), + handshake_attempts: AtomicUsize::new(0), + retransmit_handshake: runner.timer(|| {}), + new_handshake: runner.timer(|| {}), + send_keepalive: runner.timer(|| {}), + zero_key_material: runner.timer(|| {}), + rx_bytes: AtomicU64::new(0), + tx_bytes: AtomicU64::new(0), + }) + } + + pub fn handshake_sent(&self) { + self.send_keepalive.stop(); + } +} + +pub type Timers = Arc<TimersInner>; + +pub struct Events(); + +impl Callbacks for Events { + type Opaque = Timers; + + fn send(t: &Timers, size: usize, data: bool, sent: bool) { + t.tx_bytes.fetch_add(size as u64, Ordering::Relaxed); + } + + fn recv(t: &Timers, size: usize, data: bool, sent: bool) { + t.rx_bytes.fetch_add(size as u64, Ordering::Relaxed); + } + + fn need_key(t: &Timers) { + if !t.handshake_pending.swap(true, Ordering::SeqCst) { + t.handshake_attempts.store(0, Ordering::SeqCst); + t.new_handshake.reset(ZERO_DURATION); + } + } +} |