Skip to content

Commit 31013af

Browse files
Extract rekey-after-time-without-response timer
1 parent ebce84e commit 31013af

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

boringtun/src/noise/timers.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ pub struct Timers {
6060
///
6161
/// If `Some`, tracks the timestamp when we want to send a keepalive.
6262
want_passive_keepalive_at: Option<Instant>,
63-
/// Did we send data without hearing back?
64-
///
65-
/// If `Some`, tracks the timestamp when we want to initiate the new handshake.
66-
want_handshake_at: Option<Instant>,
63+
/// The earliest data packet we sent without receiving a reply.
64+
first_data_sent_without_reply: Option<Instant>,
6765
persistent_keepalive: usize,
6866
/// Should this timer call reset rr function (if not a shared rr instance)
6967
pub(super) should_reset_rr: bool,
@@ -84,7 +82,7 @@ impl Timers {
8482
is_initiator: false,
8583
timers: [now; TimerName::Top as usize],
8684
want_passive_keepalive_at: Default::default(),
87-
want_handshake_at: Default::default(),
85+
first_data_sent_without_reply: Default::default(),
8886
persistent_keepalive: usize::from(persistent_keepalive.unwrap_or(0)),
8987
should_reset_rr: reset_rr,
9088
send_handshake_at: None,
@@ -122,6 +120,12 @@ impl Timers {
122120
Some(session_established + REJECT_AFTER_TIME - KEEPALIVE_TIMEOUT - REKEY_TIMEOUT)
123121
}
124122

123+
pub(crate) fn rekey_after_time_without_response(&self) -> Option<Instant> {
124+
let first_packet_without_reply = self.first_data_sent_without_reply?;
125+
126+
Some(first_packet_without_reply + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
127+
}
128+
125129
fn is_initiator(&self) -> bool {
126130
self.is_initiator
127131
}
@@ -136,7 +140,7 @@ impl Timers {
136140
for t in &mut self.timers[..] {
137141
*t = now;
138142
}
139-
self.want_handshake_at = None;
143+
self.first_data_sent_without_reply = None;
140144
self.want_passive_keepalive_at = None;
141145
}
142146
}
@@ -158,7 +162,7 @@ impl Tunn {
158162
pub(super) fn timer_tick(&mut self, timer_name: TimerName, now: Instant) {
159163
match timer_name {
160164
TimeLastPacketReceived => {
161-
self.timers.want_handshake_at = None;
165+
self.timers.first_data_sent_without_reply = None;
162166
}
163167
TimeLastPacketSent => {
164168
self.timers.want_passive_keepalive_at = None;
@@ -167,16 +171,14 @@ impl Tunn {
167171
self.timers.want_passive_keepalive_at = Some(now + KEEPALIVE_TIMEOUT);
168172
}
169173
TimeLastDataPacketSent => {
170-
match self.timers.want_handshake_at {
174+
match self.timers.first_data_sent_without_reply {
171175
Some(_) => {
172176
// This isn't the first timer tick (i.e. not the first packet)
173177
// we haven't received a response to.
174178
}
175179
None => {
176180
// We sent a packet and haven't heard back yet.
177-
// Start a timer for when we want to make a new handshake.
178-
self.timers.want_handshake_at =
179-
Some(now + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
181+
self.timers.first_data_sent_without_reply = Some(now)
180182
}
181183
}
182184
}
@@ -352,8 +354,8 @@ impl Tunn {
352354
// we initiate a new handshake.
353355
if self
354356
.timers
355-
.want_handshake_at
356-
.is_some_and(|handshake_at| now >= handshake_at)
357+
.rekey_after_time_without_response()
358+
.is_some_and(|deadline| now >= deadline)
357359
{
358360
tracing::debug!("HANDSHAKE(KEEPALIVE + REKEY_TIMEOUT)");
359361
handshake_initiation_required = true;

0 commit comments

Comments
 (0)