diff --git a/prototypes/quic_demo/src/bin/quic_demo_client.rs b/prototypes/quic_demo/src/bin/quic_demo_client.rs index 2d63a07..eebeab6 100644 --- a/prototypes/quic_demo/src/bin/quic_demo_client.rs +++ b/prototypes/quic_demo/src/bin/quic_demo_client.rs @@ -2,7 +2,6 @@ use structopt::StructOpt; use tokio::net::TcpListener; use quic_demo::prelude::*; -use protocol::Command; #[derive (Debug, StructOpt)] struct Opt { @@ -29,18 +28,15 @@ async fn main () -> anyhow::Result <()> { trace! ("Connecting to relay server"); + let client_id = opt.client_id.unwrap_or (42); + let quinn::NewConnection { connection, .. - } = endpoint.connect (&relay_addr, "localhost")?.await?; + } = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, client_id).await?; - let (mut send, mut recv) = connection.open_bi ().await?; - - let client_id = opt.client_id.unwrap_or (42); let server_id = opt.server_id.unwrap_or (43); - protocol::p2_connect_to_p3 (&mut send, &mut recv, client_id).await?; - let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?; trace! ("Accepting local TCP connections from P1"); @@ -52,47 +48,9 @@ async fn main () -> anyhow::Result <()> { tokio::spawn (async move { let (local_recv, local_send) = tcp_socket.into_split (); - debug! ("Started PTTH connection"); + debug! ("Starting PTTH connection"); - let (mut relay_send, mut relay_recv) = connection.open_bi ().await?; - - // Ask P3 if we can connect to P4 - - let req_buf = [ - Command::CONNECT_P2_TO_P4.0, - server_id, - 0, - 0, - ]; - relay_send.write_all (&req_buf).await?; - - let mut resp_buf = [0; 4]; - relay_recv.read_exact (&mut resp_buf).await?; - assert_eq! (resp_buf, [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P4.0, - 0, - 0, - ]); - - // Ask P4 if we can connect to P5 - - let req_buf = [ - Command::CONNECT_P2_TO_P5.0, - 0, - 0, - 0, - ]; - relay_send.write_all (&req_buf).await?; - - let mut resp_buf = [0; 4]; - relay_recv.read_exact (&mut resp_buf).await?; - assert_eq! (resp_buf, [ - Command::OKAY.0, - Command::CONNECT_P2_TO_P5.0, - 0, - 0, - ]); + let (relay_send, relay_recv) = protocol::p2_connect_to_p5 (&connection, server_id).await?; trace! ("Relaying bytes..."); diff --git a/prototypes/quic_demo/src/protocol.rs b/prototypes/quic_demo/src/protocol.rs index a22ef86..32dc2ae 100644 --- a/prototypes/quic_demo/src/protocol.rs +++ b/prototypes/quic_demo/src/protocol.rs @@ -18,12 +18,15 @@ impl Command { } pub async fn p2_connect_to_p3 ( - send: &mut SendStream, - recv: &mut RecvStream, + endpoint: &quinn::Endpoint, + relay_addr: &std::net::SocketAddr, client_id: u8, -) --> anyhow::Result <()> +) -> anyhow::Result { + let new_conn = endpoint.connect (relay_addr, "localhost")?.await?; + + let (mut send, mut recv) = new_conn.connection.open_bi ().await?; + let req_buf = [ Command::CONNECT_P2_TO_P3.0, client_id, @@ -44,5 +47,59 @@ pub async fn p2_connect_to_p3 ( bail! ("P2 didn't get OK response when connecting to P3"); } - Ok (()) + Ok (new_conn) +} + +pub async fn p2_connect_to_p5 ( + connection: &quinn::Connection, + server_id: u8, +) -> anyhow::Result <(SendStream, RecvStream)> +{ + let (mut relay_send, mut relay_recv) = connection.open_bi ().await?; + + // Ask P3 if we can connect to P4 + + let req_buf = [ + Command::CONNECT_P2_TO_P4.0, + server_id, + 0, + 0, + ]; + relay_send.write_all (&req_buf).await?; + + let mut resp_buf = [0; 4]; + relay_recv.read_exact (&mut resp_buf).await?; + let expected = [ + Command::OKAY.0, + Command::CONNECT_P2_TO_P4.0, + 0, + 0, + ]; + if resp_buf != expected { + bail! ("P2 didn't get OK response when asking P3 to connect P2 to P4"); + } + + // Ask P4 if we can connect to P5 + + let req_buf = [ + Command::CONNECT_P2_TO_P5.0, + 0, + 0, + 0, + ]; + relay_send.write_all (&req_buf).await?; + + let mut resp_buf = [0; 4]; + relay_recv.read_exact (&mut resp_buf).await?; + let expected = [ + Command::OKAY.0, + Command::CONNECT_P2_TO_P5.0, + 0, + 0, + ]; + if resp_buf != expected { + bail! ("P2 didn't get OK response when asking P4 to connect P2 to P5"); + } + + Ok ((relay_send, relay_recv)) }