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

transports/{noise,tls}: Select muxer in security handshake #2994

Open
mxinden opened this issue Oct 6, 2022 · 2 comments
Open

transports/{noise,tls}: Select muxer in security handshake #2994

mxinden opened this issue Oct 6, 2022 · 2 comments

Comments

@mxinden
Copy link
Member

mxinden commented Oct 6, 2022

Description

Implement muxer selection in libp2p-noise and libp2p-tls according to the specification libp2p/specs#446.

Motivation

This document discribes an imporvement on the connection upgrade process. The
goal of the improvement is to reduce the number of RTTs that takes to select the
muxer of a connection. The solution relies on the ability of the security
protocol's handshake process to negotiate higher level protocols, which enables
the muxer selection to be carried out along with security protocol handshake.
The proposed solution saves the RTT of multistream selection for muxers.

See libp2p/specs#446

Requirements

Open questions

Are you planning to do it yourself in a pull request?

No

@MaboroshiChan
Copy link

Investigation

I have found that the following codes establish security protocols and multiplexer protocols

pub fn authenticate<C, D, U, E>(
        self,
        upgrade: U,
    ) -> Authenticated<AndThen<T, impl FnOnce(C, ConnectedPoint) -> Authenticate<C, U> + Clone>>
    where
        T: Transport<Output = C>,
        C: AsyncRead + AsyncWrite + Unpin,
        D: AsyncRead + AsyncWrite + Unpin,
        U: InboundUpgrade<Negotiated<C>, Output = (PeerId, D), Error = E>,
        U: OutboundUpgrade<Negotiated<C>, Output = (PeerId, D), Error = E> + Clone,
        E: Error + 'static,
    {
        let version = self.version;
        Authenticated(Builder::new(
            self.inner.and_then(move |conn, endpoint| Authenticate {
                inner: upgrade::apply(conn, upgrade, endpoint, version),
            }),
            version,
        ))
    }
pub fn multiplex<C, M, U, E>(
        self,
        upgrade: U,
    ) -> Multiplexed<AndThen<T, impl FnOnce((PeerId, C), ConnectedPoint) -> Multiplex<C, U> + Clone>>
    where
        T: Transport<Output = (PeerId, C)>,
        C: AsyncRead + AsyncWrite + Unpin,
        M: StreamMuxer,
        U: InboundUpgrade<Negotiated<C>, Output = M, Error = E>,
        U: OutboundUpgrade<Negotiated<C>, Output = M, Error = E> + Clone,
        E: Error + 'static,
    {
        let version = self.0.version;
        Multiplexed(self.0.inner.and_then(move |(i, c), endpoint| {
            let upgrade = upgrade::apply(c, upgrade, endpoint, version);
            
            Multiplex {
                peer_id: Some(i),
                upgrade,
            }
        }))
    }

These two functions authenticate and multiplex call the function upgrade::apply.

Here we have the implementation of upgrade::apply function,

pub fn apply<C, U>(
    conn: C,
    up: U,
    cp: ConnectedPoint,
    v: Version,
) -> Either<InboundUpgradeApply<C, U>, OutboundUpgradeApply<C, U>>
where
    C: AsyncRead + AsyncWrite + Unpin,
    U: InboundUpgrade<Negotiated<C>> + OutboundUpgrade<Negotiated<C>>,
{
    match cp {
        ConnectedPoint::Dialer { role_override, .. } if role_override.is_dialer() => {
            Either::Right(apply_outbound(conn, up, v))
        }
        _ => Either::Left(apply_inbound(conn, up)),
    }
}

functions named apply_inbound and apply_outbound are called.

Both functions also call multistream_select::listener_select_proto and multistream_select::dialer_select_proto respectively, which involve multistream-select mechanism.

By investigating the codes, these two functions will be called twice. One for selecting the security protocol, and the other for multiplexer protocol.

@MaboroshiChan
Copy link

MaboroshiChan commented Nov 9, 2022

Solution

The solution would be required to modify both authenticate and multiplex functions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants