p2p/connection/incoming/
mod.rs

1mod p2p_connection_incoming_state;
2pub use p2p_connection_incoming_state::*;
3
4mod p2p_connection_incoming_actions;
5pub use p2p_connection_incoming_actions::*;
6
7mod p2p_connection_incoming_reducer;
8
9use malloc_size_of_derive::MallocSizeOf;
10use serde::{Deserialize, Serialize};
11
12use crate::{connection::RejectionReason, webrtc, P2pState, PeerId};
13
14#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
15pub struct P2pConnectionIncomingInitOpts {
16    pub peer_id: PeerId,
17    pub signaling: IncomingSignalingMethod,
18    pub offer: Box<webrtc::Offer>,
19}
20
21// TODO(binier): maybe move to `crate::webrtc`?
22#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone, Copy, MallocSizeOf)]
23pub enum IncomingSignalingMethod {
24    /// Http rpc is used for sending offer and getting answer as a response.
25    Http,
26    /// Intermediary/Relay peer is used for exchanging offer and answer messages.
27    P2p { relay_peer_id: PeerId },
28}
29
30impl P2pState {
31    pub fn incoming_accept(
32        &self,
33        peer_id: PeerId,
34        offer: &webrtc::Offer,
35    ) -> Result<(), RejectionReason> {
36        if self.chain_id != offer.chain_id {
37            return Err(RejectionReason::ChainIdMismatch);
38        }
39
40        if peer_id != offer.identity_pub_key.peer_id() {
41            return Err(RejectionReason::PeerIdAndPublicKeyMismatch);
42        }
43
44        let my_peer_id = self.my_id();
45
46        if offer.target_peer_id != my_peer_id {
47            return Err(RejectionReason::TargetPeerIdNotMe);
48        }
49
50        if peer_id == my_peer_id {
51            return Err(RejectionReason::ConnectingToSelf);
52        }
53
54        if self.is_peer_connected_or_connecting(&peer_id) {
55            // Both nodes trying to connect to each other at the same time.
56            // Choose connection arbitrarily based on peer id.
57            if peer_id > my_peer_id {
58                return Ok(());
59            }
60            return Err(RejectionReason::AlreadyConnected);
61        }
62
63        if self.already_has_max_ready_peers() {
64            return Err(RejectionReason::PeerCapacityFull);
65        }
66
67        Ok(())
68    }
69
70    pub fn libp2p_incoming_accept(&self, peer_id: PeerId) -> Result<(), RejectionReason> {
71        if peer_id == self.my_id() {
72            return Err(RejectionReason::ConnectingToSelf);
73        }
74
75        if self.already_has_max_ready_peers() {
76            return Err(RejectionReason::PeerCapacityFull);
77        }
78
79        Ok(())
80    }
81}