Skip to content

Commit

Permalink
Return next scheduled update from next_timer_update
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaseizinger committed Jan 15, 2025
1 parent 9330043 commit 67236eb
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion boringtun/src/noise/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ impl Tunn {
}
}

fn next_expired_session(&self) -> Option<Instant> {
self.sessions
.iter()
.flat_map(|s| Some(s.as_ref()?.established_at() + REJECT_AFTER_TIME))
.min()
}

#[deprecated(note = "Prefer `Timers::update_timers_at` to avoid time-impurity")]
pub fn update_timers<'a>(&mut self, dst: &'a mut [u8]) -> TunnResult<'a> {
self.update_timers_at(dst, Instant::now())
Expand Down Expand Up @@ -425,7 +432,36 @@ impl Tunn {
///
/// If this returns `None`, you may call it at your usual desired precision (usually once a second is enough).
pub fn next_timer_update(&self) -> Option<Instant> {
self.timers.send_handshake_at
// Mimic the `update_timers_at` function: If we have a handshake scheduled, other timers don't matter.
if let Some(scheduled_handshake) = self.timers.send_handshake_at {
return Some(scheduled_handshake);
}

let common_timers = [
self.next_expired_session(),
self.handshake.cookie_expiration(),
Some(self.timers.reject_after_time()),
]
.into_iter();

if let Some(rekey_timeout) = self.handshake.rekey_timeout() {
earliest(
common_timers.chain([Some(rekey_timeout), Some(self.timers.rekey_attempt_time())]),
)
} else {
// Persistent keep-alive only makes sense if the current session is active.
let persistent_keepalive = self.sessions[self.current % N_SESSIONS]
.as_ref()
.and_then(|_| self.timers.next_persistent_keepalive());

earliest(common_timers.chain([
self.timers.rekey_after_time_on_send(),
self.timers.reject_after_time_on_receive(),
self.timers.rekey_after_time_without_response(),
self.timers.keepalive_after_time_without_send(),
persistent_keepalive,
]))
}
}

#[deprecated(note = "Prefer `Tunn::time_since_last_handshake_at` to avoid time-impurity")]
Expand Down Expand Up @@ -454,3 +490,7 @@ impl Tunn {
}
}
}

fn earliest(instants: impl IntoIterator<Item = Option<Instant>>) -> Option<Instant> {
instants.into_iter().flatten().min()
}

0 comments on commit 67236eb

Please sign in to comment.