aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJake McGinty <me@jake.su>2018-05-03 18:01:57 -0700
committerJake McGinty <me@jake.su>2018-05-03 18:01:57 -0700
commitdb6bc2e8118b5f8d46a983e7d4bf3f0075cab356 (patch)
treeb9fa5e3998ef547fa5ee5d9c60dc04889f650ab5 /src
parenttimers: create Timers struct to store more detailed timer info (diff)
downloadwireguard-rs-db6bc2e8118b5f8d46a983e7d4bf3f0075cab356.tar.xz
wireguard-rs-db6bc2e8118b5f8d46a983e7d4bf3f0075cab356.zip
timers: refactor out timers from Session struct
Diffstat (limited to 'src')
-rw-r--r--src/interface/peer_server.rs24
-rw-r--r--src/peer.rs54
2 files changed, 38 insertions, 40 deletions
diff --git a/src/interface/peer_server.rs b/src/interface/peer_server.rs
index 15268ff..2053a8c 100644
--- a/src/interface/peer_server.rs
+++ b/src/interface/peer_server.rs
@@ -338,9 +338,13 @@ impl PeerServer {
}
},
Some((_, SessionType::Current)) => {
- let since_last_recv = peer.sessions.current.as_ref().unwrap().last_received.elapsed(); // TODO: gross
- if since_last_recv <= *STALE_SESSION_TIMEOUT {
- let wait = *STALE_SESSION_TIMEOUT - since_last_recv;
+ if *peer.timers.authenticated_received > *peer.timers.data_sent {
+ self.timer.send_after(*STALE_SESSION_TIMEOUT, Rekey(peer_ref.clone(), our_index));
+ bail!("rekey tick (waiting STALE_SESSION_TIMEOUT since authenticated packet received more recently than sent)");
+ }
+ let since_last_send = peer.timers.data_sent.elapsed();
+ if since_last_send <= *STALE_SESSION_TIMEOUT {
+ let wait = *STALE_SESSION_TIMEOUT - since_last_send;
self.timer.send_after(wait, Rekey(peer_ref.clone(), our_index));
bail!("rekey tick (waiting ~{}s due to stale session check)", wait.as_secs());
}
@@ -355,11 +359,13 @@ impl PeerServer {
PassiveKeepAlive(peer_ref, our_index) => {
let mut peer = peer_ref.borrow_mut();
{
- let (session, session_type) = peer.find_session(our_index).ok_or_else(|| err_msg("missing session for timer"))?;
- ensure!(session_type == SessionType::Current, "expired session for passive keepalive timer");
+ if peer.sessions.current.is_none() {
+ self.timer.send_after(*KEEPALIVE_TIMEOUT, PassiveKeepAlive(peer_ref.clone(), our_index));
+ bail!("no active session. waiting until there is one.");
+ }
- let since_last_recv = session.last_received.elapsed();
- let since_last_send = session.last_sent.elapsed();
+ let since_last_recv = peer.timers.data_received.elapsed();
+ let since_last_send = peer.timers.data_sent.elapsed();
if since_last_recv < *KEEPALIVE_TIMEOUT {
let wait = *KEEPALIVE_TIMEOUT - since_last_recv;
self.timer.send_after(wait, PassiveKeepAlive(peer_ref.clone(), our_index));
@@ -368,11 +374,11 @@ impl PeerServer {
let wait = *KEEPALIVE_TIMEOUT - since_last_send;
self.timer.send_after(wait, PassiveKeepAlive(peer_ref.clone(), our_index));
bail!("passive keepalive tick (waiting ~{}s due to last send time)", wait.as_secs());
- } else if session.keepalive_sent {
+ } else if peer.timers.keepalive_sent {
self.timer.send_after(*KEEPALIVE_TIMEOUT, PassiveKeepAlive(peer_ref.clone(), our_index));
bail!("passive keepalive already sent (waiting ~{}s to see if session survives)", KEEPALIVE_TIMEOUT.as_secs());
} else {
- session.keepalive_sent = true;
+ peer.timers.keepalive_sent = true;
}
}
diff --git a/src/peer.rs b/src/peer.rs
index 752253a..37285c5 100644
--- a/src/peer.rs
+++ b/src/peer.rs
@@ -55,17 +55,15 @@ pub struct Timers {
pub egress_queued : Timestamp,
pub handshake_completed : Timestamp,
pub handshake_initialized : Timestamp,
+ pub keepalive_sent : bool
}
pub struct Session {
- pub noise : snow::Session,
- pub our_index : u32,
- pub their_index : u32,
- pub anti_replay : AntiReplay,
- pub birthday : Timestamp,
- pub last_sent : Timestamp,
- pub last_received : Timestamp,
- pub keepalive_sent : bool,
+ pub noise : snow::Session,
+ pub our_index : u32,
+ pub their_index : u32,
+ pub anti_replay : AntiReplay,
+ pub birthday : Timestamp,
}
impl Session {
@@ -73,12 +71,9 @@ impl Session {
Session {
noise,
our_index,
- their_index : 0,
- anti_replay : AntiReplay::default(),
- last_sent : Timestamp::default(),
- last_received : Timestamp::default(),
- birthday : Timestamp::default(),
- keepalive_sent : false,
+ their_index : 0,
+ anti_replay : AntiReplay::default(),
+ birthday : Timestamp ::default(),
}
}
@@ -87,24 +82,18 @@ impl Session {
noise,
our_index,
their_index,
- anti_replay : AntiReplay::default(),
- last_sent : Timestamp::default(),
- last_received : Timestamp::default(),
- birthday : Timestamp::default(),
- keepalive_sent : false,
+ anti_replay : AntiReplay::default(),
+ birthday : Timestamp ::default(),
}
}
pub fn into_transport_mode(self) -> Result<Session, Error> {
Ok(Session {
- noise : self.noise.into_transport_mode()?,
- our_index : self.our_index,
- their_index : self.their_index,
- anti_replay : self.anti_replay,
- last_sent : self.last_sent,
- last_received : self.last_received,
- keepalive_sent : self.keepalive_sent,
- birthday : self.birthday,
+ noise : self.noise.into_transport_mode()?,
+ our_index : self.our_index,
+ their_index : self.their_index,
+ anti_replay : self.anti_replay,
+ birthday : self.birthday,
})
}
}
@@ -359,12 +348,15 @@ impl Peer {
raw_packet.truncate(0);
}
- session.last_received = Timestamp::now();
- session.keepalive_sent = false; // reset passive keepalive token since received a valid ingress transport
-
session_type
};
+ if raw_packet.len() > 0 {
+ self.timers.data_received = Timestamp::now();
+ }
+
+ self.timers.keepalive_sent = false; // reset passive keepalive token since received a valid ingress transport
+
let transition = if session_type == SessionType::Next {
debug!("moving 'next' session to current after receiving first transport packet");
let next = std::mem::replace(&mut self.sessions.next, None);
@@ -401,7 +393,7 @@ impl Peer {
let padded_packet = &[packet, &vec![0u8; padding]].concat();
let len = session.noise.write_message(padded_packet, &mut out_packet[16..])?;
self.tx_bytes += len as u64;
- session.last_sent = Timestamp::now();
+ self.timers.data_sent = Timestamp::now(); // TODO: only set this timer if not a keepalive
out_packet.truncate(TRANSPORT_HEADER_SIZE + len);
Ok((endpoint, out_packet))
}