2018-03-15 15:18:21 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
//! The Kademlia connection protocol upgrade and associated message types.
|
2018-11-29 12:11:35 +01:00
|
|
|
//!
|
2019-05-08 10:10:58 +02:00
|
|
|
//! The connection protocol upgrade is provided by [`KademliaProtocolConfig`], with the
|
|
|
|
//! request and response types [`KadRequestMsg`] and [`KadResponseMsg`], respectively.
|
|
|
|
//! The upgrade's output is a `Sink + Stream` of messages. The `Stream` component is used
|
|
|
|
//! to poll the underlying transport for incoming messages, and the `Sink` component
|
|
|
|
//! is used to send messages to remote peers.
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
use bytes::BytesMut;
|
2019-05-08 10:10:58 +02:00
|
|
|
use codec::UviBytes;
|
|
|
|
use crate::protobuf_structs::dht as proto;
|
|
|
|
use futures::{future::{self, FutureResult}, sink, stream, Sink, Stream};
|
|
|
|
use libp2p_core::{Multiaddr, PeerId};
|
|
|
|
use libp2p_core::upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated};
|
2018-10-01 18:40:35 +02:00
|
|
|
use multihash::Multihash;
|
2018-03-15 15:18:21 +01:00
|
|
|
use protobuf::{self, Message};
|
2019-05-15 15:44:51 +02:00
|
|
|
use std::{borrow::Cow, convert::TryFrom};
|
2019-05-08 10:10:58 +02:00
|
|
|
use std::{io, iter};
|
2018-06-22 16:12:23 +02:00
|
|
|
use tokio_codec::Framed;
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
2018-08-10 16:35:41 +02:00
|
|
|
use unsigned_varint::codec;
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Status of our connection to a node reported by the Kademlia protocol.
|
2018-03-15 15:18:21 +01:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
2018-07-17 15:51:11 +02:00
|
|
|
pub enum KadConnectionType {
|
2018-03-15 15:18:21 +01:00
|
|
|
/// Sender hasn't tried to connect to peer.
|
|
|
|
NotConnected = 0,
|
|
|
|
/// Sender is currently connected to peer.
|
|
|
|
Connected = 1,
|
|
|
|
/// Sender was recently connected to peer.
|
|
|
|
CanConnect = 2,
|
|
|
|
/// Sender tried to connect to peer but failed.
|
|
|
|
CannotConnect = 3,
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
impl From<proto::Message_ConnectionType> for KadConnectionType {
|
2018-03-15 15:18:21 +01:00
|
|
|
#[inline]
|
2019-05-08 10:10:58 +02:00
|
|
|
fn from(raw: proto::Message_ConnectionType) -> KadConnectionType {
|
|
|
|
use proto::Message_ConnectionType::{
|
2019-01-21 10:33:51 +00:00
|
|
|
CAN_CONNECT, CANNOT_CONNECT, CONNECTED, NOT_CONNECTED
|
|
|
|
};
|
2018-03-15 15:18:21 +01:00
|
|
|
match raw {
|
2018-07-17 15:51:11 +02:00
|
|
|
NOT_CONNECTED => KadConnectionType::NotConnected,
|
|
|
|
CONNECTED => KadConnectionType::Connected,
|
|
|
|
CAN_CONNECT => KadConnectionType::CanConnect,
|
|
|
|
CANNOT_CONNECT => KadConnectionType::CannotConnect,
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
impl Into<proto::Message_ConnectionType> for KadConnectionType {
|
2018-03-15 15:18:21 +01:00
|
|
|
#[inline]
|
2019-05-08 10:10:58 +02:00
|
|
|
fn into(self) -> proto::Message_ConnectionType {
|
|
|
|
use proto::Message_ConnectionType::{
|
2019-01-21 10:33:51 +00:00
|
|
|
CAN_CONNECT, CANNOT_CONNECT, CONNECTED, NOT_CONNECTED
|
|
|
|
};
|
2018-03-15 15:18:21 +01:00
|
|
|
match self {
|
2018-07-17 15:51:11 +02:00
|
|
|
KadConnectionType::NotConnected => NOT_CONNECTED,
|
|
|
|
KadConnectionType::Connected => CONNECTED,
|
|
|
|
KadConnectionType::CanConnect => CAN_CONNECT,
|
|
|
|
KadConnectionType::CannotConnect => CANNOT_CONNECT,
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Information about a peer, as known by the sender.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2018-07-17 15:51:11 +02:00
|
|
|
pub struct KadPeer {
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Identifier of the peer.
|
2018-03-15 15:18:21 +01:00
|
|
|
pub node_id: PeerId,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// The multiaddresses that the sender think can be used in order to reach the peer.
|
2018-03-15 15:18:21 +01:00
|
|
|
pub multiaddrs: Vec<Multiaddr>,
|
2018-11-29 12:11:35 +01:00
|
|
|
/// How the sender is connected to that remote.
|
2018-07-17 15:51:11 +02:00
|
|
|
pub connection_ty: KadConnectionType,
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
// Builds a `KadPeer` from a corresponding protobuf message.
|
|
|
|
impl TryFrom<&mut proto::Message_Peer> for KadPeer {
|
|
|
|
type Error = io::Error;
|
|
|
|
|
|
|
|
fn try_from(peer: &mut proto::Message_Peer) -> Result<KadPeer, Self::Error> {
|
2018-10-29 20:38:32 +11:00
|
|
|
// TODO: this is in fact a CID; not sure if this should be handled in `from_bytes` or
|
2018-06-14 16:34:07 +02:00
|
|
|
// as a special case here
|
|
|
|
let node_id = PeerId::from_bytes(peer.get_id().to_vec())
|
2019-05-08 10:10:58 +02:00
|
|
|
.map_err(|_| invalid_data("invalid peer id"))?;
|
2018-06-14 16:34:07 +02:00
|
|
|
|
|
|
|
let mut addrs = Vec::with_capacity(peer.get_addrs().len());
|
|
|
|
for addr in peer.take_addrs().into_iter() {
|
2019-05-08 10:10:58 +02:00
|
|
|
let as_ma = Multiaddr::try_from(addr).map_err(invalid_data)?;
|
2018-06-14 16:34:07 +02:00
|
|
|
addrs.push(as_ma);
|
|
|
|
}
|
|
|
|
debug_assert_eq!(addrs.len(), addrs.capacity());
|
|
|
|
|
2018-03-15 15:18:21 +01:00
|
|
|
let connection_ty = peer.get_connection().into();
|
|
|
|
|
2018-07-17 15:51:11 +02:00
|
|
|
Ok(KadPeer {
|
2019-01-30 15:41:54 +01:00
|
|
|
node_id,
|
2018-03-15 15:18:21 +01:00
|
|
|
multiaddrs: addrs,
|
2019-01-30 15:41:54 +01:00
|
|
|
connection_ty
|
2018-06-14 16:34:07 +02:00
|
|
|
})
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
impl Into<proto::Message_Peer> for KadPeer {
|
|
|
|
fn into(self) -> proto::Message_Peer {
|
|
|
|
let mut out = proto::Message_Peer::new();
|
2018-03-15 15:18:21 +01:00
|
|
|
out.set_id(self.node_id.into_bytes());
|
|
|
|
for addr in self.multiaddrs {
|
2019-04-08 10:57:09 +02:00
|
|
|
out.mut_addrs().push(addr.to_vec());
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
out.set_connection(self.connection_ty.into());
|
|
|
|
out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Configuration for a Kademlia connection upgrade. When applied to a connection, turns this
|
2018-11-29 12:11:35 +01:00
|
|
|
/// connection into a `Stream + Sink` whose items are of type `KadRequestMsg` and `KadResponseMsg`.
|
|
|
|
// TODO: if, as suspected, we can confirm with Protocol Labs that each open Kademlia substream does
|
|
|
|
// only one request, then we can change the output of the `InboundUpgrade` and
|
|
|
|
// `OutboundUpgrade` to be just a single message
|
2019-05-15 15:44:51 +02:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct KademliaProtocolConfig {
|
|
|
|
protocol_name: Cow<'static, [u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl KademliaProtocolConfig {
|
|
|
|
/// Modifies the protocol name used on the wire. Can be used to create incompatibilities
|
|
|
|
/// between networks on purpose.
|
|
|
|
pub fn with_protocol_name(mut self, name: impl Into<Cow<'static, [u8]>>) -> Self {
|
|
|
|
self.protocol_name = name.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for KademliaProtocolConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
KademliaProtocolConfig {
|
|
|
|
protocol_name: Cow::Borrowed(b"/ipfs/kad/1.0.0"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
impl UpgradeInfo for KademliaProtocolConfig {
|
2019-05-15 15:44:51 +02:00
|
|
|
type Info = Cow<'static, [u8]>;
|
2018-12-11 15:13:10 +01:00
|
|
|
type InfoIter = iter::Once<Self::Info>;
|
2018-11-15 17:41:11 +01:00
|
|
|
|
2018-12-11 15:13:10 +01:00
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
2019-05-15 15:44:51 +02:00
|
|
|
iter::once(self.protocol_name.clone())
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<C> InboundUpgrade<C> for KademliaProtocolConfig
|
2018-03-15 15:18:21 +01:00
|
|
|
where
|
2018-11-29 12:11:35 +01:00
|
|
|
C: AsyncRead + AsyncWrite,
|
2018-03-15 15:18:21 +01:00
|
|
|
{
|
2019-03-19 17:27:30 +01:00
|
|
|
type Output = KadInStreamSink<Negotiated<C>>;
|
2019-05-08 10:10:58 +02:00
|
|
|
type Future = FutureResult<Self::Output, io::Error>;
|
|
|
|
type Error = io::Error;
|
2018-03-15 15:18:21 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_inbound(self, incoming: Negotiated<C>, _: Self::Info) -> Self::Future {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut codec = UviBytes::default();
|
2019-02-06 14:33:45 +01:00
|
|
|
codec.set_max_len(4096);
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
future::ok(
|
2019-02-06 14:33:45 +01:00
|
|
|
Framed::new(incoming, codec)
|
2019-05-08 10:10:58 +02:00
|
|
|
.from_err()
|
|
|
|
.with::<_, fn(_) -> _, _>(|response| {
|
2018-11-29 12:11:35 +01:00
|
|
|
let proto_struct = resp_msg_to_proto(response);
|
2019-05-08 10:10:58 +02:00
|
|
|
proto_struct.write_to_bytes().map_err(invalid_data)
|
2018-11-29 12:11:35 +01:00
|
|
|
})
|
2019-05-08 10:10:58 +02:00
|
|
|
.and_then::<fn(_) -> _, _>(|bytes| {
|
2018-11-29 12:11:35 +01:00
|
|
|
let request = protobuf::parse_from_bytes(&bytes)?;
|
|
|
|
proto_to_req_msg(request)
|
|
|
|
}),
|
|
|
|
)
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
2018-11-15 17:41:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<C> OutboundUpgrade<C> for KademliaProtocolConfig
|
|
|
|
where
|
2018-11-29 12:11:35 +01:00
|
|
|
C: AsyncRead + AsyncWrite,
|
2018-11-15 17:41:11 +01:00
|
|
|
{
|
2019-03-19 17:27:30 +01:00
|
|
|
type Output = KadOutStreamSink<Negotiated<C>>;
|
2019-05-08 10:10:58 +02:00
|
|
|
type Future = FutureResult<Self::Output, io::Error>;
|
|
|
|
type Error = io::Error;
|
2018-03-15 15:18:21 +01:00
|
|
|
|
|
|
|
#[inline]
|
2019-03-19 17:27:30 +01:00
|
|
|
fn upgrade_outbound(self, incoming: Negotiated<C>, _: Self::Info) -> Self::Future {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut codec = UviBytes::default();
|
2019-02-06 14:33:45 +01:00
|
|
|
codec.set_max_len(4096);
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
future::ok(
|
2019-02-06 14:33:45 +01:00
|
|
|
Framed::new(incoming, codec)
|
2019-05-08 10:10:58 +02:00
|
|
|
.from_err()
|
|
|
|
.with::<_, fn(_) -> _, _>(|request| {
|
2018-11-29 12:11:35 +01:00
|
|
|
let proto_struct = req_msg_to_proto(request);
|
2019-05-08 10:10:58 +02:00
|
|
|
proto_struct.write_to_bytes().map_err(invalid_data)
|
2018-11-29 12:11:35 +01:00
|
|
|
})
|
2019-05-08 10:10:58 +02:00
|
|
|
.and_then::<fn(_) -> _, _>(|bytes| {
|
2018-11-29 12:11:35 +01:00
|
|
|
let response = protobuf::parse_from_bytes(&bytes)?;
|
|
|
|
proto_to_resp_msg(response)
|
|
|
|
}),
|
|
|
|
)
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Sink of responses and stream of requests.
|
2019-05-08 10:10:58 +02:00
|
|
|
pub type KadInStreamSink<S> = KadStreamSink<S, KadResponseMsg, KadRequestMsg>;
|
2018-11-29 12:11:35 +01:00
|
|
|
|
|
|
|
/// Sink of requests and stream of responses.
|
2019-05-08 10:10:58 +02:00
|
|
|
pub type KadOutStreamSink<S> = KadStreamSink<S, KadRequestMsg, KadResponseMsg>;
|
|
|
|
|
|
|
|
pub type KadStreamSink<S, A, B> = stream::AndThen<
|
2018-11-29 12:11:35 +01:00
|
|
|
sink::With<
|
2019-05-08 10:10:58 +02:00
|
|
|
stream::FromErr<Framed<S, UviBytes<Vec<u8>>>, io::Error>,
|
|
|
|
A,
|
|
|
|
fn(A) -> Result<Vec<u8>, io::Error>,
|
|
|
|
Result<Vec<u8>, io::Error>,
|
2018-11-29 12:11:35 +01:00
|
|
|
>,
|
2019-05-08 10:10:58 +02:00
|
|
|
fn(BytesMut) -> Result<B, io::Error>,
|
|
|
|
Result<B, io::Error>,
|
2018-11-29 12:11:35 +01:00
|
|
|
>;
|
|
|
|
|
|
|
|
/// Request that we can send to a peer or that we received from a peer.
|
2018-03-15 15:18:21 +01:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2018-11-29 12:11:35 +01:00
|
|
|
pub enum KadRequestMsg {
|
|
|
|
/// Ping request.
|
2018-03-15 15:18:21 +01:00
|
|
|
Ping,
|
2018-10-01 18:40:35 +02:00
|
|
|
|
2018-03-15 15:18:21 +01:00
|
|
|
/// Request for the list of nodes whose IDs are the closest to `key`. The number of nodes
|
|
|
|
/// returned is not specified, but should be around 20.
|
2018-11-29 12:11:35 +01:00
|
|
|
FindNode {
|
2018-03-15 15:18:21 +01:00
|
|
|
/// Identifier of the node.
|
2018-10-01 18:40:35 +02:00
|
|
|
key: PeerId,
|
2018-03-15 15:18:21 +01:00
|
|
|
},
|
2018-10-01 18:40:35 +02:00
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Same as `FindNode`, but should also return the entries of the local providers list for
|
2018-10-01 18:40:35 +02:00
|
|
|
/// this key.
|
2018-11-29 12:11:35 +01:00
|
|
|
GetProviders {
|
2018-10-01 18:40:35 +02:00
|
|
|
/// Identifier being searched.
|
|
|
|
key: Multihash,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Indicates that this list of providers is known for this key.
|
|
|
|
AddProvider {
|
|
|
|
/// Key for which we should add providers.
|
|
|
|
key: Multihash,
|
|
|
|
/// Known provider for this key.
|
|
|
|
provider_peer: KadPeer,
|
|
|
|
},
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
/// Response that we can send to a peer or that we received from a peer.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
pub enum KadResponseMsg {
|
|
|
|
/// Ping response.
|
|
|
|
Pong,
|
|
|
|
|
|
|
|
/// Response to a `FindNode`.
|
|
|
|
FindNode {
|
|
|
|
/// Results of the request.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
},
|
|
|
|
|
|
|
|
/// Response to a `GetProviders`.
|
|
|
|
GetProviders {
|
|
|
|
/// Nodes closest to the key.
|
|
|
|
closer_peers: Vec<KadPeer>,
|
|
|
|
/// Known providers for this key.
|
|
|
|
provider_peers: Vec<KadPeer>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
/// Converts a `KadRequestMsg` into the corresponding protobuf message for sending.
|
|
|
|
fn req_msg_to_proto(kad_msg: KadRequestMsg) -> proto::Message {
|
2018-03-15 15:18:21 +01:00
|
|
|
match kad_msg {
|
2018-11-29 12:11:35 +01:00
|
|
|
KadRequestMsg::Ping => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::PING);
|
2018-03-15 15:18:21 +01:00
|
|
|
msg
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KadRequestMsg::FindNode { key } => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::FIND_NODE);
|
2018-10-01 18:40:35 +02:00
|
|
|
msg.set_key(key.into_bytes());
|
2018-11-29 12:11:35 +01:00
|
|
|
msg.set_clusterLevelRaw(10);
|
2018-03-15 15:18:21 +01:00
|
|
|
msg
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KadRequestMsg::GetProviders { key } => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::GET_PROVIDERS);
|
2018-10-01 18:40:35 +02:00
|
|
|
msg.set_key(key.into_bytes());
|
2018-03-15 15:18:21 +01:00
|
|
|
msg.set_clusterLevelRaw(10);
|
|
|
|
msg
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KadRequestMsg::AddProvider { key, provider_peer } => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::ADD_PROVIDER);
|
2018-03-15 15:18:21 +01:00
|
|
|
msg.set_clusterLevelRaw(10);
|
2018-11-29 12:11:35 +01:00
|
|
|
msg.set_key(key.into_bytes());
|
|
|
|
msg.mut_providerPeers().push(provider_peer.into());
|
|
|
|
msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
/// Converts a `KadResponseMsg` into the corresponding protobuf message for sending.
|
|
|
|
fn resp_msg_to_proto(kad_msg: KadResponseMsg) -> proto::Message {
|
2018-11-29 12:11:35 +01:00
|
|
|
match kad_msg {
|
|
|
|
KadResponseMsg::Pong => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::PING);
|
2018-03-15 15:18:21 +01:00
|
|
|
msg
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KadResponseMsg::FindNode { closer_peers } => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::FIND_NODE);
|
2018-03-15 15:18:21 +01:00
|
|
|
msg.set_clusterLevelRaw(9);
|
|
|
|
for peer in closer_peers {
|
|
|
|
msg.mut_closerPeers().push(peer.into());
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
KadResponseMsg::GetProviders {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
} => {
|
2019-05-08 10:10:58 +02:00
|
|
|
let mut msg = proto::Message::new();
|
|
|
|
msg.set_field_type(proto::Message_MessageType::GET_PROVIDERS);
|
2018-10-01 18:40:35 +02:00
|
|
|
msg.set_clusterLevelRaw(9);
|
|
|
|
for peer in closer_peers {
|
|
|
|
msg.mut_closerPeers().push(peer.into());
|
|
|
|
}
|
|
|
|
for peer in provider_peers {
|
|
|
|
msg.mut_providerPeers().push(peer.into());
|
|
|
|
}
|
|
|
|
msg
|
|
|
|
}
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
/// Converts a received protobuf message into a corresponding `KadRequestMsg`.
|
|
|
|
///
|
|
|
|
/// Fails if the protobuf message is not a valid and supported Kademlia request message.
|
|
|
|
fn proto_to_req_msg(mut message: proto::Message) -> Result<KadRequestMsg, io::Error> {
|
2018-03-15 15:18:21 +01:00
|
|
|
match message.get_field_type() {
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::PING => Ok(KadRequestMsg::Ping),
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::PUT_VALUE =>
|
|
|
|
Err(invalid_data("Unsupported: PUT_VALUE message.")),
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::GET_VALUE =>
|
|
|
|
Err(invalid_data("Unsupported: GET_VALUE message.")),
|
2018-03-15 15:18:21 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::FIND_NODE => {
|
|
|
|
let key = PeerId::from_bytes(message.take_key())
|
|
|
|
.map_err(|_| invalid_data("Invalid peer id in FIND_NODE"))?;
|
2018-11-29 12:11:35 +01:00
|
|
|
Ok(KadRequestMsg::FindNode { key })
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::GET_PROVIDERS => {
|
|
|
|
let key = Multihash::from_bytes(message.take_key()).map_err(invalid_data)?;
|
2018-11-29 12:11:35 +01:00
|
|
|
Ok(KadRequestMsg::GetProviders { key })
|
2018-10-01 18:40:35 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::ADD_PROVIDER => {
|
2018-10-01 18:40:35 +02:00
|
|
|
// TODO: for now we don't parse the peer properly, so it is possible that we get
|
2018-10-29 20:38:32 +11:00
|
|
|
// parsing errors for peers even when they are valid; we ignore these
|
2018-10-01 18:40:35 +02:00
|
|
|
// errors for now, but ultimately we should just error altogether
|
2018-11-29 12:11:35 +01:00
|
|
|
let provider_peer = message
|
|
|
|
.mut_providerPeers()
|
2018-10-01 18:40:35 +02:00
|
|
|
.iter_mut()
|
2019-05-08 10:10:58 +02:00
|
|
|
.find_map(|peer| KadPeer::try_from(peer).ok());
|
2018-10-01 18:40:35 +02:00
|
|
|
|
|
|
|
if let Some(provider_peer) = provider_peer {
|
2019-05-08 10:10:58 +02:00
|
|
|
let key = Multihash::from_bytes(message.take_key()).map_err(invalid_data)?;
|
2018-11-29 12:11:35 +01:00
|
|
|
Ok(KadRequestMsg::AddProvider { key, provider_peer })
|
2018-10-01 18:40:35 +02:00
|
|
|
} else {
|
2019-05-08 10:10:58 +02:00
|
|
|
Err(invalid_data("ADD_PROVIDER message with no valid peer."))
|
2018-10-01 18:40:35 +02:00
|
|
|
}
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
/// Converts a received protobuf message into a corresponding `KadResponseMessage`.
|
|
|
|
///
|
|
|
|
/// Fails if the protobuf message is not a valid and supported Kademlia response message.
|
|
|
|
fn proto_to_resp_msg(mut message: proto::Message) -> Result<KadResponseMsg, io::Error> {
|
2018-11-29 12:11:35 +01:00
|
|
|
match message.get_field_type() {
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::PING => Ok(KadResponseMsg::Pong),
|
2018-11-29 12:11:35 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::GET_VALUE =>
|
|
|
|
Err(invalid_data("Unsupported: GET_VALUE message")),
|
2018-11-29 12:11:35 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::FIND_NODE => {
|
2018-11-29 12:11:35 +01:00
|
|
|
let closer_peers = message
|
|
|
|
.mut_closerPeers()
|
|
|
|
.iter_mut()
|
2019-05-08 10:10:58 +02:00
|
|
|
.filter_map(|peer| KadPeer::try_from(peer).ok())
|
2018-11-29 12:11:35 +01:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(KadResponseMsg::FindNode { closer_peers })
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::GET_PROVIDERS => {
|
2018-11-29 12:11:35 +01:00
|
|
|
let closer_peers = message
|
|
|
|
.mut_closerPeers()
|
|
|
|
.iter_mut()
|
2019-05-08 10:10:58 +02:00
|
|
|
.filter_map(|peer| KadPeer::try_from(peer).ok())
|
2018-11-29 12:11:35 +01:00
|
|
|
.collect::<Vec<_>>();
|
2019-05-08 10:10:58 +02:00
|
|
|
|
2018-11-29 12:11:35 +01:00
|
|
|
let provider_peers = message
|
|
|
|
.mut_providerPeers()
|
|
|
|
.iter_mut()
|
2019-05-08 10:10:58 +02:00
|
|
|
.filter_map(|peer| KadPeer::try_from(peer).ok())
|
2018-11-29 12:11:35 +01:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(KadResponseMsg::GetProviders {
|
|
|
|
closer_peers,
|
|
|
|
provider_peers,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::PUT_VALUE =>
|
|
|
|
Err(invalid_data("received an unexpected PUT_VALUE message")),
|
2018-11-29 12:11:35 +01:00
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
proto::Message_MessageType::ADD_PROVIDER =>
|
|
|
|
Err(invalid_data("received an unexpected ADD_PROVIDER message"))
|
2018-11-29 12:11:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 10:10:58 +02:00
|
|
|
/// Creates an `io::Error` with `io::ErrorKind::InvalidData`.
|
|
|
|
fn invalid_data<E>(e: E) -> io::Error
|
|
|
|
where
|
|
|
|
E: Into<Box<dyn std::error::Error + Send + Sync>>
|
|
|
|
{
|
|
|
|
io::Error::new(io::ErrorKind::InvalidData, e)
|
|
|
|
}
|
|
|
|
|
2018-03-15 15:18:21 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
2018-12-05 15:01:17 +01:00
|
|
|
/*// TODO: restore
|
2018-12-10 13:39:11 +01:00
|
|
|
use self::libp2p_tcp::TcpConfig;
|
2018-11-29 12:11:35 +01:00
|
|
|
use self::tokio::runtime::current_thread::Runtime;
|
2018-03-15 15:18:21 +01:00
|
|
|
use futures::{Future, Sink, Stream};
|
2018-11-29 12:11:35 +01:00
|
|
|
use libp2p_core::{PeerId, PublicKey, Transport};
|
2018-10-01 18:40:35 +02:00
|
|
|
use multihash::{encode, Hash};
|
2018-11-29 12:11:35 +01:00
|
|
|
use protocol::{KadConnectionType, KadPeer, KademliaProtocolConfig};
|
2018-03-15 15:18:21 +01:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn correct_transfer() {
|
|
|
|
// We open a server and a client, send a message between the two, and check that they were
|
|
|
|
// successfully received.
|
|
|
|
|
|
|
|
test_one(KadMsg::Ping);
|
|
|
|
test_one(KadMsg::FindNodeReq {
|
2018-11-29 12:11:35 +01:00
|
|
|
key: PeerId::random(),
|
2018-03-15 15:18:21 +01:00
|
|
|
});
|
|
|
|
test_one(KadMsg::FindNodeRes {
|
2018-11-29 12:11:35 +01:00
|
|
|
closer_peers: vec![KadPeer {
|
|
|
|
node_id: PeerId::random(),
|
|
|
|
multiaddrs: vec!["/ip4/100.101.102.103/tcp/20105".parse().unwrap()],
|
|
|
|
connection_ty: KadConnectionType::Connected,
|
|
|
|
}],
|
2018-03-15 15:18:21 +01:00
|
|
|
});
|
2018-10-01 18:40:35 +02:00
|
|
|
test_one(KadMsg::GetProvidersReq {
|
|
|
|
key: encode(Hash::SHA2256, &[9, 12, 0, 245, 245, 201, 28, 95]).unwrap(),
|
|
|
|
});
|
|
|
|
test_one(KadMsg::GetProvidersRes {
|
2018-11-29 12:11:35 +01:00
|
|
|
closer_peers: vec![KadPeer {
|
|
|
|
node_id: PeerId::random(),
|
|
|
|
multiaddrs: vec!["/ip4/100.101.102.103/tcp/20105".parse().unwrap()],
|
|
|
|
connection_ty: KadConnectionType::Connected,
|
|
|
|
}],
|
|
|
|
provider_peers: vec![KadPeer {
|
|
|
|
node_id: PeerId::random(),
|
|
|
|
multiaddrs: vec!["/ip4/200.201.202.203/tcp/1999".parse().unwrap()],
|
|
|
|
connection_ty: KadConnectionType::NotConnected,
|
|
|
|
}],
|
2018-10-01 18:40:35 +02:00
|
|
|
});
|
|
|
|
test_one(KadMsg::AddProvider {
|
|
|
|
key: encode(Hash::SHA2256, &[9, 12, 0, 245, 245, 201, 28, 95]).unwrap(),
|
|
|
|
provider_peer: KadPeer {
|
2018-11-20 13:44:36 +01:00
|
|
|
node_id: PeerId::random(),
|
2018-10-01 18:40:35 +02:00
|
|
|
multiaddrs: vec!["/ip4/9.1.2.3/udp/23".parse().unwrap()],
|
|
|
|
connection_ty: KadConnectionType::Connected,
|
|
|
|
},
|
|
|
|
});
|
2018-03-15 15:18:21 +01:00
|
|
|
// TODO: all messages
|
|
|
|
|
|
|
|
fn test_one(msg_server: KadMsg) {
|
|
|
|
let msg_client = msg_server.clone();
|
|
|
|
let (tx, rx) = mpsc::channel();
|
|
|
|
|
|
|
|
let bg_thread = thread::spawn(move || {
|
2018-07-16 12:15:27 +02:00
|
|
|
let transport = TcpConfig::new().with_upgrade(KademliaProtocolConfig);
|
2018-03-15 15:18:21 +01:00
|
|
|
|
|
|
|
let (listener, addr) = transport
|
|
|
|
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
|
|
|
|
.unwrap();
|
|
|
|
tx.send(addr).unwrap();
|
|
|
|
|
|
|
|
let future = listener
|
|
|
|
.into_future()
|
|
|
|
.map_err(|(err, _)| err)
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|(client, _)| client.unwrap().0)
|
2018-03-15 15:18:21 +01:00
|
|
|
.and_then(|proto| proto.into_future().map_err(|(err, _)| err).map(|(v, _)| v))
|
|
|
|
.map(|recv_msg| {
|
|
|
|
assert_eq!(recv_msg.unwrap(), msg_server);
|
|
|
|
()
|
|
|
|
});
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-03-15 15:18:21 +01:00
|
|
|
});
|
|
|
|
|
2018-07-16 12:15:27 +02:00
|
|
|
let transport = TcpConfig::new().with_upgrade(KademliaProtocolConfig);
|
2018-03-15 15:18:21 +01:00
|
|
|
|
|
|
|
let future = transport
|
|
|
|
.dial(rx.recv().unwrap())
|
2019-01-10 11:27:06 +01:00
|
|
|
.unwrap()
|
2018-10-17 10:17:40 +01:00
|
|
|
.and_then(|proto| proto.send(msg_client))
|
2018-03-15 15:18:21 +01:00
|
|
|
.map(|_| ());
|
2018-10-25 05:26:37 -04:00
|
|
|
let mut rt = Runtime::new().unwrap();
|
|
|
|
let _ = rt.block_on(future).unwrap();
|
2018-03-15 15:18:21 +01:00
|
|
|
bg_thread.join().unwrap();
|
|
|
|
}
|
2018-11-29 12:11:35 +01:00
|
|
|
}*/
|
2018-03-15 15:18:21 +01:00
|
|
|
}
|