Skip to content

Commit

Permalink
Add functions for incoming multi-stream connections (#2982)
Browse files Browse the repository at this point in the history
They're unused in practice, but there's no reason to not allow ingoing
multi-stream connections in the underlying state machine.

cc #2802

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
tomaka and mergify[bot] committed Nov 11, 2022
1 parent 621e0f5 commit 02f856d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/libp2p/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ where
///
/// Must be passed the moment (as a `TNow`) when the connection as been established, in order
/// to determine when the handshake timeout expires.
// TODO: add an is_initiator parameter? right now we're always implicitly the initiator
pub fn insert_multi_stream<TSubId>(
&mut self,
now: TNow,
handshake_kind: MultiStreamHandshakeKind,
is_initiator: bool,
user_data: TConn,
) -> (ConnectionId, MultiStreamConnectionTask<TNow, TSubId>)
where
Expand All @@ -421,6 +421,7 @@ where

let connection_task = MultiStreamConnectionTask::new(
self.randomness_seeds.gen(),
is_initiator,
now,
handshake_kind,
self.max_inbound_substreams,
Expand Down
16 changes: 10 additions & 6 deletions src/libp2p/collection/multi_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ where
// a function only called from the parent module.
pub(super) fn new(
randomness_seed: [u8; 32],
is_initiator: bool,
now: TNow,
handshake_kind: MultiStreamHandshakeKind,
max_inbound_substreams: usize,
Expand All @@ -151,7 +152,6 @@ where
// In the WebRTC handshake, the Noise prologue must be set to `"libp2p-webrtc-noise:"`
// followed with the multihash-encoded fingerprints of the initiator's certificate
// and the receiver's certificate.
// TODO: we currently assume that the local node is always the initiator
// See <https://github.com/libp2p/specs/pull/412>.
let noise_prologue = {
let MultiStreamHandshakeKind::WebRtc {
Expand All @@ -165,18 +165,22 @@ where
+ remote_tls_certificate_multihash.len(),
);
out.extend_from_slice(PREFIX);
// Since smoldot always acts as a client (at least right now), we don't need to change
// the order of fingerprints.
out.extend_from_slice(&local_tls_certificate_multihash);
out.extend_from_slice(&remote_tls_certificate_multihash);
if is_initiator {
out.extend_from_slice(&local_tls_certificate_multihash);
out.extend_from_slice(&remote_tls_certificate_multihash);
} else {
out.extend_from_slice(&remote_tls_certificate_multihash);
out.extend_from_slice(&local_tls_certificate_multihash);
}
out
};

MultiStreamConnectionTask {
connection: MultiStreamConnectionTaskInner::Handshake {
handshake: Some(noise::HandshakeInProgress::new(noise::Config {
key: &noise_key,
is_initiator: false, // TODO: is_initiator?
// It's the "server" that initiates the Noise handshake.
is_initiator: !is_initiator,
prologue: &noise_prologue,
})),
opened_substream: None,
Expand Down
29 changes: 29 additions & 0 deletions src/libp2p/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,34 @@ where
(connection_id, connection_task)
}

/// Inserts a multi-stream incoming connection in the state machine.
///
/// This connection hasn't finished handshaking and the [`PeerId`] of the remote isn't known
/// yet.
///
/// Must be passed the moment (as a `TNow`) when the connection as been established, in order
/// to determine when the handshake timeout expires.
pub fn add_multi_stream_incoming_connection<TSubId>(
&mut self,
when_connected: TNow,
handshake_kind: MultiStreamHandshakeKind,
user_data: TConn,
) -> (ConnectionId, MultiStreamConnectionTask<TNow, TSubId>)
where
TSubId: Clone + PartialEq + Eq + Hash,
{
self.inner.insert_multi_stream(
when_connected,
handshake_kind,
false,
Connection {
peer_index: None,
user_data,
outbound: false,
},
)
}

/// Inserts a multi-stream outgoing connection in the state machine.
///
/// This connection hasn't finished handshaking, and the [`PeerId`] of the remote isn't known
Expand All @@ -1003,6 +1031,7 @@ where
let (connection_id, connection_task) = self.inner.insert_multi_stream(
when_connected,
handshake_kind,
true,
Connection {
peer_index: Some(peer_index),
user_data,
Expand Down
24 changes: 24 additions & 0 deletions src/network/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,30 @@ where
)
}

/// Adds a multi-stream incoming connection to the state machine.
///
/// This connection hasn't finished handshaking and the [`PeerId`] of the remote isn't known
/// yet.
///
/// Must be passed the moment (as a `TNow`) when the connection as been established, in order
/// to determine when the handshake timeout expires.
///
/// The `remote_addr` is the address used to reach back the remote. In the case of TCP, it
/// contains the TCP dialing port of the remote. The remote can ask, through the `identify`
/// libp2p protocol, its own address, in which case we send it.
pub fn add_multi_stream_incoming_connection<TSubId>(
&mut self,
when_connected: TNow,
handshake_kind: MultiStreamHandshakeKind,
remote_addr: multiaddr::Multiaddr,
) -> (ConnectionId, MultiStreamConnectionTask<TNow, TSubId>)
where
TSubId: Clone + PartialEq + Eq + Hash,
{
self.inner
.add_multi_stream_incoming_connection(when_connected, handshake_kind, remote_addr)
}

pub fn pull_message_to_connection(
&mut self,
) -> Option<(ConnectionId, CoordinatorToConnection<TNow>)> {
Expand Down

0 comments on commit 02f856d

Please sign in to comment.