Skip to content

Commit

Permalink
refactor: directly compute when to make new handshake (#69)
Browse files Browse the repository at this point in the history
When we send a data packet and don't receive any replies for a certain
amount of time, we want to initiate a new handshake. This is currently
implemented by remembering the time we sent the first unreplied packet.
That timer is cleared every time we receive a packet.

The functionality we want to implement is easier to understand if we
directly compute the time when the new handshake is due. This allows us
to avoid recomputing that timestamp later on and we can just check if
`now` has surpassed this timestamp.
  • Loading branch information
thomaseizinger authored Jan 24, 2025
1 parent 935d9ec commit 0b87029
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions boringtun/src/noise/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ pub struct Timers {
want_keepalive: bool,
/// Did we send data without hearing back?
///
/// If `Some`, tracks the timestamp of the _first_ packet without a reply.
want_handshake_since: Option<Instant>,
/// If `Some`, tracks the timestamp when we want to initiate the new handshake.
want_handshake_at: Option<Instant>,
persistent_keepalive: usize,
/// Should this timer call reset rr function (if not a shared rr instance)
pub(super) should_reset_rr: bool,
Expand All @@ -82,7 +82,7 @@ impl Timers {
is_initiator: false,
timers: [now; TimerName::Top as usize],
want_keepalive: Default::default(),
want_handshake_since: Default::default(),
want_handshake_at: Default::default(),
persistent_keepalive: usize::from(persistent_keepalive.unwrap_or(0)),
should_reset_rr: reset_rr,
send_handshake_at: None,
Expand All @@ -104,7 +104,7 @@ impl Timers {
for t in &mut self.timers[..] {
*t = now;
}
self.want_handshake_since = None;
self.want_handshake_at = None;
self.want_keepalive = false;
}
}
Expand All @@ -127,22 +127,22 @@ impl Tunn {
match timer_name {
TimeLastPacketReceived => {
self.timers.want_keepalive = true;
self.timers.want_handshake_since = None;
self.timers.want_handshake_at = None;
}
TimeLastPacketSent => {
self.timers.want_keepalive = false;
}
TimeLastDataPacketSent => {
match self.timers.want_handshake_since {
match self.timers.want_handshake_at {
Some(_) => {
// This isn't the first timer tick (i.e. not the first packet)
// we haven't received a response to.
}
None => {
// We sent a packet and haven't heard back yet.
// Track the current time so we know when to expire
// the session.
self.timers.want_handshake_since = Some(now)
// Start a timer for when we want to make a new handshake.
self.timers.want_handshake_at =
Some(now + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
}
}
}
Expand Down Expand Up @@ -315,8 +315,8 @@ impl Tunn {
// we initiate a new handshake.
if self
.timers
.want_handshake_since
.is_some_and(|sent_at| now >= sent_at + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
.want_handshake_at
.is_some_and(|handshake_at| now >= handshake_at)
{
tracing::debug!("HANDSHAKE(KEEPALIVE + REKEY_TIMEOUT)");
handshake_initiation_required = true;
Expand Down

0 comments on commit 0b87029

Please sign in to comment.