Complete transcoder documentation

This commit is contained in:
Dr. Maxim Orlovsky 2022-11-21 15:25:23 +01:00 committed by Alexis Sellier
parent 4d77f95c21
commit 008681a2ca
No known key found for this signature in database
1 changed files with 12 additions and 5 deletions

View File

@ -3,16 +3,22 @@ use std::convert::Infallible;
// TODO: Implement Try trait once stabilized
/// Result of a state-machine transition.
pub enum HandshakeResult<H: Handshake, T: Transcode> {
/// Handshake is not completed; we proceed to the next handshake stage.
Next(H, Vec<u8>),
/// Handshake is completed; we now can communicate in a secure way.
Complete(T, Vec<u8>),
/// 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<Self, Self::Transcoder>;
}
/// 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<u8>;
/// Encodes data before sending it to the remote peer.