From 56a3e6299b32063d72b94e7cdd983c5b6f822855 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sun, 18 Jul 2021 22:22:48 +0000 Subject: [PATCH] change server ID from `u8` to a string --- prototypes/quic_demo/README.md | 2 ++ .../quic_demo/src/bin/quic_demo_client.rs | 7 +++-- .../quic_demo/src/bin/quic_demo_end_server.rs | 4 +-- .../src/bin/quic_demo_relay_server.rs | 3 +- prototypes/quic_demo/src/protocol.rs | 31 ++++++++++++------- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/prototypes/quic_demo/README.md b/prototypes/quic_demo/README.md index 484af59..eed3171 100644 --- a/prototypes/quic_demo/README.md +++ b/prototypes/quic_demo/README.md @@ -24,6 +24,8 @@ The end-to-end testing above is the happy path. Try these sadder cases: # Network protocol +(This section is out of date. It describes an older version) + For the prototype, all control messages are fixed 4-byte messages at the start of bidirectional streams. diff --git a/prototypes/quic_demo/src/bin/quic_demo_client.rs b/prototypes/quic_demo/src/bin/quic_demo_client.rs index a75b479..f20e70a 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_client.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_client.rs @@ -11,7 +11,7 @@ struct Opt { #[structopt (long)] local_tcp_port: Option , #[structopt (long)] - client_id: Option , + client_id: Option , #[structopt (long)] server_id: Option , } @@ -36,7 +36,7 @@ async fn main () -> anyhow::Result <()> { .. } = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, &client_id).await?; - let server_id = opt.server_id.unwrap_or (43); + let server_id = opt.server_id.unwrap_or_else (|| "bogus_server".to_string ()); let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?; @@ -45,13 +45,14 @@ async fn main () -> anyhow::Result <()> { loop { let (tcp_socket, _) = listener.accept ().await?; let connection = connection.clone (); + let server_id = server_id.clone (); tokio::spawn (async move { let (local_recv, local_send) = tcp_socket.into_split (); debug! ("Starting PTTH connection"); - let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, server_id).await?; + let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, &server_id).await?; trace! ("Relaying bytes..."); 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 3b8f7e8..cad94d6 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_end_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_end_server.rs @@ -27,12 +27,12 @@ async fn main () -> anyhow::Result <()> { trace! ("Connecting to relay server"); - let server_id = opt.server_id.unwrap_or (43); + let server_id = opt.server_id.unwrap_or_else (|| "bogus_server".to_string ()); let quinn::NewConnection { mut bi_streams, .. - } = protocol::p4_connect_to_p3 (&endpoint, &relay_addr, server_id).await?; + } = protocol::p4_connect_to_p3 (&endpoint, &relay_addr, &server_id).await?; trace! ("Accepting bi streams from P3"); 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 9832788..171fc7c 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_relay_server.rs @@ -277,11 +277,12 @@ async fn handle_p4_connection ( { let mut p4_server_proxies = relay_state.p4_server_proxies.lock ().await; - p4_server_proxies.insert (server_id, p4_state); + p4_server_proxies.insert (server_id.clone (), p4_state); } while let Some (req) = rx.recv ().await { let connection = connection.clone (); + let server_id = server_id.clone (); tokio::spawn (async move { let RequestP2ToP4 { diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index 45ccda8..c70f15d 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -10,7 +10,7 @@ use quinn::{ use crate::prelude::*; -pub type PeerId = u8; +pub type PeerId = String; const MAX_ID_LENGTH: usize = 128; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -50,7 +50,7 @@ pub async fn p2_connect_to_p3 ( pub async fn p2_connect_to_p5 ( connection: &quinn::Connection, - server_id: PeerId, + server_id: &str, ) -> Result <(SendStream, RecvStream)> { let (mut send, mut recv) = connection.open_bi ().await?; @@ -59,7 +59,8 @@ pub async fn p2_connect_to_p5 ( let cmd_type = Command::CONNECT_P2_TO_P4.0; - send.write_all (&[cmd_type, server_id, 0, 0]).await?; + send.write_all (&[cmd_type, 0, 0, 0]).await?; + send_lv_string (&mut send, &server_id).await?; expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await .context ("P2 didn't get OK response when asking P3 to connect P2 to P4")?; @@ -82,7 +83,7 @@ pub enum P3Peer { } pub struct P2ClientProxy { - pub id: String, + pub id: PeerId, } pub struct P4ServerProxy { @@ -101,14 +102,13 @@ pub async fn p3_accept_peer ( Ok (match command { Command::CONNECT_P2_TO_P3 => { let id = recv_lv_string (recv, MAX_ID_LENGTH).await?; - debug! ("Client-side proxy (P2) connected, ID {}", id); P3Peer::P2ClientProxy (P2ClientProxy { id, }) }, Command::CONNECT_P4_TO_P3 => { - let id = buf [1]; + let id = recv_lv_string (recv, MAX_ID_LENGTH).await?; debug! ("Server-side proxy (P4) connected, ID {}", id); P3Peer::P4ServerProxy (P4ServerProxy { id, @@ -173,8 +173,12 @@ pub async fn p3_accept_p2_stream ( let cmd_type = buf [0]; Ok (match Command (cmd_type) { - Command::CONNECT_P2_TO_P4 => P2ToP3Stream::ConnectP2ToP4 { - server_id: buf [1], + Command::CONNECT_P2_TO_P4 => { + let server_id = recv_lv_string (recv, MAX_ID_LENGTH).await?; + + P2ToP3Stream::ConnectP2ToP4 { + server_id, + } }, _ => bail! ("Invalid command type while P3 was accepting a new bi stream from P2"), }) @@ -191,14 +195,19 @@ pub async fn p3_authorize_p2_to_p4_connection ( pub async fn p4_connect_to_p3 ( endpoint: &quinn::Endpoint, relay_addr: &std::net::SocketAddr, - server_id: PeerId, + server_id: &str, ) -> Result { + if server_id.as_bytes ().len () > MAX_ID_LENGTH { + bail! ("Server ID is longer than MAX_ID_LENGTH"); + } + let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; let (mut send, mut recv) = new_conn.connection.open_bi ().await?; let cmd_type = Command::CONNECT_P4_TO_P3.0; - send.write_all (&[cmd_type, server_id, 0, 0]).await?; + send.write_all (&[cmd_type, 0, 0, 0]).await?; + send_lv_string (&mut send, server_id).await?; expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await .context ("P4 didn't get OK response when connecting to P3")?; @@ -208,7 +217,7 @@ pub async fn p4_connect_to_p3 ( pub enum P3ToP4Stream { NewPtthConnection { - client_id: String, + client_id: PeerId, } }