1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
// Copyright 2023 litep2p developers
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//! Transport protocol implementations provided by [`Litep2p`](`crate::Litep2p`).
use crate::{error::DialError, transport::manager::TransportHandle, types::ConnectionId, PeerId};
use futures::Stream;
use multiaddr::Multiaddr;
use std::{fmt::Debug, time::Duration};
pub(crate) mod common;
#[cfg(feature = "quic")]
pub mod quic;
pub mod tcp;
#[cfg(feature = "webrtc")]
pub mod webrtc;
#[cfg(feature = "websocket")]
pub mod websocket;
pub(crate) mod dummy;
pub(crate) mod manager;
pub use manager::limits::{ConnectionLimitsConfig, ConnectionLimitsError};
/// Timeout for opening a connection.
pub(crate) const CONNECTION_OPEN_TIMEOUT: Duration = Duration::from_secs(10);
/// Timeout for opening a substream.
pub(crate) const SUBSTREAM_OPEN_TIMEOUT: Duration = Duration::from_secs(5);
/// Timeout for connection waiting new substreams.
pub(crate) const KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(5);
/// Maximum number of parallel dial attempts.
pub(crate) const MAX_PARALLEL_DIALS: usize = 8;
/// Connection endpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
/// Successfully established outbound connection.
Dialer {
/// Address that was dialed.
address: Multiaddr,
/// Connection ID.
connection_id: ConnectionId,
},
/// Successfully established inbound connection.
Listener {
/// Local connection address.
address: Multiaddr,
/// Connection ID.
connection_id: ConnectionId,
},
}
impl Endpoint {
/// Get `Multiaddr` of the [`Endpoint`].
pub fn address(&self) -> &Multiaddr {
match self {
Self::Dialer { address, .. } => address,
Self::Listener { address, .. } => address,
}
}
/// Crate dialer.
pub(crate) fn dialer(address: Multiaddr, connection_id: ConnectionId) -> Self {
Endpoint::Dialer {
address,
connection_id,
}
}
/// Create listener.
pub(crate) fn listener(address: Multiaddr, connection_id: ConnectionId) -> Self {
Endpoint::Listener {
address,
connection_id,
}
}
/// Get `ConnectionId` of the `Endpoint`.
pub fn connection_id(&self) -> ConnectionId {
match self {
Self::Dialer { connection_id, .. } => *connection_id,
Self::Listener { connection_id, .. } => *connection_id,
}
}
/// Is this a listener endpoint?
pub fn is_listener(&self) -> bool {
std::matches!(self, Self::Listener { .. })
}
}
/// Transport event.
#[derive(Debug)]
pub(crate) enum TransportEvent {
/// Fully negotiated connection established to remote peer.
ConnectionEstablished {
/// Peer ID.
peer: PeerId,
/// Endpoint.
endpoint: Endpoint,
},
PendingInboundConnection {
/// Connection ID.
connection_id: ConnectionId,
},
/// Connection opened to remote but not yet negotiated.
ConnectionOpened {
/// Connection ID.
connection_id: ConnectionId,
/// Address that was dialed.
address: Multiaddr,
},
/// Connection closed to remote peer.
#[allow(unused)]
ConnectionClosed {
/// Peer ID.
peer: PeerId,
/// Connection ID.
connection_id: ConnectionId,
},
/// Failed to dial remote peer.
DialFailure {
/// Connection ID.
connection_id: ConnectionId,
/// Dialed address.
address: Multiaddr,
/// Error.
error: DialError,
},
/// Open failure for an unnegotiated set of connections.
OpenFailure {
/// Connection ID.
connection_id: ConnectionId,
/// Errors.
errors: Vec<(Multiaddr, DialError)>,
},
}
pub(crate) trait TransportBuilder {
type Config: Debug;
type Transport: Transport;
/// Create new [`Transport`] object.
fn new(context: TransportHandle, config: Self::Config) -> crate::Result<(Self, Vec<Multiaddr>)>
where
Self: Sized;
}
pub(crate) trait Transport: Stream + Unpin + Send {
/// Dial `address` and negotiate connection.
fn dial(&mut self, connection_id: ConnectionId, address: Multiaddr) -> crate::Result<()>;
/// Accept negotiated connection.
fn accept(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
/// Accept pending connection.
fn accept_pending(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
/// Reject pending connection.
fn reject_pending(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
/// Reject negotiated connection.
fn reject(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
/// Attempt to open connection to remote peer over one or more addresses.
///
/// TODO: documentation
fn open(&mut self, connection_id: ConnectionId, addresses: Vec<Multiaddr>)
-> crate::Result<()>;
/// Negotiate opened connection.
///
/// TODO: documentation
fn negotiate(&mut self, connection_id: ConnectionId) -> crate::Result<()>;
/// Cancel opening connections.
///
/// This is a no-op for connections that have already succeeded/canceled.
fn cancel(&mut self, connection_id: ConnectionId);
}