p2p/network/
p2p_network_service.rs

1use std::net::{IpAddr, SocketAddr};
2
3use crate::ConnectionAddr;
4
5/// The state machine sends commands to the service.
6pub enum MioCmd {
7    /// Bind a new listener to a new socket on the interface
8    /// that previously reported by an `InterfaceAppear` event with the ip.
9    ListenOn(SocketAddr),
10    /// Accept an incoming connection that previously reported
11    /// by an `IncomingConnectionIsReady` event with the listener address.
12    Accept(SocketAddr),
13    /// Refuse to connect the incoming connection previously reported.
14    Refuse(SocketAddr),
15    /// Create a new outgoing connection to the socket.
16    Connect(SocketAddr),
17    /// Receive some data from the connection.
18    Recv(ConnectionAddr, usize),
19    /// Send the data in the connection.
20    Send(ConnectionAddr, Box<[u8]>),
21    /// Disconnect the remote peer.
22    Disconnect(ConnectionAddr),
23}
24
25pub trait P2pMioService: redux::Service {
26    fn start_mio(&mut self);
27    fn send_mio_cmd(&mut self, cmd: MioCmd);
28}
29
30pub trait P2pCryptoService: redux::Service {
31    fn generate_random_nonce(&mut self) -> [u8; 24];
32
33    fn ephemeral_sk(&mut self) -> [u8; 32];
34    fn static_sk(&mut self) -> [u8; 32];
35
36    fn sign_key(&mut self, key: &[u8; 32]) -> Vec<u8>;
37
38    fn sign_publication(&mut self, publication: &[u8]) -> Vec<u8>;
39    fn verify_publication(
40        &mut self,
41        pk: &libp2p_identity::PublicKey,
42        publication: &[u8],
43        sig: &[u8],
44    ) -> bool;
45}
46
47#[derive(Debug, thiserror::Error)]
48pub enum P2pNetworkServiceError {
49    #[error("error resolving host: {0}")]
50    Resolve(String),
51    #[error("error looking up local ip: {0}")]
52    LocalIp(String),
53}
54
55pub trait P2pNetworkService {
56    /// Resolves DNS name.
57    // TODO: make it asyncronous.
58    fn resolve_name(&mut self, host: &str) -> Result<Vec<IpAddr>, P2pNetworkServiceError>;
59
60    /// Detects local IP addresses matching the mask.
61    fn detect_local_ip(&mut self) -> Result<Vec<IpAddr>, P2pNetworkServiceError>;
62}