libp2p/builder/
select_security.rs

1// Copyright 2023 Protocol Labs.
2// Copyright 2018 Parity Technologies (UK) Ltd.
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22use either::Either;
23use futures::future::MapOk;
24use futures::{future, TryFutureExt};
25use libp2p_core::either::EitherFuture;
26use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};
27use libp2p_identity::PeerId;
28use std::iter::{Chain, Map};
29
30/// Upgrade that combines two upgrades into one. Supports all the protocols supported by either
31/// sub-upgrade.
32///
33/// The protocols supported by the first element have a higher priority.
34#[derive(Debug, Clone)]
35pub struct SelectSecurityUpgrade<A, B>(A, B);
36
37impl<A, B> SelectSecurityUpgrade<A, B> {
38    /// Combines two upgrades into an `SelectUpgrade`.
39    ///
40    /// The protocols supported by the first element have a higher priority.
41    pub fn new(a: A, b: B) -> Self {
42        SelectSecurityUpgrade(a, b)
43    }
44}
45
46impl<A, B> UpgradeInfo for SelectSecurityUpgrade<A, B>
47where
48    A: UpgradeInfo,
49    B: UpgradeInfo,
50{
51    type Info = Either<A::Info, B::Info>;
52    type InfoIter = Chain<
53        Map<<A::InfoIter as IntoIterator>::IntoIter, fn(A::Info) -> Self::Info>,
54        Map<<B::InfoIter as IntoIterator>::IntoIter, fn(B::Info) -> Self::Info>,
55    >;
56
57    fn protocol_info(&self) -> Self::InfoIter {
58        let a = self
59            .0
60            .protocol_info()
61            .into_iter()
62            .map(Either::Left as fn(A::Info) -> _);
63        let b = self
64            .1
65            .protocol_info()
66            .into_iter()
67            .map(Either::Right as fn(B::Info) -> _);
68
69        a.chain(b)
70    }
71}
72
73impl<C, A, B, TA, TB, EA, EB> InboundUpgrade<C> for SelectSecurityUpgrade<A, B>
74where
75    A: InboundUpgrade<C, Output = (PeerId, TA), Error = EA>,
76    B: InboundUpgrade<C, Output = (PeerId, TB), Error = EB>,
77{
78    type Output = (PeerId, future::Either<TA, TB>);
79    type Error = Either<EA, EB>;
80    type Future = MapOk<
81        EitherFuture<A::Future, B::Future>,
82        fn(future::Either<(PeerId, TA), (PeerId, TB)>) -> (PeerId, future::Either<TA, TB>),
83    >;
84
85    fn upgrade_inbound(self, sock: C, info: Self::Info) -> Self::Future {
86        match info {
87            Either::Left(info) => EitherFuture::First(self.0.upgrade_inbound(sock, info)),
88            Either::Right(info) => EitherFuture::Second(self.1.upgrade_inbound(sock, info)),
89        }
90        .map_ok(future::Either::factor_first)
91    }
92}
93
94impl<C, A, B, TA, TB, EA, EB> OutboundUpgrade<C> for SelectSecurityUpgrade<A, B>
95where
96    A: OutboundUpgrade<C, Output = (PeerId, TA), Error = EA>,
97    B: OutboundUpgrade<C, Output = (PeerId, TB), Error = EB>,
98{
99    type Output = (PeerId, future::Either<TA, TB>);
100    type Error = Either<EA, EB>;
101    type Future = MapOk<
102        EitherFuture<A::Future, B::Future>,
103        fn(future::Either<(PeerId, TA), (PeerId, TB)>) -> (PeerId, future::Either<TA, TB>),
104    >;
105
106    fn upgrade_outbound(self, sock: C, info: Self::Info) -> Self::Future {
107        match info {
108            Either::Left(info) => EitherFuture::First(self.0.upgrade_outbound(sock, info)),
109            Either::Right(info) => EitherFuture::Second(self.1.upgrade_outbound(sock, info)),
110        }
111        .map_ok(future::Either::factor_first)
112    }
113}