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
//! ckb network module
//!
//! This module is based on the Tentacle library, once again abstract the context that protocols can use,
//! and providing a unified implementation of the peer storage and registration mechanism.
//!
//! And implemented several basic protocols: identify, discovery, ping, feeler, disconnect_message
//!

mod behaviour;
/// compress module
pub mod compress;
pub mod errors;
pub mod network;
mod network_group;
mod peer;
pub mod peer_registry;
pub mod peer_store;
mod protocols;
mod services;

#[cfg(test)]
mod tests;

pub use crate::{
    behaviour::Behaviour,
    errors::Error,
    network::{
        DefaultExitHandler, EventHandler, ExitHandler, NetworkController, NetworkService,
        NetworkState,
    },
    peer::{Peer, PeerIdentifyInfo},
    peer_registry::PeerRegistry,
    peer_store::Score,
    protocols::{
        identify::Flags, support_protocols::SupportProtocols, CKBProtocol, CKBProtocolContext,
        CKBProtocolHandler, PeerIndex,
    },
};
pub use p2p::{
    async_trait,
    builder::ServiceBuilder,
    bytes, multiaddr,
    secio::{PeerId, PublicKey},
    service::{ServiceControl, SessionType, TargetProtocol, TargetSession},
    traits::ServiceProtocol,
    utils::{extract_peer_id, multiaddr_to_socketaddr},
    ProtocolId, SessionId,
};
pub use tokio;

/// Protocol version used by network protocol open
pub type ProtocolVersion = String;

/// Observe listen port occupancy
pub async fn observe_listen_port_occupancy(
    _addrs: &[multiaddr::MultiAddr],
) -> Result<(), std::io::Error> {
    #[cfg(target_os = "linux")]
    {
        use p2p::utils::dns::DnsResolver;
        use std::net::{SocketAddr, TcpListener};

        for raw_addr in _addrs {
            let ip_addr: Option<SocketAddr> = match DnsResolver::new(raw_addr.clone()) {
                Some(dns) => dns.await.ok().as_ref().and_then(multiaddr_to_socketaddr),
                None => multiaddr_to_socketaddr(raw_addr),
            };

            if let Some(addr) = ip_addr {
                if let Err(e) = TcpListener::bind(addr) {
                    ckb_logger::error!(
                        "addr {} can't use on your machines by error: {}, please check",
                        raw_addr,
                        e
                    );
                    return Err(e);
                }
            }
        }
    }

    Ok(())
}