Skip to content

Commit 1a5d36a

Browse files
Track time of first packet without reply
1 parent 9daa595 commit 1a5d36a

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

boringtun/src/noise/timers.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ pub struct Timers {
4848
/// Did we receive data without sending anything back?
4949
want_keepalive: bool,
5050
/// Did we send data without hearing back?
51-
want_handshake: bool,
51+
///
52+
/// If `Some`, holds the _first_ instant when we sent a packet.
53+
want_handshake_since: Option<Instant>,
5254
persistent_keepalive: usize,
5355
/// Should this timer call reset rr function (if not a shared rr instance)
5456
pub(super) should_reset_rr: bool,
@@ -69,7 +71,7 @@ impl Timers {
6971
is_initiator: false,
7072
timers: [now; TimerName::Top as usize],
7173
want_keepalive: Default::default(),
72-
want_handshake: Default::default(),
74+
want_handshake_since: Default::default(),
7375
persistent_keepalive: usize::from(persistent_keepalive.unwrap_or(0)),
7476
should_reset_rr: reset_rr,
7577
send_handshake_at: None,
@@ -118,9 +120,7 @@ impl Timers {
118120
}
119121

120122
pub(crate) fn rekey_after_time_without_response(&self) -> Option<Instant> {
121-
if !self.want_handshake {
122-
return None;
123-
}
123+
let first_packet_without_reply = self.want_handshake_since?;
124124

125125
let last_data_packet_sent = self[TimeLastDataPacketSent];
126126
let last_packet_received = self[TimeLastPacketReceived];
@@ -130,7 +130,7 @@ impl Timers {
130130
return None;
131131
}
132132

133-
Some(last_packet_received + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
133+
Some(first_packet_without_reply + KEEPALIVE_TIMEOUT + REKEY_TIMEOUT)
134134
}
135135

136136
pub(crate) fn keepalive_after_time_without_send(&self) -> Option<Instant> {
@@ -169,7 +169,7 @@ impl Timers {
169169
for t in &mut self.timers[..] {
170170
*t = now;
171171
}
172-
self.want_handshake = false;
172+
self.want_handshake_since = None;
173173
self.want_keepalive = false;
174174
}
175175
}
@@ -192,12 +192,20 @@ impl Tunn {
192192
match timer_name {
193193
TimeLastPacketReceived => {
194194
self.timers.want_keepalive = true;
195-
self.timers.want_handshake = false;
195+
self.timers.want_handshake_since = None;
196196
}
197197
TimeLastPacketSent => {
198-
self.timers.want_handshake = true;
199198
self.timers.want_keepalive = false;
200199
}
200+
TimeLastDataPacketSent => {
201+
match self.timers.want_handshake_since {
202+
Some(_) => {} // Already waiting for a reply, don't update the timestamp.
203+
None => {
204+
// This is the first packet to not be replied to, start tracking the time.
205+
self.timers.want_handshake_since = Some(now)
206+
}
207+
}
208+
}
201209
_ => {}
202210
}
203211

0 commit comments

Comments
 (0)