diff --git a/crates/tor-cell/src/relaycell/hs.rs b/crates/tor-cell/src/relaycell/hs.rs index f05cf7a15..3f1e3cd53 100644 --- a/crates/tor-cell/src/relaycell/hs.rs +++ b/crates/tor-cell/src/relaycell/hs.rs @@ -163,11 +163,26 @@ pub struct Rendezvous1 { impl Body for Rendezvous1 { fn decode_from_reader(r: &mut Reader<'_>) -> Result { - todo!() + let cookie = r.extract()?; + let message = r.take_rest().into(); + Ok(Self { cookie, message }) } fn encode_onto(self, w: &mut W) -> EncodeResult<()> { - todo!() + w.write(&self.cookie)?; + w.write_all(&self.message[..]); + Ok(()) + } +} + +impl Rendezvous1 { + /// Create a new Rendezvous1 message, to handshake with a client identified + /// by a given RendCookie, and send it a given message. + pub fn new(cookie: RendCookie, message: impl Into>) -> Self { + Self { + cookie, + message: message.into(), + } } } @@ -179,13 +194,32 @@ pub struct Rendezvous2 { message: Vec, } +impl Rendezvous2 { + /// Construct a new Rendezvous2 cell containing a given message. + pub fn new(message: impl Into>) -> Self { + Self { + message: message.into(), + } + } +} + +impl From for Rendezvous2 { + fn from(value: Rendezvous1) -> Self { + Self { + message: value.message, + } + } +} + impl Body for Rendezvous2 { fn decode_from_reader(r: &mut Reader<'_>) -> Result { - todo!() + let message = r.take_rest().into(); + Ok(Self { message }) } fn encode_onto(self, w: &mut W) -> EncodeResult<()> { - todo!() + w.write_all(&self.message[..]); + Ok(()) } } diff --git a/crates/tor-cell/tests/testvec_relaymsg.rs b/crates/tor-cell/tests/testvec_relaymsg.rs index 54077745e..cf75d6bf8 100644 --- a/crates/tor-cell/tests/testvec_relaymsg.rs +++ b/crates/tor-cell/tests/testvec_relaymsg.rs @@ -771,3 +771,25 @@ fn test_introduce() { // TODO: need to add tests for: // - unrecognized // - data + +#[test] +fn test_rendezvous() { + use tor_cell::relaycell::hs::{Rendezvous1, Rendezvous2}; + use tor_hscrypto::RendCookie; + + let cmd1 = RelayCmd::RENDEZVOUS1; + let cmd2 = RelayCmd::RENDEZVOUS2; + + let cookie = RendCookie::from([0; 20]); + // Introduce1 with no extension + let rend1 = Rendezvous1::new(cookie, hex!("123456")); + msg( + cmd1, + "0000000000000000000000000000000000000000 + 123456", + &rend1.clone().into(), + ); + + let rend2: Rendezvous2 = rend1.into(); + msg(cmd2, "123456", &rend2.into()); +}