Skip to content

Commit

Permalink
Flush stream on final handshake stage (#431)
Browse files Browse the repository at this point in the history
Flush the stream before finalizing the writing stage in the handshake machine. This prevents potential data loss by ensuring all buffered contents are transmitted.
  • Loading branch information
yukibtc authored Aug 21, 2024
1 parent 1617041 commit 4f9c5cb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/handshake/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ impl<Stream: Read + Write> HandshakeMachine<Stream> {
..self
})
} else {
RoundResult::StageFinished(StageResult::DoneWriting(self.stream))
RoundResult::Incomplete(HandshakeMachine {
state: HandshakeState::Flushing,
..self
})
})
} else {
Ok(RoundResult::WouldBlock(HandshakeMachine {
Expand All @@ -90,6 +93,13 @@ impl<Stream: Read + Write> HandshakeMachine<Stream> {
}))
}
}
HandshakeState::Flushing => Ok(match self.stream.flush().no_block()? {
Some(()) => RoundResult::StageFinished(StageResult::DoneWriting(self.stream)),
None => RoundResult::WouldBlock(HandshakeMachine {
state: HandshakeState::Flushing,
..self
}),
}),
}
}
}
Expand Down Expand Up @@ -128,6 +138,8 @@ enum HandshakeState {
Reading(ReadBuffer, AttackCheck),
/// Sending data to the peer.
Writing(Cursor<Vec<u8>>),
/// Flushing data to ensure that all intermediately buffered contents reach their destination.
Flushing,
}

/// Attack mitigation. Contains counters needed to prevent DoS attacks
Expand Down

0 comments on commit 4f9c5cb

Please sign in to comment.