Skip to content

Commit e45deda

Browse files
Return next scheduled update from next_timer_update
1 parent bb7b2a5 commit e45deda

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

boringtun/src/noise/timers.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,13 @@ impl Tunn {
245245
}
246246
}
247247

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

437469
#[deprecated(note = "Prefer `Tunn::time_since_last_handshake_at` to avoid time-impurity")]

0 commit comments

Comments
 (0)