Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh-net)!: ALPNs can be bytes, not just strings #2377

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions iroh-gossip/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ async fn handle_connection(
let alpn = conn.alpn().await?;
let conn = conn.await?;
let peer_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
match alpn.as_bytes() {
GOSSIP_ALPN => gossip
.handle_connection(conn)
.await
.context(format!("connection to {peer_id} with ALPN {alpn} failed"))?,
match alpn.as_ref() {
GOSSIP_ALPN => gossip.handle_connection(conn).await.context(format!(
"connection to {peer_id} with ALPN {} failed",
String::from_utf8_lossy(&alpn)
))?,
_ => println!("> ignoring connection from {peer_id}: unsupported ALPN protocol"),
}
Ok(())
Expand Down
3 changes: 2 additions & 1 deletion iroh-net/examples/listen-unreliable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ async fn main() -> anyhow::Result<()> {
let conn = conn.await?;
let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
info!(
"new (unreliable) connection from {node_id} with ALPN {alpn} (coming from {})",
"new (unreliable) connection from {node_id} with ALPN {} (coming from {})",
String::from_utf8_lossy(&alpn),
conn.remote_address()
);
// spawn a task to handle reading and writing off of the connection
Expand Down
3 changes: 2 additions & 1 deletion iroh-net/examples/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ async fn main() -> anyhow::Result<()> {
let conn = conn.await?;
let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
info!(
"new connection from {node_id} with ALPN {alpn} (coming from {})",
"new connection from {node_id} with ALPN {} (coming from {})",
String::from_utf8_lossy(&alpn),
conn.remote_address()
);

Expand Down
6 changes: 3 additions & 3 deletions iroh-net/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,11 @@ impl Connecting {
/// Extracts the ALPN protocol from the peer's handshake data.
// Note, we could totally provide this method to be on a Connection as well. But we'd
// need to wrap Connection too.
pub async fn alpn(&mut self) -> Result<String> {
pub async fn alpn(&mut self) -> Result<Vec<u8>> {
let data = self.handshake_data().await?;
match data.downcast::<quinn::crypto::rustls::HandshakeData>() {
Ok(data) => match data.protocol {
Some(protocol) => std::string::String::from_utf8(protocol).map_err(Into::into),
Some(protocol) => Ok(protocol),
None => bail!("no ALPN protocol available"),
},
Err(_) => bail!("unknown handshake type"),
Expand Down Expand Up @@ -1365,7 +1365,7 @@ mod tests {
let conn = incoming.await.unwrap();
let node_id = get_remote_node_id(&conn).unwrap();
assert_eq!(node_id, src);
assert_eq!(alpn.as_bytes(), TEST_ALPN);
assert_eq!(alpn, TEST_ALPN);
let (mut send, mut recv) = conn.accept_bi().await.unwrap();
let m = recv.read_to_end(100).await.unwrap();
assert_eq!(m, b"hello");
Expand Down
4 changes: 2 additions & 2 deletions iroh/src/node/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -734,12 +734,12 @@ impl Default for GcPolicy {
#[allow(clippy::too_many_arguments)]
async fn handle_connection<D: BaoStore>(
connecting: iroh_net::endpoint::Connecting,
alpn: String,
alpn: Vec<u8>,
node: Arc<NodeInner<D>>,
gossip: Gossip,
sync: DocsEngine,
) -> Result<()> {
match alpn.as_bytes() {
match alpn.as_ref() {
GOSSIP_ALPN => gossip.handle_connection(connecting.await?).await?,
DOCS_ALPN => sync.handle_connection(connecting).await?,
alpn if alpn == iroh_blobs::protocol::ALPN => {
Expand Down
Loading