Skip to content

Commit 7facc4e

Browse files
Extract keepalive-after-time-without-send timer
1 parent 31013af commit 7facc4e

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

boringtun/src/noise/timers.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ pub struct Timers {
5656
/// Is the owner of the timer the initiator or the responder for the last handshake?
5757
is_initiator: bool,
5858
timers: [Instant; TimerName::Top as usize],
59-
/// Did we receive data without sending anything back?
60-
///
61-
/// If `Some`, tracks the timestamp when we want to send a keepalive.
62-
want_passive_keepalive_at: Option<Instant>,
59+
/// The last data packet we received without sending a reply.
60+
last_data_received_without_reply: Option<Instant>,
6361
/// The earliest data packet we sent without receiving a reply.
6462
first_data_sent_without_reply: Option<Instant>,
6563
persistent_keepalive: usize,
@@ -81,7 +79,7 @@ impl Timers {
8179
Timers {
8280
is_initiator: false,
8381
timers: [now; TimerName::Top as usize],
84-
want_passive_keepalive_at: Default::default(),
82+
last_data_received_without_reply: Default::default(),
8583
first_data_sent_without_reply: Default::default(),
8684
persistent_keepalive: usize::from(persistent_keepalive.unwrap_or(0)),
8785
should_reset_rr: reset_rr,
@@ -126,6 +124,12 @@ impl Timers {
126124
Some(first_packet_without_reply + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
127125
}
128126

127+
pub(crate) fn keepalive_after_time_without_send(&self) -> Option<Instant> {
128+
let last_data_received_without_reply = self.last_data_received_without_reply?;
129+
130+
Some(last_data_received_without_reply + KEEPALIVE_TIMEOUT)
131+
}
132+
129133
fn is_initiator(&self) -> bool {
130134
self.is_initiator
131135
}
@@ -141,7 +145,7 @@ impl Timers {
141145
*t = now;
142146
}
143147
self.first_data_sent_without_reply = None;
144-
self.want_passive_keepalive_at = None;
148+
self.last_data_received_without_reply = None;
145149
}
146150
}
147151

@@ -165,10 +169,10 @@ impl Tunn {
165169
self.timers.first_data_sent_without_reply = None;
166170
}
167171
TimeLastPacketSent => {
168-
self.timers.want_passive_keepalive_at = None;
172+
self.timers.last_data_received_without_reply = None;
169173
}
170174
TimeLastDataPacketReceived => {
171-
self.timers.want_passive_keepalive_at = Some(now + KEEPALIVE_TIMEOUT);
175+
self.timers.last_data_received_without_reply = Some(now);
172176
}
173177
TimeLastDataPacketSent => {
174178
match self.timers.first_data_sent_without_reply {
@@ -366,8 +370,8 @@ impl Tunn {
366370
// to the given peer in KEEPALIVE ms, we send an empty packet.
367371
if self
368372
.timers
369-
.want_passive_keepalive_at
370-
.is_some_and(|keepalive_at| now >= keepalive_at)
373+
.keepalive_after_time_without_send()
374+
.is_some_and(|deadline| now >= deadline)
371375
{
372376
tracing::debug!("KEEPALIVE(KEEPALIVE_TIMEOUT)");
373377
keepalive_required = true;
@@ -415,7 +419,7 @@ impl Tunn {
415419
pub fn next_timer_update(&self) -> Option<Instant> {
416420
iter::empty()
417421
.chain(self.timers.send_handshake_at)
418-
.chain(self.timers.want_passive_keepalive_at)
422+
.chain(self.timers.last_data_received_without_reply)
419423
.min()
420424
}
421425

0 commit comments

Comments
 (0)