Browse Source

timers: refactor out timers from Session struct

jm/master
Jake McGinty 8 years ago
parent
commit
db6bc2e811
  1. 24
      src/interface/peer_server.rs
  2. 54
      src/peer.rs

24
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;
}
}

54
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))
}

Loading…
Cancel
Save