From 1634f7a00d263c11b5f1c3db9cf525ef4ec1ab75 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 18 Jul 2021 19:38:54 +0000 Subject: [PATCH] :recycle: refactor: server proxy is now using the protocol module --- .../quic_demo/src/bin/quic_demo_end_server.rs | 28 ++---- .../src/bin/quic_demo_relay_server.rs | 2 +- prototypes/quic_demo/src/protocol.rs | 96 ++++++++++++++----- 3 files changed, 78 insertions(+), 48 deletions(-) diff --git a/prototypes/quic_demo/src/bin/quic_demo_end_server.rs b/prototypes/quic_demo/src/bin/quic_demo_end_server.rs index bd89bb8..232ef7d 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_end_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_end_server.rs @@ -53,7 +53,7 @@ async fn handle_bi_stream ( protocol::P3ToP4Stream::NewPtthConnection { client_id, .. - } => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port).await?, + } => handle_new_ptth_connection (relay_send, relay_recv, local_tcp_port, client_id).await?, } Ok (()) @@ -63,31 +63,17 @@ async fn handle_new_ptth_connection ( mut relay_send: quinn::SendStream, mut relay_recv: quinn::RecvStream, local_tcp_port: u16, + _client_id: u8, ) -> anyhow::Result <()> { - // TODO: Authorize P2 to connect to us + // TODO: Check authorization for P2 --> P4 - let resp_buf = [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P4_STEP_2.0, - 0, - 0, - ]; - relay_send.write_all (&resp_buf).await?; + protocol::p4_authorize_p2_connection (&mut relay_send).await?; + protocol::p4_expect_p5_request (&mut relay_recv).await?; - let mut req_buf = [0, 0, 0, 0]; - relay_recv.read_exact (&mut req_buf).await?; - assert_eq! (req_buf [0], Command::CONNECT_P2_TO_P5.0); + // TODO: Check authorization for P1 --> P5 - // TODO: Authorize P2 to connect to P5 - - let resp_buf = [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P5.0, - 0, - 0, - ]; - relay_send.write_all (&resp_buf).await?; + protocol::p4_authorize_p1_connection (&mut relay_send).await?; debug! ("Started PTTH connection"); diff --git a/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs b/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs index e5fae3b..a976c4b 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs @@ -74,7 +74,7 @@ impl ConnectEvents { connects - disconnects } - fn active (&self) -> u64 { + fn _active (&self) -> u64 { let connects = self.connects.load (Ordering::Relaxed); let disconnects = self.disconnects.load (Ordering::Relaxed); connects - disconnects diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 3103d47..5c86b3a 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -18,29 +18,6 @@ impl Command { pub const OKAY: Command = Command (20); } -pub enum P3ToP4Stream { - NewPtthConnection { - client_id: u8, - } -} - -pub async fn p4_accept_p3_stream ( - recv: &mut RecvStream, -) -> Result -{ - let mut req_buf = [0, 0, 0, 0]; - recv.read_exact (&mut req_buf).await?; - - let cmd_type = req_buf [0]; - - Ok (match Command (cmd_type) { - Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection { - client_id: req_buf [1], - }, - _ => bail! ("Invalid command type while P2 was accepting a new bi stream from P3"), - }) -} - pub async fn p2_connect_to_p3 ( endpoint: &quinn::Endpoint, relay_addr: &std::net::SocketAddr, @@ -105,13 +82,80 @@ pub async fn p4_connect_to_p3 ( Ok (new_conn) } +pub enum P3ToP4Stream { + NewPtthConnection { + client_id: u8, + } +} + +pub async fn p4_accept_p3_stream ( + recv: &mut RecvStream, +) -> Result +{ + let mut buf = [0, 0, 0, 0]; + recv.read_exact (&mut buf).await?; + + let cmd_type = buf [0]; + + Ok (match Command (cmd_type) { + Command::CONNECT_P2_TO_P4_STEP_2 => P3ToP4Stream::NewPtthConnection { + client_id: buf [1], + }, + _ => bail! ("Invalid command type while P2 was accepting a new bi stream from P3"), + }) +} + +pub async fn p4_authorize_p2_connection ( + send: &mut SendStream, +) -> Result <()> +{ + let buf = [ + Command::OKAY.0, + Command::CONNECT_P2_TO_P4_STEP_2.0, + 0, + 0, + ]; + send.write_all (&buf).await?; + + Ok (()) +} + +pub async fn p4_authorize_p1_connection ( + send: &mut SendStream, +) -> Result <()> +{ + let buf = [ + Command::OKAY.0, + Command::CONNECT_P2_TO_P5.0, + 0, + 0, + ]; + send.write_all (&buf).await?; + + Ok (()) +} + +pub async fn p4_expect_p5_request ( + recv: &mut RecvStream, +) -> Result <()> +{ + let mut buf = [0, 0, 0, 0]; + recv.read_exact (&mut buf).await?; + let cmd_type = Command (buf [0]); + if cmd_type != Command::CONNECT_P2_TO_P5 { + bail! ("P4 expected CONNECT_P2_TO_P5 but P2 sent something different"); + } + + Ok (()) +} + async fn expect_exact_response ( recv: &mut RecvStream, expected: [u8; 4] ) -> Result <()> { - let mut resp_buf = [0, 0, 0, 0]; - recv.read_exact (&mut resp_buf).await?; - if resp_buf != expected { + let mut buf = [0, 0, 0, 0]; + recv.read_exact (&mut buf).await?; + if buf != expected { bail! ("Didn't receive exact response"); }