Skip to content

Commit 67236eb

Browse files
Return next scheduled update from next_timer_update
1 parent 9330043 commit 67236eb

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

boringtun/src/noise/timers.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ impl Tunn {
242242
}
243243
}
244244

245+
fn next_expired_session(&self) -> Option<Instant> {
246+
self.sessions
247+
.iter()
248+
.flat_map(|s| Some(s.as_ref()?.established_at() + REJECT_AFTER_TIME))
249+
.min()
250+
}
251+
245252
#[deprecated(note = "Prefer `Timers::update_timers_at` to avoid time-impurity")]
246253
pub fn update_timers<'a>(&mut self, dst: &'a mut [u8]) -> TunnResult<'a> {
247254
self.update_timers_at(dst, Instant::now())
@@ -425,7 +432,36 @@ impl Tunn {
425432
///
426433
/// If this returns `None`, you may call it at your usual desired precision (usually once a second is enough).
427434
pub fn next_timer_update(&self) -> Option<Instant> {
428-
self.timers.send_handshake_at
435+
// Mimic the `update_timers_at` function: If we have a handshake scheduled, other timers don't matter.
436+
if let Some(scheduled_handshake) = self.timers.send_handshake_at {
437+
return Some(scheduled_handshake);
438+
}
439+
440+
let common_timers = [
441+
self.next_expired_session(),
442+
self.handshake.cookie_expiration(),
443+
Some(self.timers.reject_after_time()),
444+
]
445+
.into_iter();
446+
447+
if let Some(rekey_timeout) = self.handshake.rekey_timeout() {
448+
earliest(
449+
common_timers.chain([Some(rekey_timeout), Some(self.timers.rekey_attempt_time())]),
450+
)
451+
} else {
452+
// Persistent keep-alive only makes sense if the current session is active.
453+
let persistent_keepalive = self.sessions[self.current % N_SESSIONS]
454+
.as_ref()
455+
.and_then(|_| self.timers.next_persistent_keepalive());
456+
457+
earliest(common_timers.chain([
458+
self.timers.rekey_after_time_on_send(),
459+
self.timers.reject_after_time_on_receive(),
460+
self.timers.rekey_after_time_without_response(),
461+
self.timers.keepalive_after_time_without_send(),
462+
persistent_keepalive,
463+
]))
464+
}
429465
}
430466

431467
#[deprecated(note = "Prefer `Tunn::time_since_last_handshake_at` to avoid time-impurity")]
@@ -454,3 +490,7 @@ impl Tunn {
454490
}
455491
}
456492
}
493+
494+
fn earliest(instants: impl IntoIterator<Item = Option<Instant>>) -> Option<Instant> {
495+
instants.into_iter().flatten().min()
496+
}

0 commit comments

Comments
 (0)