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

Add functions for incoming multi-stream connections #2982

Merged
merged 2 commits into from
Nov 11, 2022
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
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