change server ID from `u8` to a string

main
_ 2021-07-18 22:22:48 +00:00
parent 5bfb99f383
commit 56a3e6299b
5 changed files with 30 additions and 17 deletions

View File

@ -24,6 +24,8 @@ The end-to-end testing above is the happy path. Try these sadder cases:
# Network protocol # 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 For the prototype, all control messages are fixed 4-byte messages at the
start of bidirectional streams. start of bidirectional streams.

View File

@ -11,7 +11,7 @@ struct Opt {
#[structopt (long)] #[structopt (long)]
local_tcp_port: Option <u16>, local_tcp_port: Option <u16>,
#[structopt (long)] #[structopt (long)]
client_id: Option <String>, client_id: Option <PeerId>,
#[structopt (long)] #[structopt (long)]
server_id: Option <PeerId>, server_id: Option <PeerId>,
} }
@ -36,7 +36,7 @@ async fn main () -> anyhow::Result <()> {
.. ..
} = protocol::p2_connect_to_p3 (&endpoint, &relay_addr, &client_id).await?; } = 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?; let listener = TcpListener::bind (("127.0.0.1", local_tcp_port)).await?;
@ -45,13 +45,14 @@ async fn main () -> anyhow::Result <()> {
loop { loop {
let (tcp_socket, _) = listener.accept ().await?; let (tcp_socket, _) = listener.accept ().await?;
let connection = connection.clone (); let connection = connection.clone ();
let server_id = server_id.clone ();
tokio::spawn (async move { tokio::spawn (async move {
let (local_recv, local_send) = tcp_socket.into_split (); let (local_recv, local_send) = tcp_socket.into_split ();
debug! ("Starting PTTH connection"); 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..."); trace! ("Relaying bytes...");

View File

@ -27,12 +27,12 @@ async fn main () -> anyhow::Result <()> {
trace! ("Connecting to relay server"); 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 { let quinn::NewConnection {
mut bi_streams, 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"); trace! ("Accepting bi streams from P3");

View File

@ -277,11 +277,12 @@ async fn handle_p4_connection (
{ {
let mut p4_server_proxies = relay_state.p4_server_proxies.lock ().await; 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 { while let Some (req) = rx.recv ().await {
let connection = connection.clone (); let connection = connection.clone ();
let server_id = server_id.clone ();
tokio::spawn (async move { tokio::spawn (async move {
let RequestP2ToP4 { let RequestP2ToP4 {

View File

@ -10,7 +10,7 @@ use quinn::{
use crate::prelude::*; use crate::prelude::*;
pub type PeerId = u8; pub type PeerId = String;
const MAX_ID_LENGTH: usize = 128; const MAX_ID_LENGTH: usize = 128;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[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 ( pub async fn p2_connect_to_p5 (
connection: &quinn::Connection, connection: &quinn::Connection,
server_id: PeerId, server_id: &str,
) -> Result <(SendStream, RecvStream)> ) -> Result <(SendStream, RecvStream)>
{ {
let (mut send, mut recv) = connection.open_bi ().await?; 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; 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 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")?; .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 struct P2ClientProxy {
pub id: String, pub id: PeerId,
} }
pub struct P4ServerProxy { pub struct P4ServerProxy {
@ -101,14 +102,13 @@ pub async fn p3_accept_peer (
Ok (match command { Ok (match command {
Command::CONNECT_P2_TO_P3 => { Command::CONNECT_P2_TO_P3 => {
let id = recv_lv_string (recv, MAX_ID_LENGTH).await?; let id = recv_lv_string (recv, MAX_ID_LENGTH).await?;
debug! ("Client-side proxy (P2) connected, ID {}", id); debug! ("Client-side proxy (P2) connected, ID {}", id);
P3Peer::P2ClientProxy (P2ClientProxy { P3Peer::P2ClientProxy (P2ClientProxy {
id, id,
}) })
}, },
Command::CONNECT_P4_TO_P3 => { 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); debug! ("Server-side proxy (P4) connected, ID {}", id);
P3Peer::P4ServerProxy (P4ServerProxy { P3Peer::P4ServerProxy (P4ServerProxy {
id, id,
@ -173,8 +173,12 @@ pub async fn p3_accept_p2_stream (
let cmd_type = buf [0]; let cmd_type = buf [0];
Ok (match Command (cmd_type) { Ok (match Command (cmd_type) {
Command::CONNECT_P2_TO_P4 => P2ToP3Stream::ConnectP2ToP4 { Command::CONNECT_P2_TO_P4 => {
server_id: buf [1], 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"), _ => 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 ( pub async fn p4_connect_to_p3 (
endpoint: &quinn::Endpoint, endpoint: &quinn::Endpoint,
relay_addr: &std::net::SocketAddr, relay_addr: &std::net::SocketAddr,
server_id: PeerId, server_id: &str,
) -> Result <quinn::NewConnection> ) -> Result <quinn::NewConnection>
{ {
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 new_conn = endpoint.connect (relay_addr, "localhost")?.await?;
let (mut send, mut recv) = new_conn.connection.open_bi ().await?; let (mut send, mut recv) = new_conn.connection.open_bi ().await?;
let cmd_type = Command::CONNECT_P4_TO_P3.0; 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 expect_exact_response (&mut recv, [Command::OKAY.0, cmd_type, 0, 0]).await
.context ("P4 didn't get OK response when connecting to P3")?; .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 { pub enum P3ToP4Stream {
NewPtthConnection { NewPtthConnection {
client_id: String, client_id: PeerId,
} }
} }