use crate::circuit::celltypes::CreateResponse;
use crate::{Error, Result};
use tor_cell::chancell;
use tor_cell::chancell::msg::{AnyChanMsg, HandshakeType};
pub(crate) trait CreateHandshakeWrap {
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg;
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>>;
}
pub(crate) struct CreateFastWrap;
impl CreateHandshakeWrap for CreateFastWrap {
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg {
chancell::msg::CreateFast::new(bytes).into()
}
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>> {
use CreateResponse::*;
match msg {
CreatedFast(m) => Ok(m.into_handshake()),
Destroy(_) => Err(Error::CircRefused(
"Relay replied to CREATE_FAST with DESTROY.",
)),
_ => Err(Error::CircProto(format!(
"Relay replied to CREATE_FAST with unexpected cell: {}",
msg
))),
}
}
}
pub(crate) struct Create2Wrap {
pub(crate) handshake_type: HandshakeType,
}
impl CreateHandshakeWrap for Create2Wrap {
fn to_chanmsg(&self, bytes: Vec<u8>) -> AnyChanMsg {
chancell::msg::Create2::new(self.handshake_type, bytes).into()
}
fn decode_chanmsg(&self, msg: CreateResponse) -> Result<Vec<u8>> {
use CreateResponse::*;
match msg {
Created2(m) => Ok(m.into_body()),
Destroy(_) => Err(Error::CircRefused("Relay replied to CREATE2 with DESTROY.")),
_ => Err(Error::CircProto(format!(
"Relay replied to CREATE2 with unexpected cell {}",
msg
))),
}
}
}