diff --git a/radicle-node/src/wire/transcoder.rs b/radicle-node/src/wire/transcoder.rs index 94e34197..98ce6a40 100644 --- a/radicle-node/src/wire/transcoder.rs +++ b/radicle-node/src/wire/transcoder.rs @@ -3,16 +3,22 @@ use std::convert::Infallible; // TODO: Implement Try trait once stabilized /// Result of a state-machine transition. pub enum HandshakeResult { + /// Handshake is not completed; we proceed to the next handshake stage. Next(H, Vec), + /// Handshake is completed; we now can communicate in a secure way. Complete(T, Vec), + /// Handshake has failed with some error. Error(H::Error), } +/// State machine implementation of a handshake protocol which can be run by +/// peers. pub trait Handshake: Sized { + /// The resulting transcoder which will be constructed upon a successful + /// handshake + type Transcoder: Transcode; /// Errors which may happen during the handshake. type Error: std::error::Error; - /// Underlying transcoder. - type Transcoder: Transcode; /// Create a new handshake state-machine. fn new() -> Self; @@ -20,12 +26,13 @@ pub trait Handshake: Sized { fn step(self, input: &[u8]) -> HandshakeResult; } +/// Dumb handshake structure which runs void protocol. #[derive(Debug, Default)] pub struct NoHandshake; impl Handshake for NoHandshake { - type Error = Infallible; type Transcoder = PlainTranscoder; + type Error = Infallible; fn new() -> Self { NoHandshake @@ -39,8 +46,8 @@ impl Handshake for NoHandshake { /// Trait allowing transcoding a stream using some form of stream encryption /// and/or encoding. pub trait Transcode { - /// Decodes data received from the remote peer and updates the internal state - /// of the transcoder. + /// Decodes data received from the remote peer and update the internal state + /// of the transcoder, if necessary. fn decrypt(&mut self, data: &[u8]) -> Vec; /// Encodes data before sending it to the remote peer.