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

Allow setting custom protocol in mplex #2332

Merged
merged 8 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions muxers/mplex/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
hanabi1224 marked this conversation as resolved.
Show resolved Hide resolved

/// Configuration for the multiplexer.
#[derive(Debug, Clone)]
pub struct MplexConfig {
Expand All @@ -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 {
Expand Down Expand Up @@ -84,6 +88,17 @@ impl MplexConfig {
self.split_send_size = size;
self
}

/// Gets protocol
pub fn protocol(&self) -> &[u8] {
self.protocol
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the other fields have a getter method. Why is this one needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's useful for debugging/logging purposes to be able to retrieve the protocol name when it can be customized


/// Sets protocol, muxer.set_protocol(b"/mplex/6.7.0")
hanabi1224 marked this conversation as resolved.
Show resolved Hide resolved
pub fn set_protocol(&mut self, protocol: &'static [u8]) -> &mut Self {
hanabi1224 marked this conversation as resolved.
Show resolved Hide resolved
self.protocol = protocol;
self
}
}

/// Behaviour when the maximum length of the buffer is reached.
Expand Down Expand Up @@ -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,
hanabi1224 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
1 change: 1 addition & 0 deletions muxers/mplex/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl UpgradeInfo for MplexConfig {
type InfoIter = iter::Once<Self::Info>;

fn protocol_info(&self) -> Self::InfoIter {
iter::once(b"/mplex/6.7.0")
iter::once(self.protocol)
}
}

Expand Down
59 changes: 59 additions & 0 deletions muxers/mplex/tests/two_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
_ => {}
}
});
}