From 50c4d23fdca71bba6ff6ead098f068f96877ec8c Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Tue, 9 Nov 2021 18:36:42 +0800 Subject: [PATCH 1/6] Allow setting custom protocol in mplex --- muxers/mplex/src/config.rs | 16 +++++++++ muxers/mplex/src/io.rs | 1 + muxers/mplex/src/lib.rs | 2 +- muxers/mplex/tests/two_peers.rs | 59 +++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 1 deletion(-) diff --git a/muxers/mplex/src/config.rs b/muxers/mplex/src/config.rs index df12c0ce617..5573f0ce562 100644 --- a/muxers/mplex/src/config.rs +++ b/muxers/mplex/src/config.rs @@ -21,6 +21,8 @@ use crate::codec::MAX_FRAME_SIZE; use std::cmp; +pub(crate) const DEFAULT_MPLEX_PROTOCOL: &'static [u8] = b"/mplex/6.7.0"; + /// Configuration for the multiplexer. #[derive(Debug, Clone)] pub struct MplexConfig { @@ -33,6 +35,8 @@ pub struct MplexConfig { /// When sending data, split it into frames whose maximum size is this value /// (max 1MByte, as per the Mplex spec). pub(crate) split_send_size: usize, + /// Protocol defaults to b"/mplex/6.7.0" + pub(crate) protocol: &'static [u8], } impl MplexConfig { @@ -84,6 +88,17 @@ impl MplexConfig { self.split_send_size = size; self } + + /// Gets protocol + pub fn protocol(&self) -> &[u8] { + self.protocol + } + + /// Sets protocol, muxer.set_protocol(b"/mplex/6.7.0") + pub fn set_protocol(&mut self, protocol: &'static [u8]) -> &mut Self { + self.protocol = protocol; + self + } } /// Behaviour when the maximum length of the buffer is reached. @@ -120,6 +135,7 @@ impl Default for MplexConfig { max_buffer_len: 32, max_buffer_behaviour: MaxBufferBehaviour::Block, split_send_size: 8 * 1024, + protocol: DEFAULT_MPLEX_PROTOCOL, } } } diff --git a/muxers/mplex/src/io.rs b/muxers/mplex/src/io.rs index e475b15763b..53b70628e6c 100644 --- a/muxers/mplex/src/io.rs +++ b/muxers/mplex/src/io.rs @@ -1126,6 +1126,7 @@ mod tests { max_buffer_len: g.gen_range(1, 1000), max_buffer_behaviour: MaxBufferBehaviour::arbitrary(g), split_send_size: g.gen_range(1, 10000), + protocol: crate::config::DEFAULT_MPLEX_PROTOCOL, } } } diff --git a/muxers/mplex/src/lib.rs b/muxers/mplex/src/lib.rs index 05e7571cf87..1fb15e40d98 100644 --- a/muxers/mplex/src/lib.rs +++ b/muxers/mplex/src/lib.rs @@ -40,7 +40,7 @@ impl UpgradeInfo for MplexConfig { type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { - iter::once(b"/mplex/6.7.0") + iter::once(self.protocol) } } diff --git a/muxers/mplex/tests/two_peers.rs b/muxers/mplex/tests/two_peers.rs index eb0526f4044..c8b2819e955 100644 --- a/muxers/mplex/tests/two_peers.rs +++ b/muxers/mplex/tests/two_peers.rs @@ -160,3 +160,62 @@ fn client_to_server_inbound() { bg_thread.await; }); } + +#[test] +fn protocol_not_match() { + let (tx, rx) = oneshot::channel(); + + let _bg_thread = async_std::task::spawn(async move { + let mplex = libp2p_mplex::MplexConfig::new(); + + let transport = TcpConfig::new() + .and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); + + let mut listener = transport + .listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()) + .unwrap(); + + let addr = listener + .next() + .await + .expect("some event") + .expect("no error") + .into_new_address() + .expect("listen address"); + + tx.send(addr).unwrap(); + + let client = listener + .next() + .await + .unwrap() + .unwrap() + .into_upgrade() + .unwrap() + .0 + .await + .unwrap(); + + let mut outbound = muxing::outbound_from_ref_and_wrap(Arc::new(client)) + .await + .unwrap(); + + let mut buf = Vec::new(); + outbound.read_to_end(&mut buf).await.unwrap(); + assert_eq!(buf, b"hello world"); + }); + + async_std::task::block_on(async { + // Make sure they do not connect when protocols do not match + let mut mplex = libp2p_mplex::MplexConfig::new(); + mplex.set_protocol(b"/mplextest/1.0.0"); + let transport = TcpConfig::new() + .and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); + match transport.dial(rx.await.unwrap()).unwrap().await { + Ok(_) => { + assert!(false, "Dialing should fail here as protocols do not match") + } + _ => {} + } + }); +} From b13c0f70f22e466ae8e0347110b8bac8683e2c89 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Sat, 20 Nov 2021 12:52:11 +0800 Subject: [PATCH 2/6] Apply suggestions from code review Co-authored-by: Max Inden --- muxers/mplex/src/config.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/muxers/mplex/src/config.rs b/muxers/mplex/src/config.rs index 5573f0ce562..5430f634367 100644 --- a/muxers/mplex/src/config.rs +++ b/muxers/mplex/src/config.rs @@ -21,7 +21,7 @@ use crate::codec::MAX_FRAME_SIZE; use std::cmp; -pub(crate) const DEFAULT_MPLEX_PROTOCOL: &'static [u8] = b"/mplex/6.7.0"; +pub(crate) const DEFAULT_MPLEX_PROTOCOL_NAME: &'static [u8] = b"/mplex/6.7.0"; /// Configuration for the multiplexer. #[derive(Debug, Clone)] @@ -94,8 +94,12 @@ impl MplexConfig { self.protocol } - /// Sets protocol, muxer.set_protocol(b"/mplex/6.7.0") - pub fn set_protocol(&mut self, protocol: &'static [u8]) -> &mut Self { + /// Set protocol name. + /// + /// ```rust + /// muxer_config.set_protocol(b"/mplex/6.7.0"); + /// ``` + pub fn set_protocol_name(&mut self, protocol: &'static [u8]) -> &mut Self { self.protocol = protocol; self } @@ -135,7 +139,7 @@ impl Default for MplexConfig { max_buffer_len: 32, max_buffer_behaviour: MaxBufferBehaviour::Block, split_send_size: 8 * 1024, - protocol: DEFAULT_MPLEX_PROTOCOL, + protocol_name: DEFAULT_MPLEX_PROTOCOL, } } } From e89a2a113efcd017323ede476ac205796ebdd7e0 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Sat, 20 Nov 2021 13:06:45 +0800 Subject: [PATCH 3/6] changelog --- Cargo.toml | 2 +- muxers/mplex/CHANGELOG.md | 4 ++++ muxers/mplex/Cargo.toml | 2 +- muxers/mplex/src/config.rs | 22 ++++++++++++---------- muxers/mplex/src/io.rs | 2 +- muxers/mplex/src/lib.rs | 2 +- muxers/mplex/tests/two_peers.rs | 2 +- 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7a013d18843..407f3bf0b41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,7 @@ libp2p-gossipsub = { version = "0.34.0", path = "./protocols/gossipsub", optiona libp2p-identify = { version = "0.32.0", path = "protocols/identify", optional = true } libp2p-kad = { version = "0.33.0", path = "protocols/kad", optional = true } libp2p-metrics = { version = "0.2.0", path = "misc/metrics", optional = true } -libp2p-mplex = { version = "0.30.0", path = "muxers/mplex", optional = true } +libp2p-mplex = { version = "0.31.0", path = "muxers/mplex", optional = true } libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true } libp2p-ping = { version = "0.32.0", path = "protocols/ping", optional = true } libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true } diff --git a/muxers/mplex/CHANGELOG.md b/muxers/mplex/CHANGELOG.md index 90573fd513f..c1830dfc1a3 100644 --- a/muxers/mplex/CHANGELOG.md +++ b/muxers/mplex/CHANGELOG.md @@ -1,3 +1,7 @@ +# unreleased + +- Add `set_protocol_name` to MplexConfig + # 0.30.0 [2021-11-01] - Make default features of `libp2p-core` optional. diff --git a/muxers/mplex/Cargo.toml b/muxers/mplex/Cargo.toml index 285fe386517..28d6b5b023b 100644 --- a/muxers/mplex/Cargo.toml +++ b/muxers/mplex/Cargo.toml @@ -2,7 +2,7 @@ name = "libp2p-mplex" edition = "2018" description = "Mplex multiplexing protocol for libp2p" -version = "0.30.0" +version = "0.31.0" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/muxers/mplex/src/config.rs b/muxers/mplex/src/config.rs index 5430f634367..9c05bef714a 100644 --- a/muxers/mplex/src/config.rs +++ b/muxers/mplex/src/config.rs @@ -35,8 +35,8 @@ pub struct MplexConfig { /// When sending data, split it into frames whose maximum size is this value /// (max 1MByte, as per the Mplex spec). pub(crate) split_send_size: usize, - /// Protocol defaults to b"/mplex/6.7.0" - pub(crate) protocol: &'static [u8], + /// Protocol name, defaults to b"/mplex/6.7.0" + pub(crate) protocol_name: &'static [u8], } impl MplexConfig { @@ -89,18 +89,20 @@ impl MplexConfig { self } - /// Gets protocol - pub fn protocol(&self) -> &[u8] { - self.protocol + /// Gets the protocol anme + pub fn protocol_name(&self) -> &[u8] { + self.protocol_name } - /// Set protocol name. + /// Set the protocol name. /// /// ```rust - /// muxer_config.set_protocol(b"/mplex/6.7.0"); + /// use libp2p_mplex::MplexConfig; + /// let mut muxer_config = MplexConfig::new(); + /// muxer_config.set_protocol_name(b"/mplex/6.7.0"); /// ``` - pub fn set_protocol_name(&mut self, protocol: &'static [u8]) -> &mut Self { - self.protocol = protocol; + pub fn set_protocol_name(&mut self, protocol_name: &'static [u8]) -> &mut Self { + self.protocol_name = protocol_name; self } } @@ -139,7 +141,7 @@ impl Default for MplexConfig { max_buffer_len: 32, max_buffer_behaviour: MaxBufferBehaviour::Block, split_send_size: 8 * 1024, - protocol_name: DEFAULT_MPLEX_PROTOCOL, + protocol_name: DEFAULT_MPLEX_PROTOCOL_NAME, } } } diff --git a/muxers/mplex/src/io.rs b/muxers/mplex/src/io.rs index 53b70628e6c..ec782439efe 100644 --- a/muxers/mplex/src/io.rs +++ b/muxers/mplex/src/io.rs @@ -1126,7 +1126,7 @@ mod tests { max_buffer_len: g.gen_range(1, 1000), max_buffer_behaviour: MaxBufferBehaviour::arbitrary(g), split_send_size: g.gen_range(1, 10000), - protocol: crate::config::DEFAULT_MPLEX_PROTOCOL, + protocol_name: crate::config::DEFAULT_MPLEX_PROTOCOL_NAME, } } } diff --git a/muxers/mplex/src/lib.rs b/muxers/mplex/src/lib.rs index 1fb15e40d98..0f8598c5eef 100644 --- a/muxers/mplex/src/lib.rs +++ b/muxers/mplex/src/lib.rs @@ -40,7 +40,7 @@ impl UpgradeInfo for MplexConfig { type InfoIter = iter::Once; fn protocol_info(&self) -> Self::InfoIter { - iter::once(self.protocol) + iter::once(self.protocol_name) } } diff --git a/muxers/mplex/tests/two_peers.rs b/muxers/mplex/tests/two_peers.rs index c8b2819e955..6be1cc4a4de 100644 --- a/muxers/mplex/tests/two_peers.rs +++ b/muxers/mplex/tests/two_peers.rs @@ -208,7 +208,7 @@ fn protocol_not_match() { async_std::task::block_on(async { // Make sure they do not connect when protocols do not match let mut mplex = libp2p_mplex::MplexConfig::new(); - mplex.set_protocol(b"/mplextest/1.0.0"); + mplex.set_protocol_name(b"/mplextest/1.0.0"); let transport = TcpConfig::new() .and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1)); match transport.dial(rx.await.unwrap()).unwrap().await { From 340c5cea77e33d291a88ffc62d0b95931f1fe5fa Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Sat, 20 Nov 2021 13:26:34 +0800 Subject: [PATCH 4/6] readme --- muxers/mplex/CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/muxers/mplex/CHANGELOG.md b/muxers/mplex/CHANGELOG.md index c1830dfc1a3..b885d469b2b 100644 --- a/muxers/mplex/CHANGELOG.md +++ b/muxers/mplex/CHANGELOG.md @@ -1,6 +1,7 @@ # unreleased -- Add `set_protocol_name` to MplexConfig +- Add `fn set_protocol_name(&mut self, protocol_name: &'static [u8])` to MplexConfig +- Add `fn protocol_name(&self)` to MplexConfig # 0.30.0 [2021-11-01] From b65dc9e4f0a44d3d3d9ab4671dc3caa4e1676ba9 Mon Sep 17 00:00:00 2001 From: hanabi1224 Date: Tue, 23 Nov 2021 21:40:54 +0800 Subject: [PATCH 5/6] resolve comments --- Cargo.toml | 2 +- muxers/mplex/CHANGELOG.md | 3 +-- muxers/mplex/Cargo.toml | 2 +- muxers/mplex/src/config.rs | 5 ----- 4 files changed, 3 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 407f3bf0b41..64a621f240b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,7 +77,7 @@ libp2p-gossipsub = { version = "0.34.0", path = "./protocols/gossipsub", optiona libp2p-identify = { version = "0.32.0", path = "protocols/identify", optional = true } libp2p-kad = { version = "0.33.0", path = "protocols/kad", optional = true } libp2p-metrics = { version = "0.2.0", path = "misc/metrics", optional = true } -libp2p-mplex = { version = "0.31.0", path = "muxers/mplex", optional = true } +libp2p-mplex = { version = "0.30.1", path = "muxers/mplex", optional = true } libp2p-noise = { version = "0.33.0", path = "transports/noise", optional = true } libp2p-ping = { version = "0.32.0", path = "protocols/ping", optional = true } libp2p-plaintext = { version = "0.30.0", path = "transports/plaintext", optional = true } diff --git a/muxers/mplex/CHANGELOG.md b/muxers/mplex/CHANGELOG.md index b885d469b2b..a50e1107155 100644 --- a/muxers/mplex/CHANGELOG.md +++ b/muxers/mplex/CHANGELOG.md @@ -1,7 +1,6 @@ -# unreleased +# 0.30.1 - Add `fn set_protocol_name(&mut self, protocol_name: &'static [u8])` to MplexConfig -- Add `fn protocol_name(&self)` to MplexConfig # 0.30.0 [2021-11-01] diff --git a/muxers/mplex/Cargo.toml b/muxers/mplex/Cargo.toml index 28d6b5b023b..06462ae736d 100644 --- a/muxers/mplex/Cargo.toml +++ b/muxers/mplex/Cargo.toml @@ -2,7 +2,7 @@ name = "libp2p-mplex" edition = "2018" description = "Mplex multiplexing protocol for libp2p" -version = "0.31.0" +version = "0.30.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/muxers/mplex/src/config.rs b/muxers/mplex/src/config.rs index 9c05bef714a..b6166f827e5 100644 --- a/muxers/mplex/src/config.rs +++ b/muxers/mplex/src/config.rs @@ -89,11 +89,6 @@ impl MplexConfig { self } - /// Gets the protocol anme - pub fn protocol_name(&self) -> &[u8] { - self.protocol_name - } - /// Set the protocol name. /// /// ```rust From 40dc7c59334fbbb6bcb2f06278fcd8bdb77c81cd Mon Sep 17 00:00:00 2001 From: Max Inden Date: Tue, 23 Nov 2021 17:30:44 +0100 Subject: [PATCH 6/6] muxers/mplex/CHANGELOG.md: Add [unreleased] tag --- muxers/mplex/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/muxers/mplex/CHANGELOG.md b/muxers/mplex/CHANGELOG.md index a50e1107155..90788596223 100644 --- a/muxers/mplex/CHANGELOG.md +++ b/muxers/mplex/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.30.1 +# 0.30.1 [unreleased] - Add `fn set_protocol_name(&mut self, protocol_name: &'static [u8])` to MplexConfig