This commit adds an implementation for the circuit relay v2 protocol to be used
as a relay server, i.e. it supports incoming HOP requests and outgoing STOP
requests and used as a relay clients, i.e. outgoing HOP requests and incoming
STOP requests.
The existing circuit relay v1 protocol implementation is moved to
protocols/relay/src/v1.
Co-authored-by: ronzigelman <ronzigelman@gmail.com>
Co-authored-by: Marco Munizaga <git@marcopolo.io>
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
Co-authored-by: Elena Frank <57632201+elenaf9@users.noreply.github.com>
This commit implements the [libp2p circuit
relay](https://github.com/libp2p/specs/tree/master/relay) specification. It is
based on previous work from https://github.com/libp2p/rust-libp2p/pull/1134.
Instead of altering the `Transport` trait, the approach taken in this commit
is to wrap an existing implementation of `Transport` allowing one to:
- Intercept `dial` requests with a relayed address.
- Inject incoming relayed connections with the local node being the destination.
- Intercept `listen_on` requests pointing to a relay, ensuring to keep a
constant connection to the relay, waiting for incoming requests with the local
node being the destination.
More concretely one would wrap an existing `Transport` implementation as seen
below, allowing the `Relay` behaviour and the `RelayTransport` to communicate
via channels.
### Example
```rust
let (relay_transport, relay_behaviour) = new_transport_and_behaviour(
RelayConfig::default(),
MemoryTransport::default(),
);
let transport = relay_transport
.upgrade(upgrade::Version::V1)
.authenticate(plaintext)
.multiplex(YamuxConfig::default())
.boxed();
let mut swarm = Swarm::new(transport, relay_behaviour, local_peer_id);
let relay_addr = Multiaddr::from_str("/memory/1234").unwrap()
.with(Protocol::P2p(PeerId::random().into()))
.with(Protocol::P2pCircuit);
let dst_addr = relay_addr.clone().with(Protocol::Memory(5678));
// Listen for incoming connections via relay node (1234).
Swarm::listen_on(&mut swarm, relay_addr).unwrap();
// Dial node (5678) via relay node (1234).
Swarm::dial_addr(&mut swarm, dst_addr).unwrap();
```
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: David Craven <david@craven.ch>