@@ -242,6 +242,13 @@ impl Tunn {
242
242
}
243
243
}
244
244
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
+
245
252
#[ deprecated( note = "Prefer `Timers::update_timers_at` to avoid time-impurity" ) ]
246
253
pub fn update_timers < ' a > ( & mut self , dst : & ' a mut [ u8 ] ) -> TunnResult < ' a > {
247
254
self . update_timers_at ( dst, Instant :: now ( ) )
@@ -425,7 +432,36 @@ impl Tunn {
425
432
///
426
433
/// If this returns `None`, you may call it at your usual desired precision (usually once a second is enough).
427
434
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
+ }
429
465
}
430
466
431
467
#[ deprecated( note = "Prefer `Tunn::time_since_last_handshake_at` to avoid time-impurity" ) ]
@@ -454,3 +490,7 @@ impl Tunn {
454
490
}
455
491
}
456
492
}
493
+
494
+ fn earliest ( instants : impl IntoIterator < Item = Option < Instant > > ) -> Option < Instant > {
495
+ instants. into_iter ( ) . flatten ( ) . min ( )
496
+ }
0 commit comments