Skip to content

Commit f57c34f

Browse files
fix(iroh-net)!: ALPNs can be bytes, not just strings (#2377)
## Breaking Changes - `iroh_net::endpoint::Connecting::alpn` returns `Vec<u8>` instead of `String`
1 parent a5e5939 commit f57c34f

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

iroh-gossip/examples/chat.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ async fn handle_connection(
206206
let alpn = conn.alpn().await?;
207207
let conn = conn.await?;
208208
let peer_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
209-
match alpn.as_bytes() {
210-
GOSSIP_ALPN => gossip
211-
.handle_connection(conn)
212-
.await
213-
.context(format!("connection to {peer_id} with ALPN {alpn} failed"))?,
209+
match alpn.as_ref() {
210+
GOSSIP_ALPN => gossip.handle_connection(conn).await.context(format!(
211+
"connection to {peer_id} with ALPN {} failed",
212+
String::from_utf8_lossy(&alpn)
213+
))?,
214214
_ => println!("> ignoring connection from {peer_id}: unsupported ALPN protocol"),
215215
}
216216
Ok(())

iroh-net/examples/listen-unreliable.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ async fn main() -> anyhow::Result<()> {
6767
let conn = conn.await?;
6868
let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
6969
info!(
70-
"new (unreliable) connection from {node_id} with ALPN {alpn} (coming from {})",
70+
"new (unreliable) connection from {node_id} with ALPN {} (coming from {})",
71+
String::from_utf8_lossy(&alpn),
7172
conn.remote_address()
7273
);
7374
// spawn a task to handle reading and writing off of the connection

iroh-net/examples/listen.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ async fn main() -> anyhow::Result<()> {
6666
let conn = conn.await?;
6767
let node_id = iroh_net::endpoint::get_remote_node_id(&conn)?;
6868
info!(
69-
"new connection from {node_id} with ALPN {alpn} (coming from {})",
69+
"new connection from {node_id} with ALPN {} (coming from {})",
70+
String::from_utf8_lossy(&alpn),
7071
conn.remote_address()
7172
);
7273

iroh-net/src/endpoint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,11 +908,11 @@ impl Connecting {
908908
/// Extracts the ALPN protocol from the peer's handshake data.
909909
// Note, we could totally provide this method to be on a Connection as well. But we'd
910910
// need to wrap Connection too.
911-
pub async fn alpn(&mut self) -> Result<String> {
911+
pub async fn alpn(&mut self) -> Result<Vec<u8>> {
912912
let data = self.handshake_data().await?;
913913
match data.downcast::<quinn::crypto::rustls::HandshakeData>() {
914914
Ok(data) => match data.protocol {
915-
Some(protocol) => std::string::String::from_utf8(protocol).map_err(Into::into),
915+
Some(protocol) => Ok(protocol),
916916
None => bail!("no ALPN protocol available"),
917917
},
918918
Err(_) => bail!("unknown handshake type"),
@@ -1365,7 +1365,7 @@ mod tests {
13651365
let conn = incoming.await.unwrap();
13661366
let node_id = get_remote_node_id(&conn).unwrap();
13671367
assert_eq!(node_id, src);
1368-
assert_eq!(alpn.as_bytes(), TEST_ALPN);
1368+
assert_eq!(alpn, TEST_ALPN);
13691369
let (mut send, mut recv) = conn.accept_bi().await.unwrap();
13701370
let m = recv.read_to_end(100).await.unwrap();
13711371
assert_eq!(m, b"hello");

iroh/src/node/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,12 +734,12 @@ impl Default for GcPolicy {
734734
#[allow(clippy::too_many_arguments)]
735735
async fn handle_connection<D: BaoStore>(
736736
connecting: iroh_net::endpoint::Connecting,
737-
alpn: String,
737+
alpn: Vec<u8>,
738738
node: Arc<NodeInner<D>>,
739739
gossip: Gossip,
740740
sync: DocsEngine,
741741
) -> Result<()> {
742-
match alpn.as_bytes() {
742+
match alpn.as_ref() {
743743
GOSSIP_ALPN => gossip.handle_connection(connecting.await?).await?,
744744
DOCS_ALPN => sync.handle_connection(connecting).await?,
745745
alpn if alpn == iroh_blobs::protocol::ALPN => {

0 commit comments

Comments
 (0)