From aabefa50436af8d614520bb219d675953eeba6eb Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Sat, 21 Dec 2019 00:17:31 +0100 Subject: Remove unused test code. - make naming consistent with the kernel module. - better distribution of functionality from src/wireguard.rs - more consistent "import pattern" throughout the project. - remove unused test code. --- src/wireguard/peer.rs | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) (limited to 'src/wireguard/peer.rs') diff --git a/src/wireguard/peer.rs b/src/wireguard/peer.rs index 85e340f..5d15cf3 100644 --- a/src/wireguard/peer.rs +++ b/src/wireguard/peer.rs @@ -3,11 +3,14 @@ use super::timers::{Events, Timers}; use super::tun::Tun; use super::udp::UDP; -use super::wireguard::WireguardInner; +use super::Wireguard; + +use super::constants::REKEY_TIMEOUT; +use super::workers::HandshakeJob; use std::fmt; use std::ops::Deref; -use std::sync::atomic::{AtomicBool, AtomicU64}; +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; use std::time::{Instant, SystemTime}; @@ -15,17 +18,12 @@ use spin::{Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard}; use x25519_dalek::PublicKey; -pub struct Peer { - pub router: Arc, T::Writer, B::Writer>>, - pub state: Arc>, -} - pub struct PeerInner { // internal id (for logging) pub id: u64, // wireguard device state - pub wg: Arc>, + pub wg: Wireguard, // handshake state pub walltime_last_handshake: Mutex>, // walltime for last handshake (for UAPI status) @@ -41,6 +39,11 @@ pub struct PeerInner { pub timers: RwLock, } +pub struct Peer { + pub router: Arc, T::Writer, B::Writer>>, + pub state: Arc>, +} + impl Clone for Peer { fn clone(&self) -> Peer { Peer { @@ -51,6 +54,30 @@ impl Clone for Peer { } impl PeerInner { + /* Queue a handshake request for the parallel workers + * (if one does not already exist) + * + * The function is ratelimited. + */ + pub fn packet_send_handshake_initiation(&self) { + // the function is rate limited + + { + let mut lhs = self.last_handshake_sent.lock(); + if lhs.elapsed() < REKEY_TIMEOUT { + return; + } + *lhs = Instant::now(); + } + + // create a new handshake job for the peer + + if !self.handshake_queued.swap(true, Ordering::SeqCst) { + self.wg.pending.fetch_add(1, Ordering::SeqCst); + self.wg.queue.send(HandshakeJob::New(self.pk)); + } + } + #[inline(always)] pub fn timers(&self) -> RwLockReadGuard { self.timers.read() -- cgit v1.2.3-59-g8ed1b