mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-12 09:31:20 +00:00
Clean up directory structure (#426)
* Remove unused circular-buffer crate * Move transports into subdirectory * Move misc into subdirectory * Move stores into subdirectory * Move multiplexers * Move protocols * Move libp2p top layer * Fix Test: skip doctest if secio isn't enabled
This commit is contained in:
committed by
GitHub
parent
f5ce93c730
commit
2ea49718f3
21
protocols/floodsub/Cargo.toml
Normal file
21
protocols/floodsub/Cargo.toml
Normal file
@ -0,0 +1,21 @@
|
||||
[package]
|
||||
name = "libp2p-floodsub"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
bs58 = "0.2.0"
|
||||
byteorder = "1.2.1"
|
||||
bytes = "0.4"
|
||||
fnv = "1.0"
|
||||
futures = "0.1"
|
||||
libp2p-core = { path = "../../core" }
|
||||
log = "0.4.1"
|
||||
multiaddr = { path = "../../misc/multiaddr" }
|
||||
parking_lot = "0.6"
|
||||
protobuf = "2.0.2"
|
||||
smallvec = "0.6.0"
|
||||
tokio-codec = "0.1"
|
||||
tokio-io = "0.1"
|
||||
unsigned-varint = { version = "0.1", features = ["codec"] }
|
13
protocols/floodsub/regen_structs_proto.sh
Executable file
13
protocols/floodsub/regen_structs_proto.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/rpc_proto.rs` file from `rpc.proto`.
|
||||
|
||||
docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 2.0.2 protobuf-codegen; \
|
||||
protoc --rust_out . rpc.proto"
|
||||
|
||||
sudo chown $USER:$USER *.rs
|
||||
|
||||
mv -f rpc.rs ./src/rpc_proto.rs
|
47
protocols/floodsub/rpc.proto
Normal file
47
protocols/floodsub/rpc.proto
Normal file
@ -0,0 +1,47 @@
|
||||
package floodsub.pb;
|
||||
|
||||
message RPC {
|
||||
repeated SubOpts subscriptions = 1;
|
||||
repeated Message publish = 2;
|
||||
|
||||
message SubOpts {
|
||||
optional bool subscribe = 1; // subscribe or unsubcribe
|
||||
optional string topicid = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message Message {
|
||||
optional bytes from = 1;
|
||||
optional bytes data = 2;
|
||||
optional bytes seqno = 3;
|
||||
repeated string topicIDs = 4;
|
||||
}
|
||||
|
||||
// topicID = hash(topicDescriptor); (not the topic.name)
|
||||
message TopicDescriptor {
|
||||
optional string name = 1;
|
||||
optional AuthOpts auth = 2;
|
||||
optional EncOpts enc = 3;
|
||||
|
||||
message AuthOpts {
|
||||
optional AuthMode mode = 1;
|
||||
repeated bytes keys = 2; // root keys to trust
|
||||
|
||||
enum AuthMode {
|
||||
NONE = 0; // no authentication, anyone can publish
|
||||
KEY = 1; // only messages signed by keys in the topic descriptor are accepted
|
||||
WOT = 2; // web of trust, certificates can allow publisher set to grow
|
||||
}
|
||||
}
|
||||
|
||||
message EncOpts {
|
||||
optional EncMode mode = 1;
|
||||
repeated bytes keyHashes = 2; // the hashes of the shared keys used (salted)
|
||||
|
||||
enum EncMode {
|
||||
NONE = 0; // no encryption, anyone can read
|
||||
SHAREDKEY = 1; // messages are encrypted with shared key
|
||||
WOT = 2; // web of trust, certificates can allow publisher set to grow
|
||||
}
|
||||
}
|
||||
}
|
661
protocols/floodsub/src/lib.rs
Normal file
661
protocols/floodsub/src/lib.rs
Normal file
@ -0,0 +1,661 @@
|
||||
// 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.
|
||||
|
||||
extern crate bs58;
|
||||
extern crate byteorder;
|
||||
extern crate bytes;
|
||||
extern crate fnv;
|
||||
extern crate futures;
|
||||
extern crate libp2p_core;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate multiaddr;
|
||||
extern crate parking_lot;
|
||||
extern crate protobuf;
|
||||
extern crate smallvec;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
extern crate unsigned_varint;
|
||||
|
||||
mod rpc_proto;
|
||||
mod topic;
|
||||
|
||||
pub use self::topic::{Topic, TopicBuilder, TopicHash};
|
||||
|
||||
use byteorder::{BigEndian, WriteBytesExt};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use fnv::{FnvHashMap, FnvHashSet, FnvHasher};
|
||||
use futures::sync::mpsc;
|
||||
use futures::{future, Future, Poll, Sink, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PeerId};
|
||||
use log::Level;
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use parking_lot::{Mutex, RwLock, RwLockUpgradableReadGuard};
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
use smallvec::SmallVec;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::iter;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use tokio_codec::Framed;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use unsigned_varint::codec;
|
||||
|
||||
/// Implementation of the `ConnectionUpgrade` for the floodsub protocol.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FloodSubUpgrade {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
impl FloodSubUpgrade {
|
||||
/// Builds a new `FloodSubUpgrade`. Also returns a `FloodSubReceiver` that will stream incoming
|
||||
/// messages for the floodsub system.
|
||||
pub fn new(my_id: PeerId) -> (FloodSubUpgrade, FloodSubReceiver) {
|
||||
let (output_tx, output_rx) = mpsc::unbounded();
|
||||
|
||||
let inner = Arc::new(Inner {
|
||||
peer_id: my_id.into_bytes(),
|
||||
output_tx: output_tx,
|
||||
remote_connections: RwLock::new(FnvHashMap::default()),
|
||||
subscribed_topics: RwLock::new(Vec::new()),
|
||||
seq_no: AtomicUsize::new(0),
|
||||
received: Mutex::new(FnvHashSet::default()),
|
||||
});
|
||||
|
||||
let upgrade = FloodSubUpgrade { inner: inner };
|
||||
|
||||
let receiver = FloodSubReceiver { inner: output_rx };
|
||||
|
||||
(upgrade, receiver)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, Maf> ConnectionUpgrade<C, Maf> for FloodSubUpgrade
|
||||
where
|
||||
C: AsyncRead + AsyncWrite + 'static,
|
||||
Maf: Future<Item = Multiaddr, Error = IoError> + 'static,
|
||||
{
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeIdentifier)>;
|
||||
type UpgradeIdentifier = ();
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/floodsub/1.0.0".into(), ()))
|
||||
}
|
||||
|
||||
type Output = FloodSubFuture;
|
||||
type MultiaddrFuture = future::FutureResult<Multiaddr, IoError>;
|
||||
type Future = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade(
|
||||
self,
|
||||
socket: C,
|
||||
_: Self::UpgradeIdentifier,
|
||||
_: Endpoint,
|
||||
remote_addr: Maf,
|
||||
) -> Self::Future {
|
||||
debug!("Upgrading connection as floodsub");
|
||||
|
||||
let future = remote_addr.and_then(move |remote_addr| {
|
||||
// Whenever a new node connects, we send to it a message containing the topics we are
|
||||
// already subscribed to.
|
||||
let init_msg: Vec<u8> = {
|
||||
let subscribed_topics = self.inner.subscribed_topics.read();
|
||||
let mut proto = rpc_proto::RPC::new();
|
||||
|
||||
for topic in subscribed_topics.iter() {
|
||||
let mut subscription = rpc_proto::RPC_SubOpts::new();
|
||||
subscription.set_subscribe(true);
|
||||
subscription.set_topicid(topic.hash().clone().into_string());
|
||||
proto.mut_subscriptions().push(subscription);
|
||||
}
|
||||
|
||||
proto
|
||||
.write_to_bytes()
|
||||
.expect("programmer error: the protobuf message should always be valid")
|
||||
};
|
||||
|
||||
// Split the socket into writing and reading parts.
|
||||
let (floodsub_sink, floodsub_stream) = Framed::new(socket, codec::UviBytes::default())
|
||||
.sink_map_err(|err| IoError::new(IoErrorKind::InvalidData, err))
|
||||
.map_err(|err| IoError::new(IoErrorKind::InvalidData, err))
|
||||
.split();
|
||||
|
||||
// Build the channel that will be used to communicate outgoing message to this remote.
|
||||
let (input_tx, input_rx) = mpsc::unbounded();
|
||||
input_tx
|
||||
.unbounded_send(init_msg.into())
|
||||
.expect("newly-created channel should always be open");
|
||||
self.inner.remote_connections.write().insert(
|
||||
remote_addr.clone(),
|
||||
RemoteInfo {
|
||||
sender: input_tx,
|
||||
subscribed_topics: RwLock::new(FnvHashSet::default()),
|
||||
},
|
||||
);
|
||||
|
||||
// Combine the socket read and the outgoing messages input, so that we can wake up when
|
||||
// either happens.
|
||||
let messages = input_rx
|
||||
.map(|m| (m, MessageSource::FromChannel))
|
||||
.map_err(|_| unreachable!("channel streams should never produce an error"))
|
||||
.select(floodsub_stream.map(|m| (m, MessageSource::FromSocket)));
|
||||
|
||||
#[derive(Debug)]
|
||||
enum MessageSource {
|
||||
FromSocket,
|
||||
FromChannel,
|
||||
}
|
||||
|
||||
let inner = self.inner.clone();
|
||||
let remote_addr_ret = future::ok(remote_addr.clone());
|
||||
let future = future::loop_fn(
|
||||
(floodsub_sink, messages),
|
||||
move |(floodsub_sink, messages)| {
|
||||
let inner = inner.clone();
|
||||
let remote_addr = remote_addr.clone();
|
||||
|
||||
messages
|
||||
.into_future()
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(move |(input, rest)| {
|
||||
match input {
|
||||
Some((bytes, MessageSource::FromSocket)) => {
|
||||
// Received a packet from the remote.
|
||||
let fut = match handle_packet_received(bytes, inner, &remote_addr) {
|
||||
Ok(()) => {
|
||||
future::ok(future::Loop::Continue((floodsub_sink, rest)))
|
||||
}
|
||||
Err(err) => future::err(err),
|
||||
};
|
||||
Box::new(fut) as Box<_>
|
||||
}
|
||||
|
||||
Some((bytes, MessageSource::FromChannel)) => {
|
||||
// Received a packet from the channel.
|
||||
// Need to send a message to remote.
|
||||
trace!("Effectively sending message to remote");
|
||||
let future = floodsub_sink.send(bytes).map(|floodsub_sink| {
|
||||
future::Loop::Continue((floodsub_sink, rest))
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
|
||||
None => {
|
||||
// Both the connection stream and `rx` are empty, so we break
|
||||
// the loop.
|
||||
trace!("Pubsub future clean finish");
|
||||
// TODO: what if multiple connections?
|
||||
inner.remote_connections.write().remove(&remote_addr);
|
||||
let future = future::ok(future::Loop::Break(()));
|
||||
Box::new(future) as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
future::ok((FloodSubFuture {
|
||||
inner: Box::new(future) as Box<_>,
|
||||
}, remote_addr_ret))
|
||||
});
|
||||
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows one to control the behaviour of the floodsub system.
|
||||
#[derive(Clone)]
|
||||
pub struct FloodSubController {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
// Our local peer ID multihash, to pass as the source.
|
||||
peer_id: Vec<u8>,
|
||||
|
||||
// Channel where to send the messages that should be dispatched to the user.
|
||||
output_tx: mpsc::UnboundedSender<Message>,
|
||||
|
||||
// Active connections with a remote.
|
||||
remote_connections: RwLock<FnvHashMap<Multiaddr, RemoteInfo>>,
|
||||
|
||||
// List of topics we're subscribed to. Necessary in order to filter out messages that we
|
||||
// erroneously receive.
|
||||
subscribed_topics: RwLock<Vec<Topic>>,
|
||||
|
||||
// Sequence number for the messages we send.
|
||||
seq_no: AtomicUsize,
|
||||
|
||||
// We keep track of the messages we received (in the format `(remote ID, seq_no)`) so that we
|
||||
// don't dispatch the same message twice if we receive it twice on the network.
|
||||
// TODO: the `HashSet` will keep growing indefinitely :-/
|
||||
received: Mutex<FnvHashSet<u64>>,
|
||||
}
|
||||
|
||||
struct RemoteInfo {
|
||||
// Sender to send data over the socket to that host.
|
||||
sender: mpsc::UnboundedSender<BytesMut>,
|
||||
// Topics the remote is registered to.
|
||||
subscribed_topics: RwLock<FnvHashSet<TopicHash>>,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Inner {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.debug_struct("Inner")
|
||||
.field("peer_id", &self.peer_id)
|
||||
.field(
|
||||
"num_remote_connections",
|
||||
&self.remote_connections.read().len(),
|
||||
)
|
||||
.field("subscribed_topics", &*self.subscribed_topics.read())
|
||||
.field("seq_no", &self.seq_no)
|
||||
.field("received", &self.received)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl FloodSubController {
|
||||
/// Builds a new controller for floodsub.
|
||||
#[inline]
|
||||
pub fn new(upgrade: &FloodSubUpgrade) -> Self {
|
||||
FloodSubController {
|
||||
inner: upgrade.inner.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Subscribe to a topic. When a node on the network sends a message for that topic, we will
|
||||
/// likely receive it.
|
||||
///
|
||||
/// It is not guaranteed that we receive every single message published on the network.
|
||||
#[inline]
|
||||
pub fn subscribe(&self, topic: &Topic) {
|
||||
// This function exists for convenience.
|
||||
self.subscribe_many(iter::once(topic));
|
||||
}
|
||||
|
||||
/// Same as `subscribe`, but subscribes to multiple topics at once.
|
||||
///
|
||||
/// Since this results in a single packet sent to the remotes, it is preferable to use this
|
||||
/// method when subscribing to multiple topics at once rather than call `subscribe` multiple
|
||||
/// times.
|
||||
#[inline]
|
||||
pub fn subscribe_many<'a, I>(&self, topics: I)
|
||||
where
|
||||
I: IntoIterator<Item = &'a Topic>,
|
||||
I::IntoIter: Clone,
|
||||
{
|
||||
// This function exists for convenience.
|
||||
self.sub_unsub_multi(topics.into_iter().map::<_, fn(_) -> _>(|t| (t, true)))
|
||||
}
|
||||
|
||||
/// Unsubscribe from a topic. We will no longer receive any message for this topic.
|
||||
///
|
||||
/// If a message was sent to us before we are able to notify that we don't want messages
|
||||
/// anymore, then the message will be filtered out locally.
|
||||
#[inline]
|
||||
pub fn unsubscribe(&self, topic: &Topic) {
|
||||
// This function exists for convenience.
|
||||
self.unsubscribe_many(iter::once(topic));
|
||||
}
|
||||
|
||||
/// Same as `unsubscribe` but unsubscribes from multiple topics at once.
|
||||
///
|
||||
/// Since this results in a single packet sent to the remotes, it is preferable to use this
|
||||
/// method when ybsubscribing from multiple topics at once rather than call `unsubscribe`
|
||||
/// multiple times.
|
||||
#[inline]
|
||||
pub fn unsubscribe_many<'a, I>(&self, topics: I)
|
||||
where
|
||||
I: IntoIterator<Item = &'a Topic>,
|
||||
I::IntoIter: Clone,
|
||||
{
|
||||
// This function exists for convenience.
|
||||
self.sub_unsub_multi(topics.into_iter().map::<_, fn(_) -> _>(|t| (t, false)));
|
||||
}
|
||||
|
||||
// Inner implementation. The iterator should produce a boolean that is true if we subscribe and
|
||||
// false if we unsubscribe.
|
||||
fn sub_unsub_multi<'a, I>(&self, topics: I)
|
||||
where
|
||||
I: IntoIterator<Item = (&'a Topic, bool)>,
|
||||
I::IntoIter: Clone,
|
||||
{
|
||||
let mut proto = rpc_proto::RPC::new();
|
||||
|
||||
let topics = topics.into_iter();
|
||||
|
||||
if log_enabled!(Level::Debug) {
|
||||
debug!("Queuing sub/unsub message ; sub = {:?} ; unsub = {:?}",
|
||||
topics.clone().filter(|t| t.1)
|
||||
.map(|t| t.0.hash().clone().into_string())
|
||||
.collect::<Vec<_>>(),
|
||||
topics.clone().filter(|t| !t.1)
|
||||
.map(|t| t.0.hash().clone().into_string())
|
||||
.collect::<Vec<_>>());
|
||||
}
|
||||
|
||||
let mut subscribed_topics = self.inner.subscribed_topics.write();
|
||||
for (topic, subscribe) in topics {
|
||||
let mut subscription = rpc_proto::RPC_SubOpts::new();
|
||||
subscription.set_subscribe(subscribe);
|
||||
subscription.set_topicid(topic.hash().clone().into_string());
|
||||
proto.mut_subscriptions().push(subscription);
|
||||
|
||||
if subscribe {
|
||||
subscribed_topics.push(topic.clone());
|
||||
} else {
|
||||
subscribed_topics.retain(|t| t.hash() != topic.hash())
|
||||
}
|
||||
}
|
||||
|
||||
self.broadcast(proto, |_| true);
|
||||
}
|
||||
|
||||
/// Publishes a message on the network for the specified topic
|
||||
#[inline]
|
||||
pub fn publish(&self, topic: &Topic, data: Vec<u8>) {
|
||||
// This function exists for convenience.
|
||||
self.publish_many(iter::once(topic), data)
|
||||
}
|
||||
|
||||
/// Publishes a message on the network for the specified topics.
|
||||
///
|
||||
/// Since this results in a single packet sent to the remotes, it is preferable to use this
|
||||
/// method when publishing multiple messages at once rather than call `publish` multiple
|
||||
/// times.
|
||||
pub fn publish_many<'a, I>(&self, topics: I, data: Vec<u8>)
|
||||
where
|
||||
I: IntoIterator<Item = &'a Topic>,
|
||||
{
|
||||
let topics = topics.into_iter().collect::<Vec<_>>();
|
||||
|
||||
debug!("Queueing publish message ; topics = {:?} ; data_len = {:?}",
|
||||
topics.iter().map(|t| t.hash().clone().into_string()).collect::<Vec<_>>(),
|
||||
data.len());
|
||||
|
||||
// Build the `Vec<u8>` containing our sequence number for this message.
|
||||
let seq_no_bytes = {
|
||||
let mut seqno_bytes = Vec::new();
|
||||
let seqn = self.inner.seq_no.fetch_add(1, Ordering::Relaxed);
|
||||
seqno_bytes
|
||||
.write_u64::<BigEndian>(seqn as u64)
|
||||
.expect("writing to a Vec never fails");
|
||||
seqno_bytes
|
||||
};
|
||||
|
||||
// TODO: should handle encryption/authentication of the message
|
||||
|
||||
let mut msg = rpc_proto::Message::new();
|
||||
msg.set_data(data);
|
||||
msg.set_from(self.inner.peer_id.clone());
|
||||
msg.set_seqno(seq_no_bytes.clone());
|
||||
msg.set_topicIDs(
|
||||
topics
|
||||
.iter()
|
||||
.map(|t| t.hash().clone().into_string())
|
||||
.collect(),
|
||||
);
|
||||
|
||||
let mut proto = rpc_proto::RPC::new();
|
||||
proto.mut_publish().push(msg);
|
||||
|
||||
// Insert into `received` so that we ignore the message if a remote sends it back to us.
|
||||
self.inner
|
||||
.received
|
||||
.lock()
|
||||
.insert(hash((self.inner.peer_id.clone(), seq_no_bytes)));
|
||||
|
||||
self.broadcast(proto, |r_top| {
|
||||
topics.iter().any(|t| r_top.iter().any(|to| to == t.hash()))
|
||||
});
|
||||
}
|
||||
|
||||
// Internal function that dispatches an `RPC` protobuf struct to all the connected remotes
|
||||
// for which `filter` returns true.
|
||||
fn broadcast<F>(&self, message: rpc_proto::RPC, mut filter: F)
|
||||
where
|
||||
F: FnMut(&FnvHashSet<TopicHash>) -> bool,
|
||||
{
|
||||
let bytes = message
|
||||
.write_to_bytes()
|
||||
.expect("protobuf message is always valid");
|
||||
|
||||
let remote_connections = self.inner.remote_connections.upgradable_read();
|
||||
|
||||
// Number of remotes we dispatched to, for logging purposes.
|
||||
let mut num_dispatched = 0;
|
||||
// Will store the addresses of remotes which we failed to send a message to and which
|
||||
// must be removed from the active connections.
|
||||
// We use a smallvec of 6 elements because it is unlikely that we lost connection to more
|
||||
// than 6 elements at once.
|
||||
let mut failed_to_send: SmallVec<[_; 6]> = SmallVec::new();
|
||||
for (remote_addr, remote) in remote_connections.iter() {
|
||||
if !filter(&remote.subscribed_topics.read()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
num_dispatched += 1;
|
||||
match remote.sender.unbounded_send(bytes.clone().into()) {
|
||||
Ok(_) => (),
|
||||
Err(_) => {
|
||||
trace!("Failed to dispatch message to {} because channel was closed",
|
||||
remote_addr);
|
||||
failed_to_send.push(remote_addr.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the remotes which we failed to send a message to.
|
||||
if !failed_to_send.is_empty() {
|
||||
// If we fail to upgrade the read lock to a write lock, just ignore `failed_to_send`.
|
||||
if let Ok(mut remote_connections) = RwLockUpgradableReadGuard::try_upgrade(remote_connections) {
|
||||
for failed_to_send in failed_to_send {
|
||||
remote_connections.remove(&failed_to_send);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
debug!("Message queued for {} remotes", num_dispatched);
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation of `Stream` that provides messages for the subscribed topics you subscribed to.
|
||||
pub struct FloodSubReceiver {
|
||||
inner: mpsc::UnboundedReceiver<Message>,
|
||||
}
|
||||
|
||||
impl Stream for FloodSubReceiver {
|
||||
type Item = Message;
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
self.inner
|
||||
.poll()
|
||||
.map_err(|_| unreachable!("UnboundedReceiver cannot err"))
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for FloodSubReceiver {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.debug_struct("FloodSubReceiver").finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// A message received by the floodsub system.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Message {
|
||||
/// Remote that sent the message.
|
||||
pub source: Multiaddr,
|
||||
|
||||
/// Content of the message. Its meaning is out of scope of this library.
|
||||
pub data: Vec<u8>,
|
||||
|
||||
/// List of topics of this message.
|
||||
///
|
||||
/// Each message can belong to multiple topics at once.
|
||||
pub topics: Vec<TopicHash>,
|
||||
}
|
||||
|
||||
/// Implementation of `Future` that must be driven to completion in order for floodsub to work.
|
||||
pub struct FloodSubFuture {
|
||||
inner: Box<Future<Item = (), Error = IoError>>,
|
||||
}
|
||||
|
||||
impl Future for FloodSubFuture {
|
||||
type Item = ();
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
self.inner.poll()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for FloodSubFuture {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.debug_struct("FloodSubFuture").finish()
|
||||
}
|
||||
}
|
||||
|
||||
// Handles when a packet is received on a connection.
|
||||
//
|
||||
// - `bytes` contains the raw data.
|
||||
// - `remote_addr` is the address of the sender.
|
||||
fn handle_packet_received(
|
||||
bytes: BytesMut,
|
||||
inner: Arc<Inner>,
|
||||
remote_addr: &Multiaddr,
|
||||
) -> Result<(), IoError> {
|
||||
trace!("Received packet from {}", remote_addr);
|
||||
|
||||
// Parsing attempt.
|
||||
let mut input = match protobuf::parse_from_bytes::<rpc_proto::RPC>(&bytes) {
|
||||
Ok(msg) => msg,
|
||||
Err(err) => {
|
||||
debug!("Failed to parse protobuf message ; err = {:?}", err);
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
// Update the topics the remote is subscribed to.
|
||||
if !input.get_subscriptions().is_empty() {
|
||||
let remote_connec = inner.remote_connections.write();
|
||||
// TODO: what if multiple entries?
|
||||
let remote = &remote_connec[remote_addr];
|
||||
let mut topics = remote.subscribed_topics.write();
|
||||
for subscription in input.mut_subscriptions().iter_mut() {
|
||||
let topic = TopicHash::from_raw(subscription.take_topicid());
|
||||
let subscribe = subscription.get_subscribe();
|
||||
if subscribe {
|
||||
trace!("Remote {} subscribed to {:?}", remote_addr, topic); topics.insert(topic);
|
||||
} else {
|
||||
trace!("Remote {} unsubscribed from {:?}", remote_addr, topic);
|
||||
topics.remove(&topic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle the messages coming from the remote.
|
||||
for publish in input.mut_publish().iter_mut() {
|
||||
let from = publish.take_from();
|
||||
// We maintain a list of the messages that have already been
|
||||
// processed so that we don't process the same message twice.
|
||||
// Each message is identified by the `(from, seqno)` tuple.
|
||||
if !inner
|
||||
.received
|
||||
.lock()
|
||||
.insert(hash((from.clone(), publish.take_seqno())))
|
||||
{
|
||||
trace!("Skipping message because we had already received it ; payload = {} bytes",
|
||||
publish.get_data().len());
|
||||
continue;
|
||||
}
|
||||
|
||||
let peer_id = match PeerId::from_bytes(bytes.to_vec()) {
|
||||
Ok(id) => id,
|
||||
Err(err) => {
|
||||
trace!("Parsing PeerId failed: {:?}. Skipping.", err);
|
||||
continue
|
||||
}
|
||||
};
|
||||
|
||||
let from: Multiaddr = AddrComponent::P2P(peer_id.into()).into();
|
||||
|
||||
let topics = publish
|
||||
.take_topicIDs()
|
||||
.into_iter()
|
||||
.map(|h| TopicHash::from_raw(h))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
trace!("Processing message for topics {:?} ; payload = {} bytes",
|
||||
topics,
|
||||
publish.get_data().len());
|
||||
|
||||
// TODO: should check encryption/authentication of the message
|
||||
|
||||
// Broadcast the message to all the other remotes.
|
||||
{
|
||||
let remote_connections = inner.remote_connections.read();
|
||||
for (addr, info) in remote_connections.iter() {
|
||||
let st = info.subscribed_topics.read();
|
||||
if !topics.iter().any(|t| st.contains(t)) {
|
||||
continue;
|
||||
}
|
||||
// TODO: don't send back to the remote that just sent it
|
||||
trace!("Broadcasting received message to {}", addr);
|
||||
let _ = info.sender.unbounded_send(bytes.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// Send the message locally if relevant.
|
||||
let dispatch_locally = {
|
||||
let subscribed_topics = inner.subscribed_topics.read();
|
||||
topics
|
||||
.iter()
|
||||
.any(|t| subscribed_topics.iter().any(|topic| topic.hash() == t))
|
||||
};
|
||||
if dispatch_locally {
|
||||
// Ignore if channel is closed.
|
||||
trace!("Dispatching message locally");
|
||||
let _ = inner.output_tx.unbounded_send(Message {
|
||||
source: from,
|
||||
data: publish.take_data(),
|
||||
topics: topics,
|
||||
});
|
||||
} else {
|
||||
trace!("Message not dispatched locally as we are not subscribed to any of the topics");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Shortcut function that hashes a value.
|
||||
#[inline]
|
||||
fn hash<V: Hash>(value: V) -> u64 {
|
||||
let mut h = FnvHasher::default();
|
||||
value.hash(&mut h);
|
||||
h.finish()
|
||||
}
|
1683
protocols/floodsub/src/rpc_proto.rs
Normal file
1683
protocols/floodsub/src/rpc_proto.rs
Normal file
File diff suppressed because it is too large
Load Diff
92
protocols/floodsub/src/topic.rs
Normal file
92
protocols/floodsub/src/topic.rs
Normal file
@ -0,0 +1,92 @@
|
||||
// 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.
|
||||
|
||||
use bs58;
|
||||
use protobuf::Message;
|
||||
use rpc_proto;
|
||||
|
||||
/// Represents the hash of a topic.
|
||||
///
|
||||
/// Instead of a using the topic as a whole, the API of floodsub uses a hash of the topic. You only
|
||||
/// have to build the hash once, then use it everywhere.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct TopicHash {
|
||||
hash: String,
|
||||
}
|
||||
|
||||
impl TopicHash {
|
||||
/// Builds a new `TopicHash` from the given hash.
|
||||
#[inline]
|
||||
pub fn from_raw(hash: String) -> TopicHash {
|
||||
TopicHash { hash: hash }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_string(self) -> String {
|
||||
self.hash
|
||||
}
|
||||
}
|
||||
|
||||
/// Built topic.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Topic {
|
||||
descriptor: rpc_proto::TopicDescriptor,
|
||||
hash: TopicHash,
|
||||
}
|
||||
|
||||
impl Topic {
|
||||
/// Returns the hash of the topic.
|
||||
#[inline]
|
||||
pub fn hash(&self) -> &TopicHash {
|
||||
&self.hash
|
||||
}
|
||||
}
|
||||
|
||||
/// Builder for a `TopicHash`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TopicBuilder {
|
||||
builder: rpc_proto::TopicDescriptor,
|
||||
}
|
||||
|
||||
impl TopicBuilder {
|
||||
pub fn new<S>(name: S) -> TopicBuilder
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
let mut builder = rpc_proto::TopicDescriptor::new();
|
||||
builder.set_name(name.into());
|
||||
|
||||
TopicBuilder { builder: builder }
|
||||
}
|
||||
|
||||
/// Turns the builder into an actual `Topic`.
|
||||
pub fn build(self) -> Topic {
|
||||
let bytes = self.builder
|
||||
.write_to_bytes()
|
||||
.expect("protobuf message is always valid");
|
||||
let hash = TopicHash {
|
||||
hash: bs58::encode(&bytes).into_string(),
|
||||
};
|
||||
Topic {
|
||||
descriptor: self.builder,
|
||||
hash: hash,
|
||||
}
|
||||
}
|
||||
}
|
23
protocols/identify/Cargo.toml
Normal file
23
protocols/identify/Cargo.toml
Normal file
@ -0,0 +1,23 @@
|
||||
[package]
|
||||
name = "libp2p-identify"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
bytes = "0.4"
|
||||
fnv = "1"
|
||||
futures = "0.1"
|
||||
libp2p-peerstore = { path = "../../stores/peerstore" }
|
||||
libp2p-core = { path = "../../core" }
|
||||
log = "0.4.1"
|
||||
multiaddr = { path = "../../misc/multiaddr" }
|
||||
parking_lot = "0.6"
|
||||
protobuf = "2.0.2"
|
||||
tokio-codec = "0.1"
|
||||
tokio-io = "0.1.0"
|
||||
unsigned-varint = { version = "0.1", features = ["codec"] }
|
||||
|
||||
[dev-dependencies]
|
||||
libp2p-tcp-transport = { path = "../../transports/tcp" }
|
||||
tokio-current-thread = "0.1"
|
13
protocols/identify/regen_structs_proto.sh
Executable file
13
protocols/identify/regen_structs_proto.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||
|
||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 2.0.2 protobuf-codegen; \
|
||||
protoc --rust_out . structs.proto"
|
||||
|
||||
sudo chown $USER:$USER *.rs
|
||||
|
||||
mv -f structs.rs ./src/structs_proto.rs
|
323
protocols/identify/src/identify_transport.rs
Normal file
323
protocols/identify/src/identify_transport.rs
Normal file
@ -0,0 +1,323 @@
|
||||
// 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.
|
||||
|
||||
use fnv::FnvHashMap;
|
||||
use futures::{future, Future, Stream};
|
||||
use libp2p_core::{Multiaddr, MuxedTransport, Transport};
|
||||
use parking_lot::Mutex;
|
||||
use protocol::{IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::error::Error;
|
||||
use std::io::Error as IoError;
|
||||
use std::sync::Arc;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Implementation of `Transport`. See [the crate root description](index.html).
|
||||
pub struct IdentifyTransport<Trans> {
|
||||
transport: Trans,
|
||||
// Each entry is protected by an asynchronous mutex, so that if we dial the same node twice
|
||||
// simultaneously, the second time will block until the first time has identified it.
|
||||
cache: Arc<Mutex<FnvHashMap<Multiaddr, CacheEntry>>>,
|
||||
}
|
||||
|
||||
impl<Trans> Clone for IdentifyTransport<Trans>
|
||||
where Trans: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
IdentifyTransport {
|
||||
transport: self.transport.clone(),
|
||||
cache: self.cache.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type CacheEntry = future::Shared<Box<Future<Item = IdentifyTransportOutcome, Error = IoError>>>;
|
||||
|
||||
impl<Trans> IdentifyTransport<Trans> {
|
||||
/// Creates an `IdentifyTransport` that wraps around the given transport and peerstore.
|
||||
#[inline]
|
||||
pub fn new(transport: Trans) -> Self {
|
||||
IdentifyTransport {
|
||||
transport,
|
||||
cache: Arc::new(Mutex::new(Default::default())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Trans> Transport for IdentifyTransport<Trans>
|
||||
where
|
||||
Trans: Transport + Clone + 'static, // TODO: 'static :(
|
||||
Trans::Output: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Output = IdentifyTransportOutput<Trans::Output>;
|
||||
type MultiaddrFuture = future::FutureResult<Multiaddr, IoError>;
|
||||
type Listener = Box<Stream<Item = Self::ListenerUpgrade, Error = IoError>>;
|
||||
type ListenerUpgrade = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
type Dial = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
||||
let (listener, new_addr) = match self.transport.clone().listen_on(addr.clone()) {
|
||||
Ok((l, a)) => (l, a),
|
||||
Err((inner, addr)) => {
|
||||
let id = IdentifyTransport {
|
||||
transport: inner,
|
||||
cache: self.cache,
|
||||
};
|
||||
return Err((id, addr));
|
||||
}
|
||||
};
|
||||
|
||||
let identify_upgrade = self.transport.with_upgrade(IdentifyProtocolConfig);
|
||||
let cache = self.cache.clone();
|
||||
|
||||
let listener = listener.map(move |connec| {
|
||||
let identify_upgrade = identify_upgrade.clone();
|
||||
let cache = cache.clone();
|
||||
let fut = connec
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
trace!("Incoming connection, waiting for client address");
|
||||
client_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
debug!("Incoming connection from {}", client_addr);
|
||||
|
||||
// Dial the address that connected to us and try upgrade with the
|
||||
// identify protocol.
|
||||
let info_future = cache_entry(&cache, client_addr.clone(), { let client_addr = client_addr.clone(); move || {
|
||||
debug!("No cache entry for {}, dialing back in order to identify", client_addr);
|
||||
future::lazy(|| { trace!("Starting identify back"); identify_upgrade
|
||||
.dial(client_addr)
|
||||
.unwrap_or_else(|(_, addr)| {
|
||||
panic!("the multiaddr {} was determined to be valid earlier", addr)
|
||||
}) })
|
||||
.map(move |(identify, _)| {
|
||||
let (info, observed_addr) = match identify {
|
||||
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
||||
(info, observed_addr)
|
||||
},
|
||||
_ => unreachable!(
|
||||
"the identify protocol guarantees that we receive \
|
||||
remote information when we dial a node"
|
||||
),
|
||||
};
|
||||
|
||||
debug!("Identified dialed back connection as pubkey {:?}", info.public_key);
|
||||
IdentifyTransportOutcome {
|
||||
info,
|
||||
observed_addr,
|
||||
}
|
||||
})
|
||||
.map_err(move |err| {
|
||||
debug!("Failed to identify dialed back connection");
|
||||
err
|
||||
})
|
||||
}});
|
||||
|
||||
let out = IdentifyTransportOutput {
|
||||
socket: connec,
|
||||
info: Box::new(info_future),
|
||||
};
|
||||
|
||||
Ok((out, future::ok(client_addr)))
|
||||
});
|
||||
|
||||
Box::new(fut) as Box<Future<Item = _, Error = _>>
|
||||
});
|
||||
|
||||
Ok((Box::new(listener) as Box<_>, new_addr))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
|
||||
// We dial a first time the node.
|
||||
let dial = match self.transport.clone().dial(addr) {
|
||||
Ok(d) => d,
|
||||
Err((transport, addr)) => {
|
||||
let id = IdentifyTransport {
|
||||
transport,
|
||||
cache: self.cache,
|
||||
};
|
||||
return Err((id, addr));
|
||||
}
|
||||
};
|
||||
|
||||
// Once successfully dialed, we dial again to identify.
|
||||
let identify_upgrade = self.transport.with_upgrade(IdentifyProtocolConfig);
|
||||
let cache = self.cache.clone();
|
||||
let future = dial
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
trace!("Dialing successful, waiting for client address");
|
||||
client_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.and_then(move |(socket, addr)| {
|
||||
trace!("Dialing successful ; client address is {}", addr);
|
||||
let info_future = cache_entry(&cache, addr.clone(), { let addr = addr.clone(); move || {
|
||||
trace!("No cache entry for {} ; dialing again for identification", addr);
|
||||
future::lazy(|| { trace!("Starting identify back"); identify_upgrade
|
||||
.dial(addr)
|
||||
.unwrap_or_else(|(_, addr)| {
|
||||
panic!("the multiaddr {} was determined to be valid earlier", addr)
|
||||
}) })
|
||||
.map(move |(identify, _)| {
|
||||
let (info, observed_addr) = match identify {
|
||||
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
||||
(info, observed_addr)
|
||||
}
|
||||
_ => unreachable!(
|
||||
"the identify protocol guarantees that we receive \
|
||||
remote information when we dial a node"
|
||||
),
|
||||
};
|
||||
|
||||
IdentifyTransportOutcome {
|
||||
info,
|
||||
observed_addr,
|
||||
}
|
||||
})
|
||||
}});
|
||||
|
||||
let out = IdentifyTransportOutput {
|
||||
socket: socket,
|
||||
info: Box::new(info_future),
|
||||
};
|
||||
|
||||
Ok((out, future::ok(addr)))
|
||||
});
|
||||
|
||||
Ok(Box::new(future) as Box<_>)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nat_traversal(&self, a: &Multiaddr, b: &Multiaddr) -> Option<Multiaddr> {
|
||||
self.transport.nat_traversal(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Trans> MuxedTransport for IdentifyTransport<Trans>
|
||||
where
|
||||
Trans: MuxedTransport + Clone + 'static,
|
||||
Trans::Output: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Incoming = Box<Future<Item = Self::IncomingUpgrade, Error = IoError>>;
|
||||
type IncomingUpgrade = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn next_incoming(self) -> Self::Incoming {
|
||||
let identify_upgrade = self.transport.clone().with_upgrade(IdentifyProtocolConfig);
|
||||
let cache = self.cache.clone();
|
||||
|
||||
let future = self.transport.next_incoming().map(move |incoming| {
|
||||
let cache = cache.clone();
|
||||
let future = incoming
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
debug!("Incoming substream ; waiting for client address");
|
||||
client_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
debug!("Incoming substream from {}", client_addr);
|
||||
|
||||
// Dial the address that connected to us and try upgrade with the
|
||||
// identify protocol.
|
||||
let info_future = cache_entry(&cache, client_addr.clone(), { let client_addr = client_addr.clone(); move || {
|
||||
debug!("No cache entry from {} ; dialing back to identify", client_addr);
|
||||
future::lazy(|| { trace!("Starting identify back"); identify_upgrade
|
||||
.dial(client_addr)
|
||||
.unwrap_or_else(|(_, client_addr)| {
|
||||
panic!("the multiaddr {} was determined to be valid earlier", client_addr)
|
||||
}) })
|
||||
.map(move |(identify, _)| {
|
||||
let (info, observed_addr) = match identify {
|
||||
IdentifyOutput::RemoteInfo { info, observed_addr } => {
|
||||
(info, observed_addr)
|
||||
},
|
||||
_ => unreachable!(
|
||||
"the identify protocol guarantees that we receive \
|
||||
remote information when we dial a node"
|
||||
),
|
||||
};
|
||||
|
||||
debug!("Identified incoming substream as pubkey {:?}", info.public_key);
|
||||
IdentifyTransportOutcome {
|
||||
info,
|
||||
observed_addr,
|
||||
}
|
||||
})
|
||||
.map_err(move |err| {
|
||||
debug!("Failed to identify incoming substream");
|
||||
err
|
||||
})
|
||||
}});
|
||||
|
||||
let out = IdentifyTransportOutput {
|
||||
socket: connec,
|
||||
info: Box::new(info_future),
|
||||
};
|
||||
|
||||
Ok((out, future::ok(client_addr)))
|
||||
});
|
||||
|
||||
Box::new(future) as Box<Future<Item = _, Error = _>>
|
||||
});
|
||||
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
|
||||
/// Output of the identify transport.
|
||||
pub struct IdentifyTransportOutput<S> {
|
||||
/// The socket to communicate with the remote.
|
||||
pub socket: S,
|
||||
/// Outcome of the identification of the remote.
|
||||
pub info: Box<Future<Item = IdentifyTransportOutcome, Error = IoError>>,
|
||||
}
|
||||
|
||||
/// Outcome of the identification of the remote.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IdentifyTransportOutcome {
|
||||
/// Identification of the remote.
|
||||
pub info: IdentifyInfo,
|
||||
/// Address the remote sees for us.
|
||||
pub observed_addr: Multiaddr,
|
||||
}
|
||||
|
||||
fn cache_entry<F, Fut>(cache: &Mutex<FnvHashMap<Multiaddr, CacheEntry>>, addr: Multiaddr, if_no_entry: F)
|
||||
-> impl Future<Item = IdentifyTransportOutcome, Error = IoError>
|
||||
where F: FnOnce() -> Fut,
|
||||
Fut: Future<Item = IdentifyTransportOutcome, Error = IoError> + 'static,
|
||||
{
|
||||
trace!("Looking up cache entry for {}", addr);
|
||||
let mut cache = cache.lock();
|
||||
match cache.entry(addr) {
|
||||
Entry::Occupied(entry) => {
|
||||
trace!("Cache entry found, cloning");
|
||||
future::Either::A(entry.get().clone())
|
||||
},
|
||||
|
||||
Entry::Vacant(entry) => {
|
||||
trace!("No cache entry available");
|
||||
let future = (Box::new(if_no_entry()) as Box<Future<Item = _, Error = _>>).shared();
|
||||
entry.insert(future.clone());
|
||||
future::Either::B(future)
|
||||
},
|
||||
}.map(|out| (*out).clone()).map_err(|err| IoError::new(err.kind(), err.description()))
|
||||
}
|
||||
|
||||
// TODO: test that we receive back what the remote sent us
|
90
protocols/identify/src/lib.rs
Normal file
90
protocols/identify/src/lib.rs
Normal file
@ -0,0 +1,90 @@
|
||||
// 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.
|
||||
|
||||
//! Implementation of the `/ipfs/id/1.0.0` protocol. Allows a node A to query another node B which
|
||||
//! information B knows about A. Also includes the addresses B is listening on.
|
||||
//!
|
||||
//! When two nodes connect to each other, the listening half sends a message to the dialing half,
|
||||
//! indicating the information, and then the protocol stops.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! Both low-level and high-level usages are available.
|
||||
//!
|
||||
//! ## High-level usage through the `IdentifyTransport` struct
|
||||
//!
|
||||
//! This crate provides the `IdentifyTransport` struct, which wraps around a `Transport` and an
|
||||
//! implementation of `Peerstore`. `IdentifyTransport` is itself a transport that accepts
|
||||
//! multiaddresses of the form `/p2p/...` or `/ipfs/...`.
|
||||
//!
|
||||
//! > **Note**: All the documentation refers to `/p2p/...`, however `/ipfs/...` is also supported.
|
||||
//!
|
||||
//! If you dial a multiaddr of the form `/p2p/...`, then the `IdentifyTransport` will look into
|
||||
//! the `Peerstore` for any known multiaddress for this peer and try to dial them using the
|
||||
//! underlying transport. If you dial any other multiaddr, then it will dial this multiaddr using
|
||||
//! the underlying transport, then negotiate the *identify* protocol with the remote in order to
|
||||
//! obtain its ID, then add it to the peerstore, and finally dial the same multiaddr again and
|
||||
//! return the connection.
|
||||
//!
|
||||
//! Listening doesn't support multiaddresses of the form `/p2p/...` (because that wouldn't make
|
||||
//! sense). Any address passed to `listen_on` will be passed directly to the underlying transport.
|
||||
//!
|
||||
//! Whenever a remote connects to us, either through listening or through `next_incoming`, the
|
||||
//! `IdentifyTransport` dials back the remote, upgrades the connection to the *identify* protocol
|
||||
//! in order to obtain the ID of the remote, stores the information in the peerstore, and finally
|
||||
//! only returns the connection. From the exterior, the multiaddress of the remote is of the form
|
||||
//! `/p2p/...`. If the remote doesn't support the *identify* protocol, then the socket is closed.
|
||||
//!
|
||||
//! Because of the behaviour of `IdentifyProtocol`, it is recommended to build it on top of a
|
||||
//! `ConnectionReuse`.
|
||||
//!
|
||||
//! ## Low-level usage through the `IdentifyProtocolConfig` struct
|
||||
//!
|
||||
//! The `IdentifyProtocolConfig` struct implements the `ConnectionUpgrade` trait. Using it will
|
||||
//! negotiate the *identify* protocol.
|
||||
//!
|
||||
//! The output of the upgrade is a `IdentifyOutput`. If we are the dialer, then `IdentifyOutput`
|
||||
//! will contain the information sent by the remote. If we are the listener, then it will contain
|
||||
//! a `IdentifySender` struct that can be used to transmit back to the remote the information about
|
||||
//! it.
|
||||
|
||||
extern crate bytes;
|
||||
extern crate fnv;
|
||||
extern crate futures;
|
||||
extern crate libp2p_peerstore;
|
||||
extern crate libp2p_core;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate multiaddr;
|
||||
extern crate parking_lot;
|
||||
extern crate protobuf;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
extern crate unsigned_varint;
|
||||
|
||||
pub use self::identify_transport::IdentifyTransportOutcome;
|
||||
pub use self::peer_id_transport::{PeerIdTransport, PeerIdTransportOutput};
|
||||
pub use self::protocol::{IdentifyInfo, IdentifyOutput};
|
||||
pub use self::protocol::{IdentifyProtocolConfig, IdentifySender};
|
||||
|
||||
mod identify_transport;
|
||||
mod peer_id_transport;
|
||||
mod protocol;
|
||||
mod structs_proto;
|
360
protocols/identify/src/peer_id_transport.rs
Normal file
360
protocols/identify/src/peer_id_transport.rs
Normal file
@ -0,0 +1,360 @@
|
||||
// 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.
|
||||
|
||||
use futures::{future, stream, Future, Stream};
|
||||
use identify_transport::{IdentifyTransport, IdentifyTransportOutcome};
|
||||
use libp2p_core::{PeerId, MuxedTransport, Transport};
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Implementation of `Transport`. See [the crate root description](index.html).
|
||||
#[derive(Clone)]
|
||||
pub struct PeerIdTransport<Trans, AddrRes> {
|
||||
transport: IdentifyTransport<Trans>,
|
||||
addr_resolver: AddrRes,
|
||||
}
|
||||
|
||||
impl<Trans, AddrRes> PeerIdTransport<Trans, AddrRes> {
|
||||
/// Creates an `PeerIdTransport` that wraps around the given transport and address resolver.
|
||||
#[inline]
|
||||
pub fn new(transport: Trans, addr_resolver: AddrRes) -> Self {
|
||||
PeerIdTransport {
|
||||
transport: IdentifyTransport::new(transport),
|
||||
addr_resolver,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Trans, AddrRes, AddrResOut> Transport for PeerIdTransport<Trans, AddrRes>
|
||||
where
|
||||
Trans: Transport + Clone + 'static, // TODO: 'static :(
|
||||
Trans::Output: AsyncRead + AsyncWrite,
|
||||
AddrRes: Fn(PeerId) -> AddrResOut + 'static, // TODO: 'static :(
|
||||
AddrResOut: IntoIterator<Item = Multiaddr> + 'static, // TODO: 'static :(
|
||||
{
|
||||
type Output = PeerIdTransportOutput<Trans::Output>;
|
||||
type MultiaddrFuture = Box<Future<Item = Multiaddr, Error = IoError>>;
|
||||
type Listener = Box<Stream<Item = Self::ListenerUpgrade, Error = IoError>>;
|
||||
type ListenerUpgrade = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
type Dial = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn listen_on(self, addr: Multiaddr) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
||||
// Note that `listen_on` expects a "regular" multiaddr (eg. `/ip/.../tcp/...`),
|
||||
// and not `/p2p/<foo>`.
|
||||
|
||||
let (listener, listened_addr) = match self.transport.listen_on(addr) {
|
||||
Ok((listener, addr)) => (listener, addr),
|
||||
Err((inner, addr)) => {
|
||||
let id = PeerIdTransport {
|
||||
transport: inner,
|
||||
addr_resolver: self.addr_resolver,
|
||||
};
|
||||
|
||||
return Err((id, addr));
|
||||
}
|
||||
};
|
||||
|
||||
let listener = listener.map(move |connec| {
|
||||
let fut = connec
|
||||
.and_then(move |(connec, client_addr)| {
|
||||
client_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.map(move |(connec, original_addr)| {
|
||||
debug!("Successfully incoming connection from {}", original_addr);
|
||||
let info = connec.info.shared();
|
||||
let out = PeerIdTransportOutput {
|
||||
socket: connec.socket,
|
||||
info: Box::new(info.clone()
|
||||
.map(move |info| (*info).clone())
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })),
|
||||
original_addr: original_addr.clone(),
|
||||
};
|
||||
let real_addr = Box::new(info
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })
|
||||
.map(move |info| {
|
||||
let peer_id = info.info.public_key.clone().into_peer_id();
|
||||
debug!("Identified {} as {:?}", original_addr, peer_id);
|
||||
AddrComponent::P2P(peer_id.into()).into()
|
||||
})) as Box<Future<Item = _, Error = _>>;
|
||||
(out, real_addr)
|
||||
});
|
||||
|
||||
Box::new(fut) as Box<Future<Item = _, Error = _>>
|
||||
});
|
||||
|
||||
Ok((Box::new(listener) as Box<_>, listened_addr))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
|
||||
match multiaddr_to_peerid(addr.clone()) {
|
||||
Ok(peer_id) => {
|
||||
// If the multiaddress is a peer ID, try each known multiaddress (taken from the
|
||||
// address resolved) one by one.
|
||||
let addrs = {
|
||||
let resolver = &self.addr_resolver;
|
||||
resolver(peer_id.clone()).into_iter()
|
||||
};
|
||||
|
||||
trace!("Try dialing peer ID {:?} ; loading multiaddrs from addr resolver", peer_id);
|
||||
|
||||
let transport = self.transport;
|
||||
let future = stream::iter_ok(addrs)
|
||||
// Try to dial each address through the transport.
|
||||
.filter_map(move |addr| {
|
||||
match transport.clone().dial(addr) {
|
||||
Ok(dial) => Some(dial),
|
||||
Err((_, addr)) => {
|
||||
debug!("Address {} not supported by underlying transport", addr);
|
||||
None
|
||||
},
|
||||
}
|
||||
})
|
||||
.and_then(move |dial| dial)
|
||||
// Pick the first non-failing dial result by filtering out the ones which fail.
|
||||
.then(|res| Ok(res))
|
||||
.filter_map(|res| res.ok())
|
||||
.into_future()
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(move |(connec, _)| {
|
||||
match connec {
|
||||
Some(connec) => Ok((connec, peer_id)),
|
||||
None => {
|
||||
debug!("All multiaddresses failed when dialing peer {:?}", peer_id);
|
||||
Err(IoError::new(IoErrorKind::Other, "couldn't find any multiaddress for peer"))
|
||||
},
|
||||
}
|
||||
})
|
||||
.and_then(move |((connec, original_addr), peer_id)| {
|
||||
original_addr.map(move |addr| (connec, addr, peer_id))
|
||||
})
|
||||
.and_then(move |(connec, original_addr, peer_id)| {
|
||||
debug!("Successfully dialed peer {:?} through {}", peer_id, original_addr);
|
||||
let out = PeerIdTransportOutput {
|
||||
socket: connec.socket,
|
||||
info: connec.info,
|
||||
original_addr: original_addr,
|
||||
};
|
||||
// Replace the multiaddress with the one of the form `/p2p/...` or `/ipfs/...`.
|
||||
Ok((out, Box::new(future::ok(addr)) as Box<Future<Item = _, Error = _>>))
|
||||
});
|
||||
|
||||
Ok(Box::new(future) as Box<_>)
|
||||
}
|
||||
|
||||
Err(addr) => {
|
||||
// If the multiaddress is something else, propagate it to the underlying transport.
|
||||
trace!("Propagating {} to the underlying transport", addr);
|
||||
let dial = match self.transport.dial(addr) {
|
||||
Ok(d) => d,
|
||||
Err((inner, addr)) => {
|
||||
let id = PeerIdTransport {
|
||||
transport: inner,
|
||||
addr_resolver: self.addr_resolver,
|
||||
};
|
||||
return Err((id, addr));
|
||||
}
|
||||
};
|
||||
|
||||
let future = dial
|
||||
.and_then(move |(connec, original_addr)| {
|
||||
original_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.map(move |(connec, original_addr)| {
|
||||
debug!("Successfully dialed {}", original_addr);
|
||||
let info = connec.info.shared();
|
||||
let out = PeerIdTransportOutput {
|
||||
socket: connec.socket,
|
||||
info: Box::new(info.clone()
|
||||
.map(move |info| (*info).clone())
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })),
|
||||
original_addr: original_addr.clone(),
|
||||
};
|
||||
let real_addr = Box::new(info
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })
|
||||
.map(move |info| {
|
||||
let peer_id = info.info.public_key.clone().into_peer_id();
|
||||
debug!("Identified {} as {:?}", original_addr, peer_id);
|
||||
AddrComponent::P2P(peer_id.into()).into()
|
||||
})) as Box<Future<Item = _, Error = _>>;
|
||||
(out, real_addr)
|
||||
});
|
||||
|
||||
Ok(Box::new(future) as Box<_>)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nat_traversal(&self, a: &Multiaddr, b: &Multiaddr) -> Option<Multiaddr> {
|
||||
self.transport.nat_traversal(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Trans, AddrRes, AddrResOut> MuxedTransport for PeerIdTransport<Trans, AddrRes>
|
||||
where
|
||||
Trans: MuxedTransport + Clone + 'static,
|
||||
Trans::Output: AsyncRead + AsyncWrite,
|
||||
AddrRes: Fn(PeerId) -> AddrResOut + 'static, // TODO: 'static :(
|
||||
AddrResOut: IntoIterator<Item = Multiaddr> + 'static, // TODO: 'static :(
|
||||
{
|
||||
type Incoming = Box<Future<Item = Self::IncomingUpgrade, Error = IoError>>;
|
||||
type IncomingUpgrade = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn next_incoming(self) -> Self::Incoming {
|
||||
let future = self.transport.next_incoming().map(move |incoming| {
|
||||
let future = incoming
|
||||
.and_then(move |(connec, original_addr)| {
|
||||
original_addr.map(move |addr| (connec, addr))
|
||||
})
|
||||
.map(move |(connec, original_addr)| {
|
||||
debug!("Successful incoming substream from {}", original_addr);
|
||||
let info = connec.info.shared();
|
||||
let out = PeerIdTransportOutput {
|
||||
socket: connec.socket,
|
||||
info: Box::new(info.clone()
|
||||
.map(move |info| (*info).clone())
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })),
|
||||
original_addr: original_addr.clone(),
|
||||
};
|
||||
let real_addr = Box::new(info
|
||||
.map_err(move |err| { let k = err.kind(); IoError::new(k, err) })
|
||||
.map(move |info| {
|
||||
let peer_id = info.info.public_key.clone().into_peer_id();
|
||||
debug!("Identified {} as {:?}", original_addr, peer_id);
|
||||
AddrComponent::P2P(peer_id.into()).into()
|
||||
})) as Box<Future<Item = _, Error = _>>;
|
||||
(out, real_addr)
|
||||
});
|
||||
|
||||
Box::new(future) as Box<Future<Item = _, Error = _>>
|
||||
});
|
||||
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
|
||||
/// Output of the identify transport.
|
||||
pub struct PeerIdTransportOutput<S> {
|
||||
/// The socket to communicate with the remote.
|
||||
pub socket: S,
|
||||
|
||||
/// Identification of the remote.
|
||||
/// This may not be known immediately, hence why we use a future.
|
||||
pub info: Box<Future<Item = IdentifyTransportOutcome, Error = IoError>>,
|
||||
|
||||
/// Original address of the remote.
|
||||
/// This layer turns the address of the remote into the `/p2p/...` form, but stores the
|
||||
/// original address in this field.
|
||||
pub original_addr: Multiaddr,
|
||||
}
|
||||
|
||||
// If the multiaddress is in the form `/p2p/...`, turn it into a `PeerId`.
|
||||
// Otherwise, return it as-is.
|
||||
fn multiaddr_to_peerid(addr: Multiaddr) -> Result<PeerId, Multiaddr> {
|
||||
let components = addr.iter().collect::<Vec<_>>();
|
||||
if components.len() < 1 {
|
||||
return Err(addr);
|
||||
}
|
||||
|
||||
match components.last() {
|
||||
Some(&AddrComponent::P2P(ref peer_id)) => {
|
||||
match PeerId::from_multihash(peer_id.clone()) {
|
||||
Ok(peer_id) => Ok(peer_id),
|
||||
Err(_) => Err(addr),
|
||||
}
|
||||
}
|
||||
_ => Err(addr),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate libp2p_tcp_transport;
|
||||
extern crate tokio_current_thread;
|
||||
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use PeerIdTransport;
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_core::{Transport, PeerId, PublicKey};
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
|
||||
#[test]
|
||||
fn dial_peer_id() {
|
||||
// When we dial an `/p2p/...` address, the `PeerIdTransport` should look into the
|
||||
// peerstore and dial one of the known multiaddresses of the node instead.
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct UnderlyingTrans {
|
||||
inner: TcpConfig,
|
||||
}
|
||||
impl Transport for UnderlyingTrans {
|
||||
type Output = <TcpConfig as Transport>::Output;
|
||||
type MultiaddrFuture = <TcpConfig as Transport>::MultiaddrFuture;
|
||||
type Listener = Box<Stream<Item = Self::ListenerUpgrade, Error = IoError>>;
|
||||
type ListenerUpgrade = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
type Dial = <TcpConfig as Transport>::Dial;
|
||||
#[inline]
|
||||
fn listen_on(
|
||||
self,
|
||||
_: Multiaddr,
|
||||
) -> Result<(Self::Listener, Multiaddr), (Self, Multiaddr)> {
|
||||
unreachable!()
|
||||
}
|
||||
#[inline]
|
||||
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, (Self, Multiaddr)> {
|
||||
assert_eq!(
|
||||
addr,
|
||||
"/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap()
|
||||
);
|
||||
Ok(self.inner.dial(addr).unwrap_or_else(|_| panic!()))
|
||||
}
|
||||
#[inline]
|
||||
fn nat_traversal(&self, a: &Multiaddr, b: &Multiaddr) -> Option<Multiaddr> {
|
||||
self.inner.nat_traversal(a, b)
|
||||
}
|
||||
}
|
||||
|
||||
let peer_id = PeerId::from_public_key(PublicKey::Ed25519(vec![1, 2, 3, 4]));
|
||||
|
||||
let underlying = UnderlyingTrans {
|
||||
inner: TcpConfig::new(),
|
||||
};
|
||||
let transport = PeerIdTransport::new(underlying, {
|
||||
let peer_id = peer_id.clone();
|
||||
move |addr| {
|
||||
assert_eq!(addr, peer_id);
|
||||
vec!["/ip4/127.0.0.1/tcp/12345".parse().unwrap()]
|
||||
}
|
||||
});
|
||||
|
||||
let future = transport
|
||||
.dial(iter::once(AddrComponent::P2P(peer_id.into())).collect())
|
||||
.unwrap_or_else(|_| panic!())
|
||||
.then::<_, Result<(), ()>>(|_| Ok(()));
|
||||
|
||||
let _ = tokio_current_thread::block_on_all(future).unwrap();
|
||||
}
|
||||
}
|
311
protocols/identify/src/protocol.rs
Normal file
311
protocols/identify/src/protocol.rs
Normal file
@ -0,0 +1,311 @@
|
||||
// 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.
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{future, Future, Sink, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PublicKey};
|
||||
use multiaddr::Multiaddr;
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
|
||||
use protobuf::RepeatedField;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::iter;
|
||||
use structs_proto;
|
||||
use tokio_codec::Framed;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use unsigned_varint::codec;
|
||||
|
||||
/// Configuration for an upgrade to the identity protocol.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IdentifyProtocolConfig;
|
||||
|
||||
/// Output of the connection upgrade.
|
||||
pub enum IdentifyOutput<T> {
|
||||
/// We obtained information from the remote. Happens when we are the dialer.
|
||||
RemoteInfo {
|
||||
/// Information about the remote.
|
||||
info: IdentifyInfo,
|
||||
/// Address the remote sees for us.
|
||||
observed_addr: Multiaddr,
|
||||
},
|
||||
|
||||
/// We opened a connection to the remote and need to send it information. Happens when we are
|
||||
/// the listener.
|
||||
Sender {
|
||||
/// Object used to send identify info to the client.
|
||||
sender: IdentifySender<T>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Object used to send back information to the client.
|
||||
pub struct IdentifySender<T> {
|
||||
inner: Framed<T, codec::UviBytes<Vec<u8>>>,
|
||||
}
|
||||
|
||||
impl<'a, T> IdentifySender<T>
|
||||
where
|
||||
T: AsyncWrite + 'a,
|
||||
{
|
||||
/// Sends back information to the client. Returns a future that is signalled whenever the
|
||||
/// info have been sent.
|
||||
pub fn send(
|
||||
self,
|
||||
info: IdentifyInfo,
|
||||
observed_addr: &Multiaddr,
|
||||
) -> Box<Future<Item = (), Error = IoError> + 'a> {
|
||||
debug!("Sending identify info to client");
|
||||
trace!("Sending: {:?}", info);
|
||||
|
||||
let listen_addrs = info.listen_addrs
|
||||
.into_iter()
|
||||
.map(|addr| addr.into_bytes())
|
||||
.collect();
|
||||
|
||||
let mut message = structs_proto::Identify::new();
|
||||
message.set_agentVersion(info.agent_version);
|
||||
message.set_protocolVersion(info.protocol_version);
|
||||
message.set_publicKey(info.public_key.into_protobuf_encoding());
|
||||
message.set_listenAddrs(listen_addrs);
|
||||
message.set_observedAddr(observed_addr.to_bytes());
|
||||
message.set_protocols(RepeatedField::from_vec(info.protocols));
|
||||
|
||||
let bytes = message
|
||||
.write_to_bytes()
|
||||
.expect("writing protobuf failed ; should never happen");
|
||||
|
||||
let future = self.inner.send(bytes).map(|_| ());
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
|
||||
/// Information sent from the listener to the dialer.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IdentifyInfo {
|
||||
/// Public key of the node.
|
||||
pub public_key: PublicKey,
|
||||
/// Version of the "global" protocol, eg. `ipfs/1.0.0` or `polkadot/1.0.0`.
|
||||
pub protocol_version: String,
|
||||
/// Name and version of the client. Can be thought as similar to the `User-Agent` header
|
||||
/// of HTTP.
|
||||
pub agent_version: String,
|
||||
/// Addresses that the node is listening on.
|
||||
pub listen_addrs: Vec<Multiaddr>,
|
||||
/// Protocols supported by the node, eg. `/ipfs/ping/1.0.0`.
|
||||
pub protocols: Vec<String>,
|
||||
}
|
||||
|
||||
impl<C, Maf> ConnectionUpgrade<C, Maf> for IdentifyProtocolConfig
|
||||
where
|
||||
C: AsyncRead + AsyncWrite + 'static,
|
||||
Maf: Future<Item = Multiaddr, Error = IoError> + 'static,
|
||||
{
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeIdentifier)>;
|
||||
type UpgradeIdentifier = ();
|
||||
type Output = IdentifyOutput<C>;
|
||||
type MultiaddrFuture = future::Either<future::FutureResult<Multiaddr, IoError>, Maf>;
|
||||
type Future = Box<Future<Item = (Self::Output, Self::MultiaddrFuture), Error = IoError>>;
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once((Bytes::from("/ipfs/id/1.0.0"), ()))
|
||||
}
|
||||
|
||||
fn upgrade(self, socket: C, _: (), ty: Endpoint, remote_addr: Maf) -> Self::Future {
|
||||
trace!("Upgrading connection as {:?}", ty);
|
||||
|
||||
let socket = Framed::new(socket, codec::UviBytes::default());
|
||||
|
||||
match ty {
|
||||
Endpoint::Dialer => {
|
||||
let future = socket
|
||||
.into_future()
|
||||
.map(|(msg, _)| msg)
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(|msg| {
|
||||
debug!("Received identify message");
|
||||
|
||||
if let Some(msg) = msg {
|
||||
let (info, observed_addr) = match parse_proto_msg(msg) {
|
||||
Ok(v) => v,
|
||||
Err(err) => {
|
||||
debug!("Failed to parse protobuf message ; error = {:?}", err);
|
||||
return Err(err.into());
|
||||
}
|
||||
};
|
||||
|
||||
trace!("Remote observes us as {:?}", observed_addr);
|
||||
trace!("Information received: {:?}", info);
|
||||
|
||||
let out = IdentifyOutput::RemoteInfo {
|
||||
info,
|
||||
observed_addr: observed_addr.clone(),
|
||||
};
|
||||
|
||||
Ok((out, future::Either::A(future::ok(observed_addr))))
|
||||
} else {
|
||||
debug!("Identify protocol stream closed before receiving info");
|
||||
Err(IoErrorKind::InvalidData.into())
|
||||
}
|
||||
});
|
||||
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
|
||||
Endpoint::Listener => {
|
||||
let sender = IdentifySender { inner: socket };
|
||||
|
||||
let future = future::ok({
|
||||
let io = IdentifyOutput::Sender {
|
||||
sender,
|
||||
};
|
||||
|
||||
(io, future::Either::B(remote_addr))
|
||||
});
|
||||
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Turns a protobuf message into an `IdentifyInfo` and an observed address. If something bad
|
||||
// happens, turn it into an `IoError`.
|
||||
fn parse_proto_msg(msg: BytesMut) -> Result<(IdentifyInfo, Multiaddr), IoError> {
|
||||
match protobuf_parse_from_bytes::<structs_proto::Identify>(&msg) {
|
||||
Ok(mut msg) => {
|
||||
// Turn a `Vec<u8>` into a `Multiaddr`. If something bad happens, turn it into
|
||||
// an `IoError`.
|
||||
fn bytes_to_multiaddr(bytes: Vec<u8>) -> Result<Multiaddr, IoError> {
|
||||
Multiaddr::from_bytes(bytes)
|
||||
.map_err(|err| IoError::new(IoErrorKind::InvalidData, err))
|
||||
}
|
||||
|
||||
let listen_addrs = {
|
||||
let mut addrs = Vec::new();
|
||||
for addr in msg.take_listenAddrs().into_iter() {
|
||||
addrs.push(bytes_to_multiaddr(addr)?);
|
||||
}
|
||||
addrs
|
||||
};
|
||||
|
||||
let observed_addr = bytes_to_multiaddr(msg.take_observedAddr())?;
|
||||
let info = IdentifyInfo {
|
||||
public_key: PublicKey::from_protobuf_encoding(msg.get_publicKey())?,
|
||||
protocol_version: msg.take_protocolVersion(),
|
||||
agent_version: msg.take_agentVersion(),
|
||||
listen_addrs: listen_addrs,
|
||||
protocols: msg.take_protocols().into_vec(),
|
||||
};
|
||||
|
||||
Ok((info, observed_addr))
|
||||
}
|
||||
|
||||
Err(err) => Err(IoError::new(IoErrorKind::InvalidData, err)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate libp2p_tcp_transport;
|
||||
extern crate tokio_current_thread;
|
||||
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use futures::{Future, Stream};
|
||||
use libp2p_core::{PublicKey, Transport};
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use {IdentifyInfo, IdentifyOutput, IdentifyProtocolConfig};
|
||||
|
||||
#[test]
|
||||
fn correct_transfer() {
|
||||
// We open a server and a client, send info from the server to the client, and check that
|
||||
// they were successfully received.
|
||||
|
||||
let (tx, rx) = mpsc::channel();
|
||||
|
||||
let bg_thread = thread::spawn(move || {
|
||||
let transport = TcpConfig::new().with_upgrade(IdentifyProtocolConfig);
|
||||
|
||||
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)
|
||||
.and_then(|(client, _)| client.unwrap().map(|v| v.0))
|
||||
.and_then(|identify| match identify {
|
||||
IdentifyOutput::Sender { sender, .. } => sender.send(
|
||||
IdentifyInfo {
|
||||
public_key: PublicKey::Ed25519(vec![1, 2, 3, 4, 5, 7]),
|
||||
protocol_version: "proto_version".to_owned(),
|
||||
agent_version: "agent_version".to_owned(),
|
||||
listen_addrs: vec![
|
||||
"/ip4/80.81.82.83/tcp/500".parse().unwrap(),
|
||||
"/ip6/::1/udp/1000".parse().unwrap(),
|
||||
],
|
||||
protocols: vec!["proto1".to_string(), "proto2".to_string()],
|
||||
},
|
||||
&"/ip4/100.101.102.103/tcp/5000".parse().unwrap(),
|
||||
),
|
||||
_ => panic!(),
|
||||
});
|
||||
|
||||
let _ = tokio_current_thread::block_on_all(future).unwrap();
|
||||
});
|
||||
|
||||
let transport = TcpConfig::new().with_upgrade(IdentifyProtocolConfig);
|
||||
|
||||
let future = transport
|
||||
.dial(rx.recv().unwrap())
|
||||
.unwrap_or_else(|_| panic!())
|
||||
.and_then(|(identify, _)| match identify {
|
||||
IdentifyOutput::RemoteInfo {
|
||||
info,
|
||||
observed_addr,
|
||||
} => {
|
||||
assert_eq!(
|
||||
observed_addr,
|
||||
"/ip4/100.101.102.103/tcp/5000".parse().unwrap()
|
||||
);
|
||||
assert_eq!(info.public_key, PublicKey::Ed25519(vec![1, 2, 3, 4, 5, 7]));
|
||||
assert_eq!(info.protocol_version, "proto_version");
|
||||
assert_eq!(info.agent_version, "agent_version");
|
||||
assert_eq!(
|
||||
info.listen_addrs,
|
||||
&[
|
||||
"/ip4/80.81.82.83/tcp/500".parse().unwrap(),
|
||||
"/ip6/::1/udp/1000".parse().unwrap()
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
info.protocols,
|
||||
&["proto1".to_string(), "proto2".to_string()]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
_ => panic!(),
|
||||
});
|
||||
|
||||
let _ = tokio_current_thread::block_on_all(future).unwrap();
|
||||
bg_thread.join().unwrap();
|
||||
}
|
||||
}
|
497
protocols/identify/src/structs_proto.rs
Normal file
497
protocols/identify/src/structs_proto.rs
Normal file
@ -0,0 +1,497 @@
|
||||
// This file is generated by rust-protobuf 2.0.2. Do not edit
|
||||
// @generated
|
||||
|
||||
// https://github.com/Manishearth/rust-clippy/issues/702
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(clippy)]
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
#![allow(box_pointers)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(trivial_casts)]
|
||||
#![allow(unsafe_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_results)]
|
||||
|
||||
use protobuf::Message as Message_imported_for_functions;
|
||||
use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Identify {
|
||||
// message fields
|
||||
protocolVersion: ::protobuf::SingularField<::std::string::String>,
|
||||
agentVersion: ::protobuf::SingularField<::std::string::String>,
|
||||
publicKey: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
listenAddrs: ::protobuf::RepeatedField<::std::vec::Vec<u8>>,
|
||||
observedAddr: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
protocols: ::protobuf::RepeatedField<::std::string::String>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Identify {
|
||||
pub fn new() -> Identify {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional string protocolVersion = 5;
|
||||
|
||||
pub fn clear_protocolVersion(&mut self) {
|
||||
self.protocolVersion.clear();
|
||||
}
|
||||
|
||||
pub fn has_protocolVersion(&self) -> bool {
|
||||
self.protocolVersion.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_protocolVersion(&mut self, v: ::std::string::String) {
|
||||
self.protocolVersion = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_protocolVersion(&mut self) -> &mut ::std::string::String {
|
||||
if self.protocolVersion.is_none() {
|
||||
self.protocolVersion.set_default();
|
||||
}
|
||||
self.protocolVersion.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_protocolVersion(&mut self) -> ::std::string::String {
|
||||
self.protocolVersion.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_protocolVersion(&self) -> &str {
|
||||
match self.protocolVersion.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional string agentVersion = 6;
|
||||
|
||||
pub fn clear_agentVersion(&mut self) {
|
||||
self.agentVersion.clear();
|
||||
}
|
||||
|
||||
pub fn has_agentVersion(&self) -> bool {
|
||||
self.agentVersion.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_agentVersion(&mut self, v: ::std::string::String) {
|
||||
self.agentVersion = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_agentVersion(&mut self) -> &mut ::std::string::String {
|
||||
if self.agentVersion.is_none() {
|
||||
self.agentVersion.set_default();
|
||||
}
|
||||
self.agentVersion.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_agentVersion(&mut self) -> ::std::string::String {
|
||||
self.agentVersion.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_agentVersion(&self) -> &str {
|
||||
match self.agentVersion.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional bytes publicKey = 1;
|
||||
|
||||
pub fn clear_publicKey(&mut self) {
|
||||
self.publicKey.clear();
|
||||
}
|
||||
|
||||
pub fn has_publicKey(&self) -> bool {
|
||||
self.publicKey.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_publicKey(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.publicKey = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_publicKey(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.publicKey.is_none() {
|
||||
self.publicKey.set_default();
|
||||
}
|
||||
self.publicKey.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_publicKey(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.publicKey.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_publicKey(&self) -> &[u8] {
|
||||
match self.publicKey.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// repeated bytes listenAddrs = 2;
|
||||
|
||||
pub fn clear_listenAddrs(&mut self) {
|
||||
self.listenAddrs.clear();
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_listenAddrs(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec<u8>>) {
|
||||
self.listenAddrs = v;
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
pub fn mut_listenAddrs(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec<u8>> {
|
||||
&mut self.listenAddrs
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_listenAddrs(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec<u8>> {
|
||||
::std::mem::replace(&mut self.listenAddrs, ::protobuf::RepeatedField::new())
|
||||
}
|
||||
|
||||
pub fn get_listenAddrs(&self) -> &[::std::vec::Vec<u8>] {
|
||||
&self.listenAddrs
|
||||
}
|
||||
|
||||
// optional bytes observedAddr = 4;
|
||||
|
||||
pub fn clear_observedAddr(&mut self) {
|
||||
self.observedAddr.clear();
|
||||
}
|
||||
|
||||
pub fn has_observedAddr(&self) -> bool {
|
||||
self.observedAddr.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_observedAddr(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.observedAddr = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_observedAddr(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.observedAddr.is_none() {
|
||||
self.observedAddr.set_default();
|
||||
}
|
||||
self.observedAddr.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_observedAddr(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.observedAddr.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_observedAddr(&self) -> &[u8] {
|
||||
match self.observedAddr.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// repeated string protocols = 3;
|
||||
|
||||
pub fn clear_protocols(&mut self) {
|
||||
self.protocols.clear();
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_protocols(&mut self, v: ::protobuf::RepeatedField<::std::string::String>) {
|
||||
self.protocols = v;
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
pub fn mut_protocols(&mut self) -> &mut ::protobuf::RepeatedField<::std::string::String> {
|
||||
&mut self.protocols
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_protocols(&mut self) -> ::protobuf::RepeatedField<::std::string::String> {
|
||||
::std::mem::replace(&mut self.protocols, ::protobuf::RepeatedField::new())
|
||||
}
|
||||
|
||||
pub fn get_protocols(&self) -> &[::std::string::String] {
|
||||
&self.protocols
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Identify {
|
||||
fn is_initialized(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
5 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.protocolVersion)?;
|
||||
},
|
||||
6 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.agentVersion)?;
|
||||
},
|
||||
1 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.publicKey)?;
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.listenAddrs)?;
|
||||
},
|
||||
4 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.observedAddr)?;
|
||||
},
|
||||
3 => {
|
||||
::protobuf::rt::read_repeated_string_into(wire_type, is, &mut self.protocols)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(ref v) = self.protocolVersion.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(5, &v);
|
||||
}
|
||||
if let Some(ref v) = self.agentVersion.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(6, &v);
|
||||
}
|
||||
if let Some(ref v) = self.publicKey.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(1, &v);
|
||||
}
|
||||
for value in &self.listenAddrs {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &value);
|
||||
};
|
||||
if let Some(ref v) = self.observedAddr.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(4, &v);
|
||||
}
|
||||
for value in &self.protocols {
|
||||
my_size += ::protobuf::rt::string_size(3, &value);
|
||||
};
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(ref v) = self.protocolVersion.as_ref() {
|
||||
os.write_string(5, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.agentVersion.as_ref() {
|
||||
os.write_string(6, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.publicKey.as_ref() {
|
||||
os.write_bytes(1, &v)?;
|
||||
}
|
||||
for v in &self.listenAddrs {
|
||||
os.write_bytes(2, &v)?;
|
||||
};
|
||||
if let Some(ref v) = self.observedAddr.as_ref() {
|
||||
os.write_bytes(4, &v)?;
|
||||
}
|
||||
for v in &self.protocols {
|
||||
os.write_string(3, &v)?;
|
||||
};
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Identify {
|
||||
Identify::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"protocolVersion",
|
||||
|m: &Identify| { &m.protocolVersion },
|
||||
|m: &mut Identify| { &mut m.protocolVersion },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"agentVersion",
|
||||
|m: &Identify| { &m.agentVersion },
|
||||
|m: &mut Identify| { &mut m.agentVersion },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"publicKey",
|
||||
|m: &Identify| { &m.publicKey },
|
||||
|m: &mut Identify| { &mut m.publicKey },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"listenAddrs",
|
||||
|m: &Identify| { &m.listenAddrs },
|
||||
|m: &mut Identify| { &mut m.listenAddrs },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"observedAddr",
|
||||
|m: &Identify| { &m.observedAddr },
|
||||
|m: &mut Identify| { &mut m.observedAddr },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"protocols",
|
||||
|m: &Identify| { &m.protocols },
|
||||
|m: &mut Identify| { &mut m.protocols },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Identify>(
|
||||
"Identify",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Identify {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Identify> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Identify,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Identify::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Identify {
|
||||
fn clear(&mut self) {
|
||||
self.clear_protocolVersion();
|
||||
self.clear_agentVersion();
|
||||
self.clear_publicKey();
|
||||
self.clear_listenAddrs();
|
||||
self.clear_observedAddr();
|
||||
self.clear_protocols();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Identify {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Identify {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
static file_descriptor_proto_data: &'static [u8] = b"\
|
||||
\n\rstructs.proto\"\xda\x01\n\x08Identify\x12(\n\x0fprotocolVersion\x18\
|
||||
\x05\x20\x01(\tR\x0fprotocolVersion\x12\"\n\x0cagentVersion\x18\x06\x20\
|
||||
\x01(\tR\x0cagentVersion\x12\x1c\n\tpublicKey\x18\x01\x20\x01(\x0cR\tpub\
|
||||
licKey\x12\x20\n\x0blistenAddrs\x18\x02\x20\x03(\x0cR\x0blistenAddrs\x12\
|
||||
\"\n\x0cobservedAddr\x18\x04\x20\x01(\x0cR\x0cobservedAddr\x12\x1c\n\tpr\
|
||||
otocols\x18\x03\x20\x03(\tR\tprotocolsJ\xc2\t\n\x06\x12\x04\0\0\x16\x01\
|
||||
\n\n\n\x02\x04\0\x12\x04\0\0\x16\x01\n\n\n\x03\x04\0\x01\x12\x03\0\x08\
|
||||
\x10\nX\n\x04\x04\0\x02\0\x12\x03\x02\x02&\x1a8\x20protocolVersion\x20de\
|
||||
termines\x20compatibility\x20between\x20peers\n\"\x11\x20e.g.\x20ipfs/1.\
|
||||
0.0\n\n\x0c\n\x05\x04\0\x02\0\x04\x12\x03\x02\x02\n\n\x0c\n\x05\x04\0\
|
||||
\x02\0\x05\x12\x03\x02\x0b\x11\n\x0c\n\x05\x04\0\x02\0\x01\x12\x03\x02\
|
||||
\x12!\n\x0c\n\x05\x04\0\x02\0\x03\x12\x03\x02$%\n\x9f\x01\n\x04\x04\0\
|
||||
\x02\x01\x12\x03\x06\x02#\x1a|\x20agentVersion\x20is\x20like\x20a\x20Use\
|
||||
rAgent\x20string\x20in\x20browsers,\x20or\x20client\x20version\x20in\x20\
|
||||
bittorrent\n\x20includes\x20the\x20client\x20name\x20and\x20client.\n\"\
|
||||
\x14\x20e.g.\x20go-ipfs/0.1.0\n\n\x0c\n\x05\x04\0\x02\x01\x04\x12\x03\
|
||||
\x06\x02\n\n\x0c\n\x05\x04\0\x02\x01\x05\x12\x03\x06\x0b\x11\n\x0c\n\x05\
|
||||
\x04\0\x02\x01\x01\x12\x03\x06\x12\x1e\n\x0c\n\x05\x04\0\x02\x01\x03\x12\
|
||||
\x03\x06!\"\n\xe3\x01\n\x04\x04\0\x02\x02\x12\x03\x0b\x02\x1f\x1a\xd5\
|
||||
\x01\x20publicKey\x20is\x20this\x20node's\x20public\x20key\x20(which\x20\
|
||||
also\x20gives\x20its\x20node.ID)\n\x20-\x20may\x20not\x20need\x20to\x20b\
|
||||
e\x20sent,\x20as\x20secure\x20channel\x20implies\x20it\x20has\x20been\
|
||||
\x20sent.\n\x20-\x20then\x20again,\x20if\x20we\x20change\x20/\x20disable\
|
||||
\x20secure\x20channel,\x20may\x20still\x20want\x20it.\n\n\x0c\n\x05\x04\
|
||||
\0\x02\x02\x04\x12\x03\x0b\x02\n\n\x0c\n\x05\x04\0\x02\x02\x05\x12\x03\
|
||||
\x0b\x0b\x10\n\x0c\n\x05\x04\0\x02\x02\x01\x12\x03\x0b\x11\x1a\n\x0c\n\
|
||||
\x05\x04\0\x02\x02\x03\x12\x03\x0b\x1d\x1e\n]\n\x04\x04\0\x02\x03\x12\
|
||||
\x03\x0e\x02!\x1aP\x20listenAddrs\x20are\x20the\x20multiaddrs\x20the\x20\
|
||||
sender\x20node\x20listens\x20for\x20open\x20connections\x20on\n\n\x0c\n\
|
||||
\x05\x04\0\x02\x03\x04\x12\x03\x0e\x02\n\n\x0c\n\x05\x04\0\x02\x03\x05\
|
||||
\x12\x03\x0e\x0b\x10\n\x0c\n\x05\x04\0\x02\x03\x01\x12\x03\x0e\x11\x1c\n\
|
||||
\x0c\n\x05\x04\0\x02\x03\x03\x12\x03\x0e\x1f\x20\n\x81\x02\n\x04\x04\0\
|
||||
\x02\x04\x12\x03\x13\x02\"\x1a\xf3\x01\x20oservedAddr\x20is\x20the\x20mu\
|
||||
ltiaddr\x20of\x20the\x20remote\x20endpoint\x20that\x20the\x20sender\x20n\
|
||||
ode\x20perceives\n\x20this\x20is\x20useful\x20information\x20to\x20conve\
|
||||
y\x20to\x20the\x20other\x20side,\x20as\x20it\x20helps\x20the\x20remote\
|
||||
\x20endpoint\n\x20determine\x20whether\x20its\x20connection\x20to\x20the\
|
||||
\x20local\x20peer\x20goes\x20through\x20NAT.\n\n\x0c\n\x05\x04\0\x02\x04\
|
||||
\x04\x12\x03\x13\x02\n\n\x0c\n\x05\x04\0\x02\x04\x05\x12\x03\x13\x0b\x10\
|
||||
\n\x0c\n\x05\x04\0\x02\x04\x01\x12\x03\x13\x11\x1d\n\x0c\n\x05\x04\0\x02\
|
||||
\x04\x03\x12\x03\x13\x20!\n\x0b\n\x04\x04\0\x02\x05\x12\x03\x15\x02\x20\
|
||||
\n\x0c\n\x05\x04\0\x02\x05\x04\x12\x03\x15\x02\n\n\x0c\n\x05\x04\0\x02\
|
||||
\x05\x05\x12\x03\x15\x0b\x11\n\x0c\n\x05\x04\0\x02\x05\x01\x12\x03\x15\
|
||||
\x12\x1b\n\x0c\n\x05\x04\0\x02\x05\x03\x12\x03\x15\x1e\x1f\
|
||||
";
|
||||
|
||||
static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto,
|
||||
};
|
||||
|
||||
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
|
||||
::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
|
||||
unsafe {
|
||||
file_descriptor_proto_lazy.get(|| {
|
||||
parse_descriptor_proto()
|
||||
})
|
||||
}
|
||||
}
|
23
protocols/identify/structs.proto
Normal file
23
protocols/identify/structs.proto
Normal file
@ -0,0 +1,23 @@
|
||||
message Identify {
|
||||
// protocolVersion determines compatibility between peers
|
||||
optional string protocolVersion = 5; // e.g. ipfs/1.0.0
|
||||
|
||||
// agentVersion is like a UserAgent string in browsers, or client version in bittorrent
|
||||
// includes the client name and client.
|
||||
optional string agentVersion = 6; // e.g. go-ipfs/0.1.0
|
||||
|
||||
// publicKey is this node's public key (which also gives its node.ID)
|
||||
// - may not need to be sent, as secure channel implies it has been sent.
|
||||
// - then again, if we change / disable secure channel, may still want it.
|
||||
optional bytes publicKey = 1;
|
||||
|
||||
// listenAddrs are the multiaddrs the sender node listens for open connections on
|
||||
repeated bytes listenAddrs = 2;
|
||||
|
||||
// oservedAddr is the multiaddr of the remote endpoint that the sender node perceives
|
||||
// this is useful information to convey to the other side, as it helps the remote endpoint
|
||||
// determine whether its connection to the local peer goes through NAT.
|
||||
optional bytes observedAddr = 4;
|
||||
|
||||
repeated string protocols = 3;
|
||||
}
|
32
protocols/kad/Cargo.toml
Normal file
32
protocols/kad/Cargo.toml
Normal file
@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "libp2p-kad"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
arrayvec = "0.4.7"
|
||||
bs58 = "0.2.0"
|
||||
bigint = "4.2"
|
||||
bytes = "0.4"
|
||||
datastore = { path = "../../stores/datastore" }
|
||||
fnv = "1.0"
|
||||
futures = "0.1"
|
||||
libp2p-identify = { path = "../../protocols/identify" }
|
||||
libp2p-ping = { path = "../../protocols/ping" }
|
||||
libp2p-core = { path = "../../core" }
|
||||
log = "0.4"
|
||||
multiaddr = { path = "../../misc/multiaddr" }
|
||||
parking_lot = "0.6"
|
||||
protobuf = "2.0.2"
|
||||
rand = "0.4.2"
|
||||
smallvec = "0.5"
|
||||
tokio-codec = "0.1"
|
||||
tokio-io = "0.1"
|
||||
tokio-timer = "0.2.6"
|
||||
unsigned-varint = { version = "0.1", features = ["codec"] }
|
||||
|
||||
[dev-dependencies]
|
||||
libp2p-tcp-transport = { path = "../../transports/tcp" }
|
||||
rand = "0.4.2"
|
||||
tokio-current-thread = "0.1"
|
63
protocols/kad/dht.proto
Normal file
63
protocols/kad/dht.proto
Normal file
@ -0,0 +1,63 @@
|
||||
syntax = "proto2";
|
||||
package dht.pb;
|
||||
|
||||
import "record.proto";
|
||||
|
||||
message Message {
|
||||
enum MessageType {
|
||||
PUT_VALUE = 0;
|
||||
GET_VALUE = 1;
|
||||
ADD_PROVIDER = 2;
|
||||
GET_PROVIDERS = 3;
|
||||
FIND_NODE = 4;
|
||||
PING = 5;
|
||||
}
|
||||
|
||||
enum ConnectionType {
|
||||
// sender does not have a connection to peer, and no extra information (default)
|
||||
NOT_CONNECTED = 0;
|
||||
|
||||
// sender has a live connection to peer
|
||||
CONNECTED = 1;
|
||||
|
||||
// sender recently connected to peer
|
||||
CAN_CONNECT = 2;
|
||||
|
||||
// sender recently tried to connect to peer repeatedly but failed to connect
|
||||
// ("try" here is loose, but this should signal "made strong effort, failed")
|
||||
CANNOT_CONNECT = 3;
|
||||
}
|
||||
|
||||
message Peer {
|
||||
// ID of a given peer.
|
||||
optional bytes id = 1;
|
||||
|
||||
// multiaddrs for a given peer
|
||||
repeated bytes addrs = 2;
|
||||
|
||||
// used to signal the sender's connection capabilities to the peer
|
||||
optional ConnectionType connection = 3;
|
||||
}
|
||||
|
||||
// defines what type of message it is.
|
||||
optional MessageType type = 1;
|
||||
|
||||
// defines what coral cluster level this query/response belongs to.
|
||||
optional int32 clusterLevelRaw = 10;
|
||||
|
||||
// Used to specify the key associated with this message.
|
||||
// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
|
||||
optional bytes key = 2;
|
||||
|
||||
// Used to return a value
|
||||
// PUT_VALUE, GET_VALUE
|
||||
optional record.pb.Record record = 3;
|
||||
|
||||
// Used to return peers closer to a key in a query
|
||||
// GET_VALUE, GET_PROVIDERS, FIND_NODE
|
||||
repeated Peer closerPeers = 8;
|
||||
|
||||
// Used to return Providers
|
||||
// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
|
||||
repeated Peer providerPeers = 9;
|
||||
}
|
21
protocols/kad/record.proto
Normal file
21
protocols/kad/record.proto
Normal file
@ -0,0 +1,21 @@
|
||||
syntax = "proto2";
|
||||
package record.pb;
|
||||
|
||||
// Record represents a dht record that contains a value
|
||||
// for a key value pair
|
||||
message Record {
|
||||
// The key that references this record
|
||||
optional string key = 1;
|
||||
|
||||
// The actual value this record is storing
|
||||
optional bytes value = 2;
|
||||
|
||||
// hash of the authors public key
|
||||
optional string author = 3;
|
||||
|
||||
// A PKI signature for the key+value+author
|
||||
optional bytes signature = 4;
|
||||
|
||||
// Time the record was received, set by receiver
|
||||
optional string timeReceived = 5;
|
||||
}
|
15
protocols/kad/regen_dht_proto.sh
Executable file
15
protocols/kad/regen_dht_proto.sh
Executable file
@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/dht_proto.rs` file from `dht.proto`.
|
||||
|
||||
docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 2.0.2 protobuf-codegen; \
|
||||
protoc --rust_out . dht.proto;\
|
||||
protoc --rust_out . record.proto"
|
||||
|
||||
sudo chown $USER:$USER *.rs
|
||||
|
||||
mv -f dht.rs ./src/protobuf_structs/dht.rs
|
||||
mv -f record.rs ./src/protobuf_structs/record.rs
|
464
protocols/kad/src/high_level.rs
Normal file
464
protocols/kad/src/high_level.rs
Normal file
@ -0,0 +1,464 @@
|
||||
// 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.
|
||||
|
||||
use fnv::FnvHashSet;
|
||||
use futures::{future, Future, IntoFuture, stream, Stream};
|
||||
use kad_server::KadConnecController;
|
||||
use kbucket::{KBucketsTable, KBucketsPeerId};
|
||||
use libp2p_core::PeerId;
|
||||
use multiaddr::Multiaddr;
|
||||
use protocol;
|
||||
use rand;
|
||||
use smallvec::SmallVec;
|
||||
use std::cmp::Ordering;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::mem;
|
||||
use std::time::Duration;
|
||||
use tokio_timer::Timeout;
|
||||
|
||||
/// Prototype for a future Kademlia protocol running on a socket.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KadSystemConfig<I> {
|
||||
/// Degree of parallelism on the network. Often called `alpha` in technical papers.
|
||||
/// No more than this number of remotes will be used at a given time for any given operation.
|
||||
// TODO: ^ share this number between operations? or does each operation use `alpha` remotes?
|
||||
pub parallelism: u32,
|
||||
/// Id of the local peer.
|
||||
pub local_peer_id: PeerId,
|
||||
/// List of peers initially known.
|
||||
pub known_initial_peers: I,
|
||||
/// Duration after which a node in the k-buckets needs to be pinged again.
|
||||
pub kbuckets_timeout: Duration,
|
||||
/// When contacting a node, duration after which we consider it unresponsive.
|
||||
pub request_timeout: Duration,
|
||||
}
|
||||
|
||||
/// System that drives the whole Kademlia process.
|
||||
pub struct KadSystem {
|
||||
// The actual DHT.
|
||||
kbuckets: KBucketsTable<PeerId, ()>,
|
||||
// Same as in the config.
|
||||
parallelism: u32,
|
||||
// Same as in the config.
|
||||
request_timeout: Duration,
|
||||
}
|
||||
|
||||
/// Event that happens during a query.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum KadQueryEvent<TOut> {
|
||||
/// Learned about new mutiaddresses for the given peers.
|
||||
NewKnownMultiaddrs(Vec<(PeerId, Vec<Multiaddr>)>),
|
||||
/// Finished the processing of the query. Contains the result.
|
||||
Finished(TOut),
|
||||
}
|
||||
|
||||
impl KadSystem {
|
||||
/// Starts a new Kademlia system.
|
||||
///
|
||||
/// Also produces a `Future` that drives a Kademlia initialization process.
|
||||
/// This future should be driven to completion by the caller.
|
||||
pub fn start<'a, F, Fut>(config: KadSystemConfig<impl Iterator<Item = PeerId>>, access: F)
|
||||
-> (KadSystem, impl Future<Item = (), Error = IoError> + 'a)
|
||||
where F: FnMut(&PeerId) -> Fut + Clone + 'a,
|
||||
Fut: IntoFuture<Item = KadConnecController, Error = IoError> + 'a,
|
||||
{
|
||||
let system = KadSystem::without_init(config);
|
||||
let init_future = system.perform_initialization(access);
|
||||
(system, init_future)
|
||||
}
|
||||
|
||||
/// Same as `start`, but doesn't perform the initialization process.
|
||||
pub fn without_init(config: KadSystemConfig<impl Iterator<Item = PeerId>>) -> KadSystem {
|
||||
let kbuckets = KBucketsTable::new(config.local_peer_id.clone(), config.kbuckets_timeout);
|
||||
for peer in config.known_initial_peers {
|
||||
let _ = kbuckets.update(peer, ());
|
||||
}
|
||||
|
||||
let system = KadSystem {
|
||||
kbuckets: kbuckets,
|
||||
parallelism: config.parallelism,
|
||||
request_timeout: config.request_timeout,
|
||||
};
|
||||
|
||||
system
|
||||
}
|
||||
|
||||
/// Starts an initialization process.
|
||||
pub fn perform_initialization<'a, F, Fut>(&self, access: F) -> impl Future<Item = (), Error = IoError> + 'a
|
||||
where F: FnMut(&PeerId) -> Fut + Clone + 'a,
|
||||
Fut: IntoFuture<Item = KadConnecController, Error = IoError> + 'a,
|
||||
{
|
||||
let futures: Vec<_> = (0..256) // TODO: 256 is arbitrary
|
||||
.map(|n| {
|
||||
refresh(n, access.clone(), &self.kbuckets,
|
||||
self.parallelism as usize, self.request_timeout)
|
||||
})
|
||||
.map(|stream| stream.for_each(|_| Ok(())))
|
||||
.collect();
|
||||
|
||||
future::loop_fn(futures, |futures| {
|
||||
if futures.is_empty() {
|
||||
let fut = future::ok(future::Loop::Break(()));
|
||||
return future::Either::A(fut);
|
||||
}
|
||||
|
||||
let fut = future::select_all(futures)
|
||||
.map_err(|(err, _, _)| err)
|
||||
.map(|(_, _, rest)| future::Loop::Continue(rest));
|
||||
future::Either::B(fut)
|
||||
})
|
||||
}
|
||||
|
||||
/// Updates the k-buckets with the specific peer.
|
||||
///
|
||||
/// Should be called whenever we receive a message from a peer.
|
||||
pub fn update_kbuckets(&self, peer: PeerId) {
|
||||
// TODO: ping system
|
||||
let _ = self.kbuckets.update(peer, ());
|
||||
}
|
||||
|
||||
/// Returns the local peer ID, as passed in the configuration.
|
||||
pub fn local_peer_id(&self) -> &PeerId {
|
||||
self.kbuckets.my_id()
|
||||
}
|
||||
|
||||
/// Finds the known nodes closest to `id`, ordered by distance.
|
||||
pub fn known_closest_peers(&self, id: &PeerId) -> impl Iterator<Item = PeerId> {
|
||||
self.kbuckets.find_closest_with_self(id)
|
||||
}
|
||||
|
||||
/// Starts a query for an iterative `FIND_NODE` request.
|
||||
pub fn find_node<'a, F, Fut>(&self, searched_key: PeerId, access: F)
|
||||
-> impl Stream<Item = KadQueryEvent<Vec<PeerId>>, Error = IoError> + 'a
|
||||
where F: FnMut(&PeerId) -> Fut + 'a,
|
||||
Fut: IntoFuture<Item = KadConnecController, Error = IoError> + 'a,
|
||||
{
|
||||
query(access, &self.kbuckets, searched_key, self.parallelism as usize,
|
||||
20, self.request_timeout) // TODO: arbitrary const
|
||||
}
|
||||
}
|
||||
|
||||
// Refreshes a specific bucket by performing an iterative `FIND_NODE` on a random ID of this
|
||||
// bucket.
|
||||
//
|
||||
// Returns a dummy no-op future if `bucket_num` is out of range.
|
||||
fn refresh<'a, F, Fut>(bucket_num: usize, access: F, kbuckets: &KBucketsTable<PeerId, ()>,
|
||||
parallelism: usize, request_timeout: Duration)
|
||||
-> impl Stream<Item = KadQueryEvent<()>, Error = IoError> + 'a
|
||||
where F: FnMut(&PeerId) -> Fut + 'a,
|
||||
Fut: IntoFuture<Item = KadConnecController, Error = IoError> + 'a,
|
||||
{
|
||||
let peer_id = match gen_random_id(kbuckets.my_id(), bucket_num) {
|
||||
Ok(p) => p,
|
||||
Err(()) => {
|
||||
let stream = stream::once(Ok(KadQueryEvent::Finished(())));
|
||||
return Box::new(stream) as Box<Stream<Item = _, Error = _>>;
|
||||
},
|
||||
};
|
||||
|
||||
let stream = query(access, kbuckets, peer_id, parallelism, 20, request_timeout) // TODO: 20 is arbitrary
|
||||
.map(|event| {
|
||||
match event {
|
||||
KadQueryEvent::NewKnownMultiaddrs(peers) => KadQueryEvent::NewKnownMultiaddrs(peers),
|
||||
KadQueryEvent::Finished(_) => KadQueryEvent::Finished(()),
|
||||
}
|
||||
});
|
||||
Box::new(stream) as Box<Stream<Item = _, Error = _>>
|
||||
}
|
||||
|
||||
// Generates a random `PeerId` that belongs to the given bucket.
|
||||
//
|
||||
// Returns an error if `bucket_num` is out of range.
|
||||
fn gen_random_id(my_id: &PeerId, bucket_num: usize) -> Result<PeerId, ()> {
|
||||
let my_id_len = my_id.as_bytes().len();
|
||||
|
||||
// TODO: this 2 is magic here ; it is the length of the hash of the multihash
|
||||
let bits_diff = bucket_num + 1;
|
||||
if bits_diff > 8 * (my_id_len - 2) {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let mut random_id = [0; 64];
|
||||
for byte in 0..my_id_len {
|
||||
match byte.cmp(&(my_id_len - bits_diff / 8 - 1)) {
|
||||
Ordering::Less => {
|
||||
random_id[byte] = my_id.as_bytes()[byte];
|
||||
}
|
||||
Ordering::Equal => {
|
||||
let mask: u8 = (1 << (bits_diff % 8)) - 1;
|
||||
random_id[byte] = (my_id.as_bytes()[byte] & !mask) | (rand::random::<u8>() & mask);
|
||||
}
|
||||
Ordering::Greater => {
|
||||
random_id[byte] = rand::random();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let peer_id = PeerId::from_bytes(random_id[..my_id_len].to_owned())
|
||||
.expect("randomly-generated peer ID should always be valid");
|
||||
Ok(peer_id)
|
||||
}
|
||||
|
||||
// Generic query-performing function.
|
||||
fn query<'a, F, Fut>(
|
||||
access: F,
|
||||
kbuckets: &KBucketsTable<PeerId, ()>,
|
||||
searched_key: PeerId,
|
||||
parallelism: usize,
|
||||
num_results: usize,
|
||||
request_timeout: Duration,
|
||||
) -> impl Stream<Item = KadQueryEvent<Vec<PeerId>>, Error = IoError> + 'a
|
||||
where F: FnMut(&PeerId) -> Fut + 'a,
|
||||
Fut: IntoFuture<Item = KadConnecController, Error = IoError> + 'a,
|
||||
{
|
||||
debug!("Start query for {:?} ; num results = {}", searched_key, num_results);
|
||||
|
||||
// State of the current iterative process.
|
||||
struct State<'a, F> {
|
||||
// At which stage we are.
|
||||
stage: Stage,
|
||||
// The `access` parameter.
|
||||
access: F,
|
||||
// Final output of the iteration.
|
||||
result: Vec<PeerId>,
|
||||
// For each open connection, a future with the response of the remote.
|
||||
// Note that don't use a `SmallVec` here because `select_all` produces a `Vec`.
|
||||
current_attempts_fut: Vec<Box<Future<Item = Vec<protocol::KadPeer>, Error = IoError> + 'a>>,
|
||||
// For each open connection, the peer ID that we are connected to.
|
||||
// Must always have the same length as `current_attempts_fut`.
|
||||
current_attempts_addrs: SmallVec<[PeerId; 32]>,
|
||||
// Nodes that need to be attempted.
|
||||
pending_nodes: Vec<PeerId>,
|
||||
// Peers that we tried to contact but failed.
|
||||
failed_to_contact: FnvHashSet<PeerId>,
|
||||
}
|
||||
|
||||
// General stage of the state.
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
enum Stage {
|
||||
// We are still in the first step of the algorithm where we try to find the closest node.
|
||||
FirstStep,
|
||||
// We are contacting the k closest nodes in order to fill the list with enough results.
|
||||
SecondStep,
|
||||
// The results are complete, and the next stream iteration will produce the outcome.
|
||||
FinishingNextIter,
|
||||
// We are finished and the stream shouldn't return anything anymore.
|
||||
Finished,
|
||||
}
|
||||
|
||||
let initial_state = State {
|
||||
stage: Stage::FirstStep,
|
||||
access: access,
|
||||
result: Vec::with_capacity(num_results),
|
||||
current_attempts_fut: Vec::new(),
|
||||
current_attempts_addrs: SmallVec::new(),
|
||||
pending_nodes: kbuckets.find_closest(&searched_key).collect(),
|
||||
failed_to_contact: Default::default(),
|
||||
};
|
||||
|
||||
// Start of the iterative process.
|
||||
let stream = stream::unfold(initial_state, move |mut state| -> Option<_> {
|
||||
match state.stage {
|
||||
Stage::FinishingNextIter => {
|
||||
let result = mem::replace(&mut state.result, Vec::new());
|
||||
debug!("Query finished with {} results", result.len());
|
||||
state.stage = Stage::Finished;
|
||||
let future = future::ok((Some(KadQueryEvent::Finished(result)), state));
|
||||
return Some(future::Either::A(future));
|
||||
},
|
||||
Stage::Finished => {
|
||||
return None;
|
||||
},
|
||||
_ => ()
|
||||
};
|
||||
|
||||
let searched_key = searched_key.clone();
|
||||
|
||||
// Find out which nodes to contact at this iteration.
|
||||
let to_contact = {
|
||||
let wanted_len = if state.stage == Stage::FirstStep {
|
||||
parallelism.saturating_sub(state.current_attempts_fut.len())
|
||||
} else {
|
||||
num_results.saturating_sub(state.current_attempts_fut.len())
|
||||
};
|
||||
let mut to_contact = SmallVec::<[_; 16]>::new();
|
||||
while to_contact.len() < wanted_len && !state.pending_nodes.is_empty() {
|
||||
// Move the first element of `pending_nodes` to `to_contact`, but ignore nodes that
|
||||
// are already part of the results or of a current attempt or if we failed to
|
||||
// contact it before.
|
||||
let peer = state.pending_nodes.remove(0);
|
||||
if state.result.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
if state.current_attempts_addrs.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
if state.failed_to_contact.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
to_contact.push(peer);
|
||||
}
|
||||
to_contact
|
||||
};
|
||||
|
||||
debug!("New query round ; {} queries in progress ; contacting {} new peers",
|
||||
state.current_attempts_fut.len(),
|
||||
to_contact.len());
|
||||
|
||||
// For each node in `to_contact`, start an RPC query and a corresponding entry in the two
|
||||
// `state.current_attempts_*` fields.
|
||||
for peer in to_contact {
|
||||
let searched_key2 = searched_key.clone();
|
||||
let current_attempt = (state.access)(&peer)
|
||||
.into_future()
|
||||
.and_then(move |controller| {
|
||||
controller.find_node(&searched_key2)
|
||||
});
|
||||
let with_deadline = Timeout::new(current_attempt, request_timeout)
|
||||
.map_err(|err| {
|
||||
if let Some(err) = err.into_inner() {
|
||||
err
|
||||
} else {
|
||||
IoError::new(IoErrorKind::ConnectionAborted, "kademlia request timeout")
|
||||
}
|
||||
});
|
||||
state.current_attempts_addrs.push(peer.clone());
|
||||
state
|
||||
.current_attempts_fut
|
||||
.push(Box::new(with_deadline) as Box<_>);
|
||||
}
|
||||
debug_assert_eq!(
|
||||
state.current_attempts_addrs.len(),
|
||||
state.current_attempts_fut.len()
|
||||
);
|
||||
|
||||
// Extract `current_attempts_fut` so that we can pass it to `select_all`. We will push the
|
||||
// values back when inside the loop.
|
||||
let current_attempts_fut = mem::replace(&mut state.current_attempts_fut, Vec::new());
|
||||
if current_attempts_fut.is_empty() {
|
||||
// If `current_attempts_fut` is empty, then `select_all` would panic. It happens
|
||||
// when we have no additional node to query.
|
||||
debug!("Finishing query early because no additional node available");
|
||||
state.stage = Stage::FinishingNextIter;
|
||||
let future = future::ok((None, state));
|
||||
return Some(future::Either::A(future));
|
||||
}
|
||||
|
||||
// This is the future that continues or breaks the `loop_fn`.
|
||||
let future = future::select_all(current_attempts_fut.into_iter()).then(move |result| {
|
||||
let (message, trigger_idx, other_current_attempts) = match result {
|
||||
Err((err, trigger_idx, other_current_attempts)) => {
|
||||
(Err(err), trigger_idx, other_current_attempts)
|
||||
}
|
||||
Ok((message, trigger_idx, other_current_attempts)) => {
|
||||
(Ok(message), trigger_idx, other_current_attempts)
|
||||
}
|
||||
};
|
||||
|
||||
// Putting back the extracted elements in `state`.
|
||||
let remote_id = state.current_attempts_addrs.remove(trigger_idx);
|
||||
debug_assert!(state.current_attempts_fut.is_empty());
|
||||
state.current_attempts_fut = other_current_attempts;
|
||||
|
||||
// `message` contains the reason why the current future was woken up.
|
||||
let closer_peers = match message {
|
||||
Ok(msg) => msg,
|
||||
Err(err) => {
|
||||
trace!("RPC query failed for {:?}: {:?}", remote_id, err);
|
||||
state.failed_to_contact.insert(remote_id);
|
||||
return future::ok((None, state));
|
||||
}
|
||||
};
|
||||
|
||||
// Inserting the node we received a response from into `state.result`.
|
||||
// The code is non-trivial because `state.result` is ordered by distance and is limited
|
||||
// by `num_results` elements.
|
||||
if let Some(insert_pos) = state.result.iter().position(|e| {
|
||||
e.distance_with(&searched_key) >= remote_id.distance_with(&searched_key)
|
||||
}) {
|
||||
if state.result[insert_pos] != remote_id {
|
||||
if state.result.len() >= num_results {
|
||||
state.result.pop();
|
||||
}
|
||||
state.result.insert(insert_pos, remote_id);
|
||||
}
|
||||
} else if state.result.len() < num_results {
|
||||
state.result.push(remote_id);
|
||||
}
|
||||
|
||||
// The loop below will set this variable to `true` if we find a new element to put at
|
||||
// the top of the result. This would mean that we have to continue looping.
|
||||
let mut local_nearest_node_updated = false;
|
||||
|
||||
// Update `state` with the actual content of the message.
|
||||
let mut new_known_multiaddrs = Vec::with_capacity(closer_peers.len());
|
||||
for mut peer in closer_peers {
|
||||
// Update the peerstore with the information sent by
|
||||
// the remote.
|
||||
{
|
||||
let multiaddrs = mem::replace(&mut peer.multiaddrs, Vec::new());
|
||||
trace!("Reporting multiaddresses for {:?}: {:?}", peer.node_id, multiaddrs);
|
||||
new_known_multiaddrs.push((peer.node_id.clone(), multiaddrs));
|
||||
}
|
||||
|
||||
if peer.node_id.distance_with(&searched_key)
|
||||
<= state.result[0].distance_with(&searched_key)
|
||||
{
|
||||
local_nearest_node_updated = true;
|
||||
}
|
||||
|
||||
if state.result.iter().any(|ma| ma == &peer.node_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Insert the node into `pending_nodes` at the right position, or do not
|
||||
// insert it if it is already in there.
|
||||
if let Some(insert_pos) = state.pending_nodes.iter().position(|e| {
|
||||
e.distance_with(&searched_key) >= peer.node_id.distance_with(&searched_key)
|
||||
}) {
|
||||
if state.pending_nodes[insert_pos] != peer.node_id {
|
||||
state.pending_nodes.insert(insert_pos, peer.node_id.clone());
|
||||
}
|
||||
} else {
|
||||
state.pending_nodes.push(peer.node_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if state.result.len() >= num_results
|
||||
|| (state.stage != Stage::FirstStep && state.current_attempts_fut.is_empty())
|
||||
{
|
||||
state.stage = Stage::FinishingNextIter;
|
||||
|
||||
} else {
|
||||
if !local_nearest_node_updated {
|
||||
trace!("Loop didn't update closer node ; jumping to step 2");
|
||||
state.stage = Stage::SecondStep;
|
||||
}
|
||||
}
|
||||
|
||||
future::ok((Some(KadQueryEvent::NewKnownMultiaddrs(new_known_multiaddrs)), state))
|
||||
});
|
||||
|
||||
Some(future::Either::B(future))
|
||||
}).filter_map(|val| val);
|
||||
|
||||
// Boxing the stream is not necessary, but we do it in order to improve compilation time.
|
||||
Box::new(stream) as Box<_>
|
||||
}
|
508
protocols/kad/src/kad_server.rs
Normal file
508
protocols/kad/src/kad_server.rs
Normal file
@ -0,0 +1,508 @@
|
||||
// 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.
|
||||
|
||||
//! Contains a `ConnectionUpgrade` that makes it possible to send requests and receive responses
|
||||
//! from nodes after the upgrade.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! - Create a `KadConnecConfig` object. This struct implements `ConnectionUpgrade`.
|
||||
//!
|
||||
//! - Update a connection through that `KadConnecConfig`. The output yields you a
|
||||
//! `KadConnecController` and a stream that must be driven to completion. The controller
|
||||
//! allows you to perform queries and receive responses. The stream produces incoming requests
|
||||
//! from the remote.
|
||||
//!
|
||||
//! This `KadConnecController` is usually extracted and stored in some sort of hash map in an
|
||||
//! `Arc` in order to be available whenever we need to request something from a node.
|
||||
|
||||
use bytes::Bytes;
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
use futures::{future, Future, Sink, stream, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, PeerId};
|
||||
use protocol::{self, KadMsg, KademliaProtocolConfig, KadPeer};
|
||||
use std::collections::VecDeque;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::iter;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Configuration for a Kademlia server.
|
||||
///
|
||||
/// Implements `ConnectionUpgrade`. On a successful upgrade, produces a `KadConnecController`
|
||||
/// and a `Future`. The controller lets you send queries to the remote and receive answers, while
|
||||
/// the `Future` must be driven to completion in order for things to work.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KadConnecConfig {
|
||||
raw_proto: KademliaProtocolConfig,
|
||||
}
|
||||
|
||||
impl KadConnecConfig {
|
||||
/// Builds a configuration object for an upcoming Kademlia server.
|
||||
#[inline]
|
||||
pub fn new() -> Self {
|
||||
KadConnecConfig {
|
||||
raw_proto: KademliaProtocolConfig,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C, Maf> ConnectionUpgrade<C, Maf> for KadConnecConfig
|
||||
where
|
||||
C: AsyncRead + AsyncWrite + 'static, // TODO: 'static :-/
|
||||
{
|
||||
type Output = (
|
||||
KadConnecController,
|
||||
Box<Stream<Item = KadIncomingRequest, Error = IoError>>,
|
||||
);
|
||||
type MultiaddrFuture = Maf;
|
||||
type Future = future::Map<<KademliaProtocolConfig as ConnectionUpgrade<C, Maf>>::Future, fn((<KademliaProtocolConfig as ConnectionUpgrade<C, Maf>>::Output, Maf)) -> (Self::Output, Maf)>;
|
||||
type NamesIter = iter::Once<(Bytes, ())>;
|
||||
type UpgradeIdentifier = ();
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
ConnectionUpgrade::<C, Maf>::protocol_names(&self.raw_proto)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn upgrade(self, incoming: C, id: (), endpoint: Endpoint, addr: Maf) -> Self::Future {
|
||||
self.raw_proto
|
||||
.upgrade(incoming, id, endpoint, addr)
|
||||
.map::<fn(_) -> _, _>(move |(connec, addr)| {
|
||||
(build_from_sink_stream(connec), addr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows sending Kademlia requests and receiving responses.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct KadConnecController {
|
||||
// In order to send a request, we use this sender to send a tuple. The first element of the
|
||||
// tuple is the message to send to the remote, and the second element is what is used to
|
||||
// receive the response. If the query doesn't expect a response (eg. `PUT_VALUE`), then the
|
||||
// one-shot sender will be dropped without being used.
|
||||
inner: mpsc::UnboundedSender<(KadMsg, oneshot::Sender<KadMsg>)>,
|
||||
}
|
||||
|
||||
impl KadConnecController {
|
||||
/// Sends a `FIND_NODE` query to the node and provides a future that will contain the response.
|
||||
// TODO: future item could be `impl Iterator` instead
|
||||
pub fn find_node(
|
||||
&self,
|
||||
searched_key: &PeerId,
|
||||
) -> impl Future<Item = Vec<KadPeer>, Error = IoError> {
|
||||
let message = protocol::KadMsg::FindNodeReq {
|
||||
key: searched_key.clone().into_bytes(),
|
||||
};
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
match self.inner.unbounded_send((message, tx)) {
|
||||
Ok(()) => (),
|
||||
Err(_) => {
|
||||
let fut = future::err(IoError::new(
|
||||
IoErrorKind::ConnectionAborted,
|
||||
"connection to remote has aborted",
|
||||
));
|
||||
|
||||
return future::Either::B(fut);
|
||||
}
|
||||
};
|
||||
|
||||
let future = rx.map_err(|_| {
|
||||
IoError::new(
|
||||
IoErrorKind::ConnectionAborted,
|
||||
"connection to remote has aborted",
|
||||
)
|
||||
}).and_then(|msg| match msg {
|
||||
KadMsg::FindNodeRes { closer_peers, .. } => Ok(closer_peers),
|
||||
_ => Err(IoError::new(
|
||||
IoErrorKind::InvalidData,
|
||||
"invalid response type received from the remote",
|
||||
)),
|
||||
});
|
||||
|
||||
future::Either::A(future)
|
||||
}
|
||||
|
||||
/// Sends a `PING` query to the node. Because of the way the protocol is designed, there is
|
||||
/// no way to differentiate between a ping and a pong. Therefore this function doesn't return a
|
||||
/// future, and the only way to be notified of the result is through the stream.
|
||||
pub fn ping(&self) -> Result<(), IoError> {
|
||||
// Dummy channel, as the `tx` is going to be dropped anyway.
|
||||
let (tx, _rx) = oneshot::channel();
|
||||
match self.inner.unbounded_send((protocol::KadMsg::Ping, tx)) {
|
||||
Ok(()) => Ok(()),
|
||||
Err(_) => Err(IoError::new(
|
||||
IoErrorKind::ConnectionAborted,
|
||||
"connection to remote has aborted",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Request received from the remote.
|
||||
pub enum KadIncomingRequest {
|
||||
/// Find the nodes closest to `searched`.
|
||||
FindNode {
|
||||
/// The value being searched.
|
||||
searched: PeerId,
|
||||
/// Object to use to respond to the request.
|
||||
responder: KadFindNodeRespond,
|
||||
},
|
||||
|
||||
// TODO: PutValue and FindValue
|
||||
|
||||
/// Received either a ping or a pong.
|
||||
PingPong,
|
||||
}
|
||||
|
||||
/// Object used to respond to `FindNode` queries from remotes.
|
||||
pub struct KadFindNodeRespond {
|
||||
inner: oneshot::Sender<KadMsg>,
|
||||
}
|
||||
|
||||
impl KadFindNodeRespond {
|
||||
/// Respond to the `FindNode` request.
|
||||
pub fn respond<I>(self, peers: I)
|
||||
where I: IntoIterator<Item = protocol::KadPeer>
|
||||
{
|
||||
let _ = self.inner.send(KadMsg::FindNodeRes {
|
||||
closer_peers: peers.into_iter().collect()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Builds a controller and stream from a stream/sink of raw messages.
|
||||
fn build_from_sink_stream<'a, S>(connec: S) -> (KadConnecController, Box<Stream<Item = KadIncomingRequest, Error = IoError> + 'a>)
|
||||
where S: Sink<SinkItem = KadMsg, SinkError = IoError> + Stream<Item = KadMsg, Error = IoError> + 'a
|
||||
{
|
||||
let (tx, rx) = mpsc::unbounded();
|
||||
let future = kademlia_handler(connec, rx);
|
||||
let controller = KadConnecController { inner: tx };
|
||||
(controller, future)
|
||||
}
|
||||
|
||||
// Handles a newly-opened Kademlia stream with a remote peer.
|
||||
//
|
||||
// Takes a `Stream` and `Sink` of Kademlia messages representing the connection to the client,
|
||||
// plus a `Receiver` that will receive messages to transmit to that connection.
|
||||
//
|
||||
// Returns a `Stream` that must be resolved in order for progress to work. The `Stream` will
|
||||
// produce objects that represent the requests sent by the remote. These requests must be answered
|
||||
// immediately before the stream continues to produce items.
|
||||
fn kademlia_handler<'a, S>(
|
||||
kad_bistream: S,
|
||||
rq_rx: mpsc::UnboundedReceiver<(KadMsg, oneshot::Sender<KadMsg>)>,
|
||||
) -> Box<Stream<Item = KadIncomingRequest, Error = IoError> + 'a>
|
||||
where
|
||||
S: Stream<Item = KadMsg, Error = IoError> + Sink<SinkItem = KadMsg, SinkError = IoError> + 'a,
|
||||
{
|
||||
let (kad_sink, kad_stream) = kad_bistream.split();
|
||||
|
||||
// This is a stream of futures containing local responses.
|
||||
// Every time we receive a request from the remote, we create a `oneshot::channel()` and send
|
||||
// the receiving end to `responders_tx`.
|
||||
// This way, if a future is available on `responders_rx`, we block until it produces the
|
||||
// response.
|
||||
let (responders_tx, responders_rx) = mpsc::unbounded();
|
||||
|
||||
// We combine all the streams into one so that the loop wakes up whenever any generates
|
||||
// something.
|
||||
enum EventSource {
|
||||
Remote(KadMsg),
|
||||
LocalRequest(KadMsg, oneshot::Sender<KadMsg>),
|
||||
LocalResponse(oneshot::Receiver<KadMsg>),
|
||||
Finished,
|
||||
}
|
||||
|
||||
let events = {
|
||||
let responders = responders_rx
|
||||
.map(|m| EventSource::LocalResponse(m))
|
||||
.map_err(|_| unreachable!());
|
||||
let rq_rx = rq_rx
|
||||
.map(|(m, o)| EventSource::LocalRequest(m, o))
|
||||
.map_err(|_| unreachable!());
|
||||
let kad_stream = kad_stream
|
||||
.map(|m| EventSource::Remote(m))
|
||||
.chain(future::ok(EventSource::Finished).into_stream());
|
||||
responders.select(rq_rx).select(kad_stream)
|
||||
};
|
||||
|
||||
let stream = stream::unfold((events, kad_sink, responders_tx, VecDeque::new(), 0u32, false),
|
||||
move |(events, kad_sink, responders_tx, mut send_back_queue, expected_pongs, finished)| {
|
||||
if finished {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(events
|
||||
.into_future()
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(move |(message, events)| -> Box<Future<Item = _, Error = _>> {
|
||||
match message {
|
||||
Some(EventSource::Finished) | None => {
|
||||
let future = future::ok({
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, true);
|
||||
(None, state)
|
||||
});
|
||||
Box::new(future)
|
||||
},
|
||||
Some(EventSource::LocalResponse(message)) => {
|
||||
let future = message
|
||||
.map_err(|_| {
|
||||
// The user destroyed the responder without responding.
|
||||
warn!("Kad responder object destroyed without responding");
|
||||
panic!() // TODO: what to do here? we have to close the connection
|
||||
})
|
||||
.and_then(move |message| {
|
||||
kad_sink
|
||||
.send(message)
|
||||
.map(move |kad_sink| {
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
(None, state)
|
||||
})
|
||||
});
|
||||
Box::new(future)
|
||||
},
|
||||
Some(EventSource::LocalRequest(message @ KadMsg::PutValue { .. }, _)) => {
|
||||
// A `PutValue` request. Contrary to other types of messages, this one
|
||||
// doesn't expect any answer and therefore we ignore the sender.
|
||||
let future = kad_sink
|
||||
.send(message)
|
||||
.map(move |kad_sink| {
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
(None, state)
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
Some(EventSource::LocalRequest(message @ KadMsg::Ping { .. }, _)) => {
|
||||
// A local `Ping` request.
|
||||
let expected_pongs = expected_pongs.checked_add(1)
|
||||
.expect("overflow in number of simultaneous pings");
|
||||
let future = kad_sink
|
||||
.send(message)
|
||||
.map(move |kad_sink| {
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
(None, state)
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
Some(EventSource::LocalRequest(message, send_back)) => {
|
||||
// Any local request other than `PutValue` or `Ping`.
|
||||
send_back_queue.push_back(send_back);
|
||||
let future = kad_sink
|
||||
.send(message)
|
||||
.map(move |kad_sink| {
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
(None, state)
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
Some(EventSource::Remote(KadMsg::Ping)) => {
|
||||
// The way the protocol was designed, there is no way to differentiate
|
||||
// between a ping and a pong.
|
||||
if let Some(expected_pongs) = expected_pongs.checked_sub(1) {
|
||||
// Maybe we received a PONG, or maybe we received a PONG, no way
|
||||
// to tell. If it was a PING and we expected a PONG, then the
|
||||
// remote will see its PING answered only when it PONGs us.
|
||||
let future = future::ok({
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
let rq = KadIncomingRequest::PingPong;
|
||||
(Some(rq), state)
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
} else {
|
||||
let future = kad_sink
|
||||
.send(KadMsg::Ping)
|
||||
.map(move |kad_sink| {
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
let rq = KadIncomingRequest::PingPong;
|
||||
(Some(rq), state)
|
||||
});
|
||||
Box::new(future) as Box<_>
|
||||
}
|
||||
}
|
||||
Some(EventSource::Remote(message @ KadMsg::FindNodeRes { .. }))
|
||||
| Some(EventSource::Remote(message @ KadMsg::GetValueRes { .. })) => {
|
||||
// `FindNodeRes` or `GetValueRes` received on the socket.
|
||||
// Send it back through `send_back_queue`.
|
||||
if let Some(send_back) = send_back_queue.pop_front() {
|
||||
let _ = send_back.send(message);
|
||||
let future = future::ok({
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
(None, state)
|
||||
});
|
||||
Box::new(future)
|
||||
} else {
|
||||
debug!("Remote sent a Kad response but we didn't request anything");
|
||||
let future = future::err(IoErrorKind::InvalidData.into());
|
||||
Box::new(future)
|
||||
}
|
||||
}
|
||||
Some(EventSource::Remote(KadMsg::FindNodeReq { key, .. })) => {
|
||||
let peer_id = match PeerId::from_bytes(key) {
|
||||
Ok(id) => id,
|
||||
Err(key) => {
|
||||
debug!("Ignoring FIND_NODE request with invalid key: {:?}", key);
|
||||
let future = future::err(IoError::new(IoErrorKind::InvalidData, "invalid key in FIND_NODE"));
|
||||
return Box::new(future);
|
||||
}
|
||||
};
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = responders_tx.unbounded_send(rx);
|
||||
let future = future::ok({
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
let rq = KadIncomingRequest::FindNode {
|
||||
searched: peer_id,
|
||||
responder: KadFindNodeRespond {
|
||||
inner: tx
|
||||
}
|
||||
};
|
||||
(Some(rq), state)
|
||||
});
|
||||
|
||||
Box::new(future)
|
||||
}
|
||||
Some(EventSource::Remote(KadMsg::GetValueReq { .. })) => {
|
||||
warn!("GET_VALUE requests are not implemented yet");
|
||||
let future = future::err(IoError::new(IoErrorKind::Other,
|
||||
"GET_VALUE requests are not implemented yet"));
|
||||
return Box::new(future);
|
||||
}
|
||||
Some(EventSource::Remote(KadMsg::PutValue { .. })) => {
|
||||
warn!("PUT_VALUE requests are not implemented yet");
|
||||
let state = (events, kad_sink, responders_tx, send_back_queue, expected_pongs, finished);
|
||||
let future = future::ok((None, state));
|
||||
return Box::new(future);
|
||||
}
|
||||
}
|
||||
}))
|
||||
}).filter_map(|val| val);
|
||||
|
||||
Box::new(stream) as Box<Stream<Item = _, Error = IoError>>
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||
use futures::sync::mpsc;
|
||||
use kad_server::{self, KadIncomingRequest, KadConnecController};
|
||||
use libp2p_core::PublicKey;
|
||||
use protocol::{KadConnectionType, KadPeer};
|
||||
use rand;
|
||||
|
||||
// This struct merges a stream and a sink and is quite useful for tests.
|
||||
struct Wrapper<St, Si>(St, Si);
|
||||
impl<St, Si> Stream for Wrapper<St, Si>
|
||||
where
|
||||
St: Stream,
|
||||
{
|
||||
type Item = St::Item;
|
||||
type Error = St::Error;
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
self.0.poll()
|
||||
}
|
||||
}
|
||||
impl<St, Si> Sink for Wrapper<St, Si>
|
||||
where
|
||||
Si: Sink,
|
||||
{
|
||||
type SinkItem = Si::SinkItem;
|
||||
type SinkError = Si::SinkError;
|
||||
fn start_send(
|
||||
&mut self,
|
||||
item: Self::SinkItem,
|
||||
) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||
self.1.start_send(item)
|
||||
}
|
||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
||||
self.1.poll_complete()
|
||||
}
|
||||
}
|
||||
|
||||
fn build_test() -> (KadConnecController, impl Stream<Item = KadIncomingRequest, Error = IoError>, KadConnecController, impl Stream<Item = KadIncomingRequest, Error = IoError>) {
|
||||
let (a_to_b, b_from_a) = mpsc::unbounded();
|
||||
let (b_to_a, a_from_b) = mpsc::unbounded();
|
||||
|
||||
let sink_stream_a = Wrapper(a_from_b, a_to_b)
|
||||
.map_err(|_| panic!()).sink_map_err(|_| panic!());
|
||||
let sink_stream_b = Wrapper(b_from_a, b_to_a)
|
||||
.map_err(|_| panic!()).sink_map_err(|_| panic!());
|
||||
|
||||
let (controller_a, stream_events_a) = kad_server::build_from_sink_stream(sink_stream_a);
|
||||
let (controller_b, stream_events_b) = kad_server::build_from_sink_stream(sink_stream_b);
|
||||
(controller_a, stream_events_a, controller_b, stream_events_b)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ping_response() {
|
||||
let (controller_a, stream_events_a, _controller_b, stream_events_b) = build_test();
|
||||
|
||||
controller_a.ping().unwrap();
|
||||
|
||||
let streams = stream_events_a.map(|ev| (ev, "a"))
|
||||
.select(stream_events_b.map(|ev| (ev, "b")));
|
||||
match streams.into_future().map_err(|(err, _)| err).wait().unwrap() {
|
||||
(Some((KadIncomingRequest::PingPong, "b")), _) => {},
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_node_response() {
|
||||
let (controller_a, stream_events_a, _controller_b, stream_events_b) = build_test();
|
||||
|
||||
let random_peer_id = {
|
||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
||||
PublicKey::Rsa(buf).into_peer_id()
|
||||
};
|
||||
|
||||
let find_node_fut = controller_a.find_node(&random_peer_id);
|
||||
|
||||
let example_response = KadPeer {
|
||||
node_id: {
|
||||
let buf = (0 .. 1024).map(|_| -> u8 { rand::random() }).collect::<Vec<_>>();
|
||||
PublicKey::Rsa(buf).into_peer_id()
|
||||
},
|
||||
multiaddrs: Vec::new(),
|
||||
connection_ty: KadConnectionType::Connected,
|
||||
};
|
||||
|
||||
let streams = stream_events_a.map(|ev| (ev, "a"))
|
||||
.select(stream_events_b.map(|ev| (ev, "b")));
|
||||
|
||||
let streams = match streams.into_future().map_err(|(err, _)| err).wait().unwrap() {
|
||||
(Some((KadIncomingRequest::FindNode { searched, responder }, "b")), streams) => {
|
||||
assert_eq!(searched, random_peer_id);
|
||||
responder.respond(iter::once(example_response.clone()));
|
||||
streams
|
||||
},
|
||||
_ => panic!()
|
||||
};
|
||||
|
||||
let resp = streams.into_future().map_err(|(err, _)| err).map(|_| unreachable!())
|
||||
.select(find_node_fut)
|
||||
.map_err(|_| -> IoError { panic!() });
|
||||
assert_eq!(resp.wait().unwrap().0, vec![example_response]);
|
||||
}
|
||||
}
|
498
protocols/kad/src/kbucket.rs
Normal file
498
protocols/kad/src/kbucket.rs
Normal file
@ -0,0 +1,498 @@
|
||||
// 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.
|
||||
|
||||
//! Key-value storage, with a refresh and a time-to-live system.
|
||||
//!
|
||||
//! A k-buckets table allows one to store a value identified by keys, ordered by their distance
|
||||
//! to a reference key passed to the constructor.
|
||||
//!
|
||||
//! If the local ID has `N` bits, then the k-buckets table contains `N` *buckets* each containing
|
||||
//! a constant number of entries. Storing a key in the k-buckets table adds it to the bucket
|
||||
//! corresponding to its distance with the reference key.
|
||||
|
||||
use arrayvec::ArrayVec;
|
||||
use bigint::U512;
|
||||
use libp2p_core::PeerId;
|
||||
use parking_lot::{Mutex, MutexGuard};
|
||||
use std::mem;
|
||||
use std::slice::Iter as SliceIter;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::vec::IntoIter as VecIntoIter;
|
||||
|
||||
/// Maximum number of nodes in a bucket.
|
||||
pub const MAX_NODES_PER_BUCKET: usize = 20;
|
||||
|
||||
/// Table of k-buckets with interior mutability.
|
||||
#[derive(Debug)]
|
||||
pub struct KBucketsTable<Id, Val> {
|
||||
my_id: Id,
|
||||
tables: Vec<Mutex<KBucket<Id, Val>>>,
|
||||
// The timeout when pinging the first node after which we consider that it no longer responds.
|
||||
ping_timeout: Duration,
|
||||
}
|
||||
|
||||
impl<Id, Val> Clone for KBucketsTable<Id, Val>
|
||||
where
|
||||
Id: Clone,
|
||||
Val: Clone,
|
||||
{
|
||||
#[inline]
|
||||
fn clone(&self) -> Self {
|
||||
KBucketsTable {
|
||||
my_id: self.my_id.clone(),
|
||||
tables: self.tables
|
||||
.iter()
|
||||
.map(|t| t.lock().clone())
|
||||
.map(Mutex::new)
|
||||
.collect(),
|
||||
ping_timeout: self.ping_timeout.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct KBucket<Id, Val> {
|
||||
// Nodes are always ordered from oldest to newest.
|
||||
// Note that we will very often move elements to the end of this. No benchmarking has been
|
||||
// performed, but it is very likely that a `ArrayVec` is the most performant data structure.
|
||||
nodes: ArrayVec<[Node<Id, Val>; MAX_NODES_PER_BUCKET]>,
|
||||
|
||||
// Node received when the bucket was full. Will be added to the list if the first node doesn't
|
||||
// respond in time to our ping. The second element is the time when the pending node was added.
|
||||
// If it is too much in the past, then we drop the first node and add the pending node to the
|
||||
// end of the list.
|
||||
pending_node: Option<(Node<Id, Val>, Instant)>,
|
||||
|
||||
// Last time this bucket was updated.
|
||||
last_update: Instant,
|
||||
}
|
||||
|
||||
impl<Id, Val> KBucket<Id, Val> {
|
||||
// Puts the kbucket into a coherent state.
|
||||
// If a node is pending and the timeout has expired, removes the first element of `nodes`
|
||||
// and pushes back the node in `pending_node`.
|
||||
fn flush(&mut self, timeout: Duration) {
|
||||
if let Some((pending_node, instant)) = self.pending_node.take() {
|
||||
if instant.elapsed() >= timeout {
|
||||
let _ = self.nodes.remove(0);
|
||||
self.nodes.push(pending_node);
|
||||
} else {
|
||||
self.pending_node = Some((pending_node, instant));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Node<Id, Val> {
|
||||
id: Id,
|
||||
value: Val,
|
||||
}
|
||||
|
||||
/// Trait that must be implemented on types that can be used as an identifier in a k-bucket.
|
||||
pub trait KBucketsPeerId: Eq + Clone {
|
||||
/// Distance between two peer IDs.
|
||||
type Distance: Ord;
|
||||
|
||||
/// Computes the XOR of this value and another one.
|
||||
fn distance_with(&self, other: &Self) -> Self::Distance;
|
||||
|
||||
/// Returns then number of bits that are necessary to store the distance between peer IDs.
|
||||
/// Used for pre-allocations.
|
||||
///
|
||||
/// > **Note**: Returning 0 would lead to a panic.
|
||||
fn num_bits() -> usize;
|
||||
|
||||
/// Returns the number of leading zeroes of the distance between peer IDs.
|
||||
fn leading_zeros(Self::Distance) -> u32;
|
||||
}
|
||||
|
||||
impl KBucketsPeerId for PeerId {
|
||||
type Distance = U512;
|
||||
|
||||
#[inline]
|
||||
fn num_bits() -> usize {
|
||||
512
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn distance_with(&self, other: &Self) -> Self::Distance {
|
||||
// Note that we don't compare the hash functions because there's no chance of collision
|
||||
// of the same value hashed with two different hash functions.
|
||||
let my_hash = U512::from(self.digest());
|
||||
let other_hash = U512::from(other.digest());
|
||||
my_hash ^ other_hash
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn leading_zeros(distance: Self::Distance) -> u32 {
|
||||
distance.leading_zeros()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Id, Val> KBucketsTable<Id, Val>
|
||||
where
|
||||
Id: KBucketsPeerId,
|
||||
{
|
||||
/// Builds a new routing table.
|
||||
pub fn new(my_id: Id, ping_timeout: Duration) -> Self {
|
||||
KBucketsTable {
|
||||
my_id: my_id,
|
||||
tables: (0..Id::num_bits())
|
||||
.map(|_| KBucket {
|
||||
nodes: ArrayVec::new(),
|
||||
pending_node: None,
|
||||
last_update: Instant::now(),
|
||||
})
|
||||
.map(Mutex::new)
|
||||
.collect(),
|
||||
ping_timeout: ping_timeout,
|
||||
}
|
||||
}
|
||||
|
||||
// Returns the id of the bucket that should contain the peer with the given ID.
|
||||
//
|
||||
// Returns `None` if out of range, which happens if `id` is the same as the local peer id.
|
||||
#[inline]
|
||||
fn bucket_num(&self, id: &Id) -> Option<usize> {
|
||||
(Id::num_bits() - 1).checked_sub(Id::leading_zeros(self.my_id.distance_with(id)) as usize)
|
||||
}
|
||||
|
||||
/// Returns an iterator to all the buckets of this table.
|
||||
///
|
||||
/// Ordered by proximity to the local node. Closest bucket (with max. one node in it) comes
|
||||
/// first.
|
||||
#[inline]
|
||||
pub fn buckets(&self) -> BucketsIter<Id, Val> {
|
||||
BucketsIter(self.tables.iter(), self.ping_timeout)
|
||||
}
|
||||
|
||||
/// Returns the ID of the local node.
|
||||
#[inline]
|
||||
pub fn my_id(&self) -> &Id {
|
||||
&self.my_id
|
||||
}
|
||||
|
||||
/// Finds the `num` nodes closest to `id`, ordered by distance.
|
||||
pub fn find_closest(&self, id: &Id) -> VecIntoIter<Id>
|
||||
where
|
||||
Id: Clone,
|
||||
{
|
||||
// TODO: optimize
|
||||
let mut out = Vec::new();
|
||||
for table in self.tables.iter() {
|
||||
let mut table = table.lock();
|
||||
table.flush(self.ping_timeout);
|
||||
if table.last_update.elapsed() > self.ping_timeout {
|
||||
continue // ignore bucket with expired nodes
|
||||
}
|
||||
for node in table.nodes.iter() {
|
||||
out.push(node.id.clone());
|
||||
}
|
||||
}
|
||||
out.sort_by(|a, b| b.distance_with(id).cmp(&a.distance_with(id)));
|
||||
out.into_iter()
|
||||
}
|
||||
|
||||
/// Same as `find_closest`, but includes the local peer as well.
|
||||
pub fn find_closest_with_self(&self, id: &Id) -> VecIntoIter<Id>
|
||||
where
|
||||
Id: Clone,
|
||||
{
|
||||
// TODO: optimize
|
||||
let mut intermediate: Vec<_> = self.find_closest(&id).collect();
|
||||
if let Some(pos) = intermediate
|
||||
.iter()
|
||||
.position(|e| e.distance_with(&id) >= self.my_id.distance_with(&id))
|
||||
{
|
||||
if intermediate[pos] != self.my_id {
|
||||
intermediate.insert(pos, self.my_id.clone());
|
||||
}
|
||||
} else {
|
||||
intermediate.push(self.my_id.clone());
|
||||
}
|
||||
intermediate.into_iter()
|
||||
}
|
||||
|
||||
/// Marks the node as "most recent" in its bucket and modifies the value associated to it.
|
||||
/// This function should be called whenever we receive a communication from a node.
|
||||
pub fn update(&self, id: Id, value: Val) -> UpdateOutcome<Id, Val> {
|
||||
let table = match self.bucket_num(&id) {
|
||||
Some(n) => &self.tables[n],
|
||||
None => return UpdateOutcome::FailSelfUpdate,
|
||||
};
|
||||
|
||||
let mut table = table.lock();
|
||||
table.flush(self.ping_timeout);
|
||||
|
||||
if let Some(pos) = table.nodes.iter().position(|n| n.id == id) {
|
||||
// Node is already in the bucket.
|
||||
let mut existing = table.nodes.remove(pos);
|
||||
let old_val = mem::replace(&mut existing.value, value);
|
||||
if pos == 0 {
|
||||
// If it's the first node of the bucket that we update, then we drop the node that
|
||||
// was waiting for a ping.
|
||||
table.nodes.truncate(MAX_NODES_PER_BUCKET - 1);
|
||||
table.pending_node = None;
|
||||
}
|
||||
table.nodes.push(existing);
|
||||
table.last_update = Instant::now();
|
||||
UpdateOutcome::Refreshed(old_val)
|
||||
} else if table.nodes.len() < MAX_NODES_PER_BUCKET {
|
||||
// Node not yet in the bucket, but there's plenty of space.
|
||||
table.nodes.push(Node {
|
||||
id: id,
|
||||
value: value,
|
||||
});
|
||||
table.last_update = Instant::now();
|
||||
UpdateOutcome::Added
|
||||
} else {
|
||||
// Not enough space to put the node, but we can add it to the end as "pending". We
|
||||
// then need to tell the caller that we want it to ping the node at the top of the
|
||||
// list.
|
||||
if table.pending_node.is_none() {
|
||||
table.pending_node = Some((
|
||||
Node {
|
||||
id: id,
|
||||
value: value,
|
||||
},
|
||||
Instant::now(),
|
||||
));
|
||||
UpdateOutcome::NeedPing(table.nodes[0].id.clone())
|
||||
} else {
|
||||
UpdateOutcome::Discarded
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Return value of the `update()` method.
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
#[must_use]
|
||||
pub enum UpdateOutcome<Id, Val> {
|
||||
/// The node has been added to the bucket.
|
||||
Added,
|
||||
/// The node was already in the bucket and has been refreshed.
|
||||
Refreshed(Val),
|
||||
/// The node wasn't added. Instead we need to ping the node passed as parameter, and call
|
||||
/// `update` if it responds.
|
||||
NeedPing(Id),
|
||||
/// The node wasn't added at all because a node was already pending.
|
||||
Discarded,
|
||||
/// Tried to update the local peer ID. This is an invalid operation.
|
||||
FailSelfUpdate,
|
||||
}
|
||||
|
||||
/// Iterator giving access to a bucket.
|
||||
pub struct BucketsIter<'a, Id: 'a, Val: 'a>(SliceIter<'a, Mutex<KBucket<Id, Val>>>, Duration);
|
||||
|
||||
impl<'a, Id: 'a, Val: 'a> Iterator for BucketsIter<'a, Id, Val> {
|
||||
type Item = Bucket<'a, Id, Val>;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.0.next().map(|bucket| {
|
||||
let mut bucket = bucket.lock();
|
||||
bucket.flush(self.1);
|
||||
Bucket(bucket)
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
self.0.size_hint()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Id: 'a, Val: 'a> ExactSizeIterator for BucketsIter<'a, Id, Val> {}
|
||||
|
||||
/// Access to a bucket.
|
||||
pub struct Bucket<'a, Id: 'a, Val: 'a>(MutexGuard<'a, KBucket<Id, Val>>);
|
||||
|
||||
impl<'a, Id: 'a, Val: 'a> Bucket<'a, Id, Val> {
|
||||
/// Returns the number of entries in that bucket.
|
||||
///
|
||||
/// > **Note**: Keep in mind that this operation can be racy. If `update()` is called on the
|
||||
/// > table while this function is running, the `update()` may or may not be taken
|
||||
/// > into account.
|
||||
#[inline]
|
||||
pub fn num_entries(&self) -> usize {
|
||||
self.0.nodes.len()
|
||||
}
|
||||
|
||||
/// Returns true if this bucket has a pending node.
|
||||
#[inline]
|
||||
pub fn has_pending(&self) -> bool {
|
||||
self.0.pending_node.is_some()
|
||||
}
|
||||
|
||||
/// Returns the time when any of the values in this bucket was last updated.
|
||||
///
|
||||
/// If the bucket is empty, this returns the time when the whole table was created.
|
||||
#[inline]
|
||||
pub fn last_update(&self) -> Instant {
|
||||
self.0.last_update.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate rand;
|
||||
use self::rand::random;
|
||||
use kbucket::{KBucketsTable, UpdateOutcome, MAX_NODES_PER_BUCKET};
|
||||
use libp2p_core::PeerId;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
#[test]
|
||||
fn basic_closest() {
|
||||
let my_id = {
|
||||
let mut bytes = vec![random(); 34];
|
||||
bytes[0] = 18;
|
||||
bytes[1] = 32;
|
||||
PeerId::from_bytes(bytes).unwrap()
|
||||
};
|
||||
|
||||
let other_id = {
|
||||
let mut bytes = vec![random(); 34];
|
||||
bytes[0] = 18;
|
||||
bytes[1] = 32;
|
||||
PeerId::from_bytes(bytes).unwrap()
|
||||
};
|
||||
|
||||
let table = KBucketsTable::new(my_id, Duration::from_secs(5));
|
||||
let _ = table.update(other_id.clone(), ());
|
||||
|
||||
let res = table.find_closest(&other_id).collect::<Vec<_>>();
|
||||
assert_eq!(res.len(), 1);
|
||||
assert_eq!(res[0], other_id);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_local_id_fails() {
|
||||
let my_id = {
|
||||
let mut bytes = vec![random(); 34];
|
||||
bytes[0] = 18;
|
||||
bytes[1] = 32;
|
||||
PeerId::from_bytes(bytes).unwrap()
|
||||
};
|
||||
|
||||
let table = KBucketsTable::new(my_id.clone(), Duration::from_secs(5));
|
||||
match table.update(my_id, ()) {
|
||||
UpdateOutcome::FailSelfUpdate => (),
|
||||
_ => panic!()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_time_last_refresh() {
|
||||
let my_id = {
|
||||
let mut bytes = vec![random(); 34];
|
||||
bytes[0] = 18;
|
||||
bytes[1] = 32;
|
||||
PeerId::from_bytes(bytes).unwrap()
|
||||
};
|
||||
|
||||
// Generate some other IDs varying by just one bit.
|
||||
let other_ids = (0..random::<usize>() % 20)
|
||||
.map(|_| {
|
||||
let bit_num = random::<usize>() % 256;
|
||||
let mut id = my_id.as_bytes().to_vec().clone();
|
||||
id[33 - (bit_num / 8)] ^= 1 << (bit_num % 8);
|
||||
(PeerId::from_bytes(id).unwrap(), bit_num)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let table = KBucketsTable::new(my_id, Duration::from_secs(5));
|
||||
let before_update = table.buckets().map(|b| b.last_update()).collect::<Vec<_>>();
|
||||
|
||||
thread::sleep(Duration::from_secs(2));
|
||||
for &(ref id, _) in &other_ids {
|
||||
let _ = table.update(id.clone(), ());
|
||||
}
|
||||
|
||||
let after_update = table.buckets().map(|b| b.last_update()).collect::<Vec<_>>();
|
||||
|
||||
for (offset, (bef, aft)) in before_update.iter().zip(after_update.iter()).enumerate() {
|
||||
if other_ids.iter().any(|&(_, bucket)| bucket == offset) {
|
||||
assert_ne!(bef, aft);
|
||||
} else {
|
||||
assert_eq!(bef, aft);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_kbucket() {
|
||||
let my_id = {
|
||||
let mut bytes = vec![random(); 34];
|
||||
bytes[0] = 18;
|
||||
bytes[1] = 32;
|
||||
PeerId::from_bytes(bytes).unwrap()
|
||||
};
|
||||
|
||||
assert!(MAX_NODES_PER_BUCKET <= 251); // Test doesn't work otherwise.
|
||||
let mut fill_ids = (0..MAX_NODES_PER_BUCKET + 3)
|
||||
.map(|n| {
|
||||
let mut id = my_id.clone().into_bytes();
|
||||
id[2] ^= 0x80; // Flip the first bit so that we get in the most distant bucket.
|
||||
id[33] = id[33].wrapping_add(n as u8);
|
||||
PeerId::from_bytes(id).unwrap()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let first_node = fill_ids[0].clone();
|
||||
let second_node = fill_ids[1].clone();
|
||||
|
||||
let table = KBucketsTable::new(my_id.clone(), Duration::from_secs(1));
|
||||
|
||||
for (num, id) in fill_ids.drain(..MAX_NODES_PER_BUCKET).enumerate() {
|
||||
assert_eq!(table.update(id, ()), UpdateOutcome::Added);
|
||||
assert_eq!(table.buckets().nth(255).unwrap().num_entries(), num + 1);
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
table.buckets().nth(255).unwrap().num_entries(),
|
||||
MAX_NODES_PER_BUCKET
|
||||
);
|
||||
assert!(!table.buckets().nth(255).unwrap().has_pending());
|
||||
assert_eq!(
|
||||
table.update(fill_ids.remove(0), ()),
|
||||
UpdateOutcome::NeedPing(first_node)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
table.buckets().nth(255).unwrap().num_entries(),
|
||||
MAX_NODES_PER_BUCKET
|
||||
);
|
||||
assert!(table.buckets().nth(255).unwrap().has_pending());
|
||||
assert_eq!(
|
||||
table.update(fill_ids.remove(0), ()),
|
||||
UpdateOutcome::Discarded
|
||||
);
|
||||
|
||||
thread::sleep(Duration::from_secs(2));
|
||||
assert!(!table.buckets().nth(255).unwrap().has_pending());
|
||||
assert_eq!(
|
||||
table.update(fill_ids.remove(0), ()),
|
||||
UpdateOutcome::NeedPing(second_node)
|
||||
);
|
||||
}
|
||||
}
|
89
protocols/kad/src/lib.rs
Normal file
89
protocols/kad/src/lib.rs
Normal file
@ -0,0 +1,89 @@
|
||||
// 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.
|
||||
|
||||
//! Kademlia protocol. Allows peer discovery, records store and records fetch.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! Usage is done in the following steps:
|
||||
//!
|
||||
//! - Build a `KadSystemConfig` and a `KadConnecConfig` object that contain the way you want the
|
||||
//! Kademlia protocol to behave.
|
||||
//!
|
||||
//! - Create a swarm that upgrades incoming connections with the `KadConnecConfig`.
|
||||
//!
|
||||
//! - Build a `KadSystem` from the `KadSystemConfig`. This requires passing a closure that provides
|
||||
//! the Kademlia controller of a peer.
|
||||
//!
|
||||
//! - You can perform queries using the `KadSystem`.
|
||||
//!
|
||||
|
||||
// TODO: we allow dead_code for now because this library contains a lot of unused code that will
|
||||
// be useful later for record store
|
||||
#![allow(dead_code)]
|
||||
|
||||
// # Crate organization
|
||||
//
|
||||
// The crate contains three levels of abstractions over the Kademlia protocol.
|
||||
//
|
||||
// - The first level of abstraction is in `protocol`. The API of this module lets you turn a raw
|
||||
// bytes stream (`AsyncRead + AsyncWrite`) into a `Sink + Stream` of raw but strongly-typed
|
||||
// Kademlia messages.
|
||||
//
|
||||
// - The second level of abstraction is in `kad_server`. Its API lets you upgrade a connection and
|
||||
// obtain a future (that must be driven to completion), plus a controller. Processing the future
|
||||
// will automatically respond to Kad requests received by the remote. The controller lets you
|
||||
// send your own requests to this remote and obtain strongly-typed responses.
|
||||
//
|
||||
// - The third level of abstraction is in `high_level`. This module only provides the
|
||||
// `KademliaSystem`.
|
||||
//
|
||||
|
||||
extern crate arrayvec;
|
||||
extern crate bigint;
|
||||
extern crate bs58;
|
||||
extern crate bytes;
|
||||
extern crate datastore;
|
||||
extern crate fnv;
|
||||
extern crate futures;
|
||||
extern crate libp2p_identify;
|
||||
extern crate libp2p_ping;
|
||||
extern crate libp2p_core;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate multiaddr;
|
||||
extern crate parking_lot;
|
||||
extern crate protobuf;
|
||||
extern crate rand;
|
||||
extern crate smallvec;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_timer;
|
||||
extern crate unsigned_varint;
|
||||
|
||||
pub use self::high_level::{KadSystemConfig, KadSystem, KadQueryEvent};
|
||||
pub use self::kad_server::{KadConnecController, KadConnecConfig, KadIncomingRequest, KadFindNodeRespond};
|
||||
pub use self::protocol::{KadConnectionType, KadPeer};
|
||||
|
||||
mod high_level;
|
||||
mod kad_server;
|
||||
mod kbucket;
|
||||
mod protobuf_structs;
|
||||
mod protocol;
|
900
protocols/kad/src/protobuf_structs/dht.rs
Normal file
900
protocols/kad/src/protobuf_structs/dht.rs
Normal file
@ -0,0 +1,900 @@
|
||||
// This file is generated by rust-protobuf 2.0.2. Do not edit
|
||||
// @generated
|
||||
|
||||
// https://github.com/Manishearth/rust-clippy/issues/702
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(clippy)]
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
#![allow(box_pointers)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(trivial_casts)]
|
||||
#![allow(unsafe_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_results)]
|
||||
|
||||
use protobuf::Message as Message_imported_for_functions;
|
||||
use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Message {
|
||||
// message fields
|
||||
field_type: ::std::option::Option<Message_MessageType>,
|
||||
clusterLevelRaw: ::std::option::Option<i32>,
|
||||
key: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
record: ::protobuf::SingularPtrField<super::record::Record>,
|
||||
closerPeers: ::protobuf::RepeatedField<Message_Peer>,
|
||||
providerPeers: ::protobuf::RepeatedField<Message_Peer>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new() -> Message {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional .dht.pb.Message.MessageType type = 1;
|
||||
|
||||
pub fn clear_field_type(&mut self) {
|
||||
self.field_type = ::std::option::Option::None;
|
||||
}
|
||||
|
||||
pub fn has_field_type(&self) -> bool {
|
||||
self.field_type.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_field_type(&mut self, v: Message_MessageType) {
|
||||
self.field_type = ::std::option::Option::Some(v);
|
||||
}
|
||||
|
||||
pub fn get_field_type(&self) -> Message_MessageType {
|
||||
self.field_type.unwrap_or(Message_MessageType::PUT_VALUE)
|
||||
}
|
||||
|
||||
// optional int32 clusterLevelRaw = 10;
|
||||
|
||||
pub fn clear_clusterLevelRaw(&mut self) {
|
||||
self.clusterLevelRaw = ::std::option::Option::None;
|
||||
}
|
||||
|
||||
pub fn has_clusterLevelRaw(&self) -> bool {
|
||||
self.clusterLevelRaw.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_clusterLevelRaw(&mut self, v: i32) {
|
||||
self.clusterLevelRaw = ::std::option::Option::Some(v);
|
||||
}
|
||||
|
||||
pub fn get_clusterLevelRaw(&self) -> i32 {
|
||||
self.clusterLevelRaw.unwrap_or(0)
|
||||
}
|
||||
|
||||
// optional bytes key = 2;
|
||||
|
||||
pub fn clear_key(&mut self) {
|
||||
self.key.clear();
|
||||
}
|
||||
|
||||
pub fn has_key(&self) -> bool {
|
||||
self.key.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_key(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.key = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_key(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.key.is_none() {
|
||||
self.key.set_default();
|
||||
}
|
||||
self.key.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_key(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.key.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_key(&self) -> &[u8] {
|
||||
match self.key.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional .record.pb.Record record = 3;
|
||||
|
||||
pub fn clear_record(&mut self) {
|
||||
self.record.clear();
|
||||
}
|
||||
|
||||
pub fn has_record(&self) -> bool {
|
||||
self.record.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_record(&mut self, v: super::record::Record) {
|
||||
self.record = ::protobuf::SingularPtrField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_record(&mut self) -> &mut super::record::Record {
|
||||
if self.record.is_none() {
|
||||
self.record.set_default();
|
||||
}
|
||||
self.record.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_record(&mut self) -> super::record::Record {
|
||||
self.record.take().unwrap_or_else(|| super::record::Record::new())
|
||||
}
|
||||
|
||||
pub fn get_record(&self) -> &super::record::Record {
|
||||
self.record.as_ref().unwrap_or_else(|| super::record::Record::default_instance())
|
||||
}
|
||||
|
||||
// repeated .dht.pb.Message.Peer closerPeers = 8;
|
||||
|
||||
pub fn clear_closerPeers(&mut self) {
|
||||
self.closerPeers.clear();
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_closerPeers(&mut self, v: ::protobuf::RepeatedField<Message_Peer>) {
|
||||
self.closerPeers = v;
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
pub fn mut_closerPeers(&mut self) -> &mut ::protobuf::RepeatedField<Message_Peer> {
|
||||
&mut self.closerPeers
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_closerPeers(&mut self) -> ::protobuf::RepeatedField<Message_Peer> {
|
||||
::std::mem::replace(&mut self.closerPeers, ::protobuf::RepeatedField::new())
|
||||
}
|
||||
|
||||
pub fn get_closerPeers(&self) -> &[Message_Peer] {
|
||||
&self.closerPeers
|
||||
}
|
||||
|
||||
// repeated .dht.pb.Message.Peer providerPeers = 9;
|
||||
|
||||
pub fn clear_providerPeers(&mut self) {
|
||||
self.providerPeers.clear();
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_providerPeers(&mut self, v: ::protobuf::RepeatedField<Message_Peer>) {
|
||||
self.providerPeers = v;
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
pub fn mut_providerPeers(&mut self) -> &mut ::protobuf::RepeatedField<Message_Peer> {
|
||||
&mut self.providerPeers
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_providerPeers(&mut self) -> ::protobuf::RepeatedField<Message_Peer> {
|
||||
::std::mem::replace(&mut self.providerPeers, ::protobuf::RepeatedField::new())
|
||||
}
|
||||
|
||||
pub fn get_providerPeers(&self) -> &[Message_Peer] {
|
||||
&self.providerPeers
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Message {
|
||||
fn is_initialized(&self) -> bool {
|
||||
for v in &self.record {
|
||||
if !v.is_initialized() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
for v in &self.closerPeers {
|
||||
if !v.is_initialized() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
for v in &self.providerPeers {
|
||||
if !v.is_initialized() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_proto2_enum_with_unknown_fields_into(wire_type, is, &mut self.field_type, 1, &mut self.unknown_fields)?
|
||||
},
|
||||
10 => {
|
||||
if wire_type != ::protobuf::wire_format::WireTypeVarint {
|
||||
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
|
||||
}
|
||||
let tmp = is.read_int32()?;
|
||||
self.clusterLevelRaw = ::std::option::Option::Some(tmp);
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.key)?;
|
||||
},
|
||||
3 => {
|
||||
::protobuf::rt::read_singular_message_into(wire_type, is, &mut self.record)?;
|
||||
},
|
||||
8 => {
|
||||
::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.closerPeers)?;
|
||||
},
|
||||
9 => {
|
||||
::protobuf::rt::read_repeated_message_into(wire_type, is, &mut self.providerPeers)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(v) = self.field_type {
|
||||
my_size += ::protobuf::rt::enum_size(1, v);
|
||||
}
|
||||
if let Some(v) = self.clusterLevelRaw {
|
||||
my_size += ::protobuf::rt::value_size(10, v, ::protobuf::wire_format::WireTypeVarint);
|
||||
}
|
||||
if let Some(ref v) = self.key.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
if let Some(ref v) = self.record.as_ref() {
|
||||
let len = v.compute_size();
|
||||
my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
|
||||
}
|
||||
for value in &self.closerPeers {
|
||||
let len = value.compute_size();
|
||||
my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
|
||||
};
|
||||
for value in &self.providerPeers {
|
||||
let len = value.compute_size();
|
||||
my_size += 1 + ::protobuf::rt::compute_raw_varint32_size(len) + len;
|
||||
};
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(v) = self.field_type {
|
||||
os.write_enum(1, v.value())?;
|
||||
}
|
||||
if let Some(v) = self.clusterLevelRaw {
|
||||
os.write_int32(10, v)?;
|
||||
}
|
||||
if let Some(ref v) = self.key.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.record.as_ref() {
|
||||
os.write_tag(3, ::protobuf::wire_format::WireTypeLengthDelimited)?;
|
||||
os.write_raw_varint32(v.get_cached_size())?;
|
||||
v.write_to_with_cached_sizes(os)?;
|
||||
}
|
||||
for v in &self.closerPeers {
|
||||
os.write_tag(8, ::protobuf::wire_format::WireTypeLengthDelimited)?;
|
||||
os.write_raw_varint32(v.get_cached_size())?;
|
||||
v.write_to_with_cached_sizes(os)?;
|
||||
};
|
||||
for v in &self.providerPeers {
|
||||
os.write_tag(9, ::protobuf::wire_format::WireTypeLengthDelimited)?;
|
||||
os.write_raw_varint32(v.get_cached_size())?;
|
||||
v.write_to_with_cached_sizes(os)?;
|
||||
};
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Message {
|
||||
Message::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_option_accessor::<_, ::protobuf::types::ProtobufTypeEnum<Message_MessageType>>(
|
||||
"type",
|
||||
|m: &Message| { &m.field_type },
|
||||
|m: &mut Message| { &mut m.field_type },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_option_accessor::<_, ::protobuf::types::ProtobufTypeInt32>(
|
||||
"clusterLevelRaw",
|
||||
|m: &Message| { &m.clusterLevelRaw },
|
||||
|m: &mut Message| { &mut m.clusterLevelRaw },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"key",
|
||||
|m: &Message| { &m.key },
|
||||
|m: &mut Message| { &mut m.key },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_ptr_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage<super::record::Record>>(
|
||||
"record",
|
||||
|m: &Message| { &m.record },
|
||||
|m: &mut Message| { &mut m.record },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage<Message_Peer>>(
|
||||
"closerPeers",
|
||||
|m: &Message| { &m.closerPeers },
|
||||
|m: &mut Message| { &mut m.closerPeers },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeMessage<Message_Peer>>(
|
||||
"providerPeers",
|
||||
|m: &Message| { &m.providerPeers },
|
||||
|m: &mut Message| { &mut m.providerPeers },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Message>(
|
||||
"Message",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Message {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Message> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Message,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Message::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Message {
|
||||
fn clear(&mut self) {
|
||||
self.clear_field_type();
|
||||
self.clear_clusterLevelRaw();
|
||||
self.clear_key();
|
||||
self.clear_record();
|
||||
self.clear_closerPeers();
|
||||
self.clear_providerPeers();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Message {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Message {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Message_Peer {
|
||||
// message fields
|
||||
id: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
addrs: ::protobuf::RepeatedField<::std::vec::Vec<u8>>,
|
||||
connection: ::std::option::Option<Message_ConnectionType>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Message_Peer {
|
||||
pub fn new() -> Message_Peer {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional bytes id = 1;
|
||||
|
||||
pub fn clear_id(&mut self) {
|
||||
self.id.clear();
|
||||
}
|
||||
|
||||
pub fn has_id(&self) -> bool {
|
||||
self.id.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_id(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.id = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_id(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.id.is_none() {
|
||||
self.id.set_default();
|
||||
}
|
||||
self.id.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_id(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.id.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> &[u8] {
|
||||
match self.id.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// repeated bytes addrs = 2;
|
||||
|
||||
pub fn clear_addrs(&mut self) {
|
||||
self.addrs.clear();
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_addrs(&mut self, v: ::protobuf::RepeatedField<::std::vec::Vec<u8>>) {
|
||||
self.addrs = v;
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
pub fn mut_addrs(&mut self) -> &mut ::protobuf::RepeatedField<::std::vec::Vec<u8>> {
|
||||
&mut self.addrs
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_addrs(&mut self) -> ::protobuf::RepeatedField<::std::vec::Vec<u8>> {
|
||||
::std::mem::replace(&mut self.addrs, ::protobuf::RepeatedField::new())
|
||||
}
|
||||
|
||||
pub fn get_addrs(&self) -> &[::std::vec::Vec<u8>] {
|
||||
&self.addrs
|
||||
}
|
||||
|
||||
// optional .dht.pb.Message.ConnectionType connection = 3;
|
||||
|
||||
pub fn clear_connection(&mut self) {
|
||||
self.connection = ::std::option::Option::None;
|
||||
}
|
||||
|
||||
pub fn has_connection(&self) -> bool {
|
||||
self.connection.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_connection(&mut self, v: Message_ConnectionType) {
|
||||
self.connection = ::std::option::Option::Some(v);
|
||||
}
|
||||
|
||||
pub fn get_connection(&self) -> Message_ConnectionType {
|
||||
self.connection.unwrap_or(Message_ConnectionType::NOT_CONNECTED)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Message_Peer {
|
||||
fn is_initialized(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.id)?;
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_repeated_bytes_into(wire_type, is, &mut self.addrs)?;
|
||||
},
|
||||
3 => {
|
||||
::protobuf::rt::read_proto2_enum_with_unknown_fields_into(wire_type, is, &mut self.connection, 3, &mut self.unknown_fields)?
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(ref v) = self.id.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(1, &v);
|
||||
}
|
||||
for value in &self.addrs {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &value);
|
||||
};
|
||||
if let Some(v) = self.connection {
|
||||
my_size += ::protobuf::rt::enum_size(3, v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(ref v) = self.id.as_ref() {
|
||||
os.write_bytes(1, &v)?;
|
||||
}
|
||||
for v in &self.addrs {
|
||||
os.write_bytes(2, &v)?;
|
||||
};
|
||||
if let Some(v) = self.connection {
|
||||
os.write_enum(3, v.value())?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Message_Peer {
|
||||
Message_Peer::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"id",
|
||||
|m: &Message_Peer| { &m.id },
|
||||
|m: &mut Message_Peer| { &mut m.id },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_repeated_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"addrs",
|
||||
|m: &Message_Peer| { &m.addrs },
|
||||
|m: &mut Message_Peer| { &mut m.addrs },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_option_accessor::<_, ::protobuf::types::ProtobufTypeEnum<Message_ConnectionType>>(
|
||||
"connection",
|
||||
|m: &Message_Peer| { &m.connection },
|
||||
|m: &mut Message_Peer| { &mut m.connection },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Message_Peer>(
|
||||
"Message_Peer",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Message_Peer {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Message_Peer> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Message_Peer,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Message_Peer::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Message_Peer {
|
||||
fn clear(&mut self) {
|
||||
self.clear_id();
|
||||
self.clear_addrs();
|
||||
self.clear_connection();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Message_Peer {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Message_Peer {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,PartialEq,Eq,Debug,Hash)]
|
||||
pub enum Message_MessageType {
|
||||
PUT_VALUE = 0,
|
||||
GET_VALUE = 1,
|
||||
ADD_PROVIDER = 2,
|
||||
GET_PROVIDERS = 3,
|
||||
FIND_NODE = 4,
|
||||
PING = 5,
|
||||
}
|
||||
|
||||
impl ::protobuf::ProtobufEnum for Message_MessageType {
|
||||
fn value(&self) -> i32 {
|
||||
*self as i32
|
||||
}
|
||||
|
||||
fn from_i32(value: i32) -> ::std::option::Option<Message_MessageType> {
|
||||
match value {
|
||||
0 => ::std::option::Option::Some(Message_MessageType::PUT_VALUE),
|
||||
1 => ::std::option::Option::Some(Message_MessageType::GET_VALUE),
|
||||
2 => ::std::option::Option::Some(Message_MessageType::ADD_PROVIDER),
|
||||
3 => ::std::option::Option::Some(Message_MessageType::GET_PROVIDERS),
|
||||
4 => ::std::option::Option::Some(Message_MessageType::FIND_NODE),
|
||||
5 => ::std::option::Option::Some(Message_MessageType::PING),
|
||||
_ => ::std::option::Option::None
|
||||
}
|
||||
}
|
||||
|
||||
fn values() -> &'static [Self] {
|
||||
static values: &'static [Message_MessageType] = &[
|
||||
Message_MessageType::PUT_VALUE,
|
||||
Message_MessageType::GET_VALUE,
|
||||
Message_MessageType::ADD_PROVIDER,
|
||||
Message_MessageType::GET_PROVIDERS,
|
||||
Message_MessageType::FIND_NODE,
|
||||
Message_MessageType::PING,
|
||||
];
|
||||
values
|
||||
}
|
||||
|
||||
fn enum_descriptor_static() -> &'static ::protobuf::reflect::EnumDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::EnumDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::EnumDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
::protobuf::reflect::EnumDescriptor::new("Message_MessageType", file_descriptor_proto())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::marker::Copy for Message_MessageType {
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Message_MessageType {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Enum(self.descriptor())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone,PartialEq,Eq,Debug,Hash)]
|
||||
pub enum Message_ConnectionType {
|
||||
NOT_CONNECTED = 0,
|
||||
CONNECTED = 1,
|
||||
CAN_CONNECT = 2,
|
||||
CANNOT_CONNECT = 3,
|
||||
}
|
||||
|
||||
impl ::protobuf::ProtobufEnum for Message_ConnectionType {
|
||||
fn value(&self) -> i32 {
|
||||
*self as i32
|
||||
}
|
||||
|
||||
fn from_i32(value: i32) -> ::std::option::Option<Message_ConnectionType> {
|
||||
match value {
|
||||
0 => ::std::option::Option::Some(Message_ConnectionType::NOT_CONNECTED),
|
||||
1 => ::std::option::Option::Some(Message_ConnectionType::CONNECTED),
|
||||
2 => ::std::option::Option::Some(Message_ConnectionType::CAN_CONNECT),
|
||||
3 => ::std::option::Option::Some(Message_ConnectionType::CANNOT_CONNECT),
|
||||
_ => ::std::option::Option::None
|
||||
}
|
||||
}
|
||||
|
||||
fn values() -> &'static [Self] {
|
||||
static values: &'static [Message_ConnectionType] = &[
|
||||
Message_ConnectionType::NOT_CONNECTED,
|
||||
Message_ConnectionType::CONNECTED,
|
||||
Message_ConnectionType::CAN_CONNECT,
|
||||
Message_ConnectionType::CANNOT_CONNECT,
|
||||
];
|
||||
values
|
||||
}
|
||||
|
||||
fn enum_descriptor_static() -> &'static ::protobuf::reflect::EnumDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::EnumDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::EnumDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
::protobuf::reflect::EnumDescriptor::new("Message_ConnectionType", file_descriptor_proto())
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::marker::Copy for Message_ConnectionType {
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Message_ConnectionType {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Enum(self.descriptor())
|
||||
}
|
||||
}
|
||||
|
||||
static file_descriptor_proto_data: &'static [u8] = b"\
|
||||
\n\tdht.proto\x12\x06dht.pb\x1a\x0crecord.proto\"\xc7\x04\n\x07Message\
|
||||
\x12/\n\x04type\x18\x01\x20\x01(\x0e2\x1b.dht.pb.Message.MessageTypeR\
|
||||
\x04type\x12(\n\x0fclusterLevelRaw\x18\n\x20\x01(\x05R\x0fclusterLevelRa\
|
||||
w\x12\x10\n\x03key\x18\x02\x20\x01(\x0cR\x03key\x12)\n\x06record\x18\x03\
|
||||
\x20\x01(\x0b2\x11.record.pb.RecordR\x06record\x126\n\x0bcloserPeers\x18\
|
||||
\x08\x20\x03(\x0b2\x14.dht.pb.Message.PeerR\x0bcloserPeers\x12:\n\rprovi\
|
||||
derPeers\x18\t\x20\x03(\x0b2\x14.dht.pb.Message.PeerR\rproviderPeers\x1a\
|
||||
l\n\x04Peer\x12\x0e\n\x02id\x18\x01\x20\x01(\x0cR\x02id\x12\x14\n\x05add\
|
||||
rs\x18\x02\x20\x03(\x0cR\x05addrs\x12>\n\nconnection\x18\x03\x20\x01(\
|
||||
\x0e2\x1e.dht.pb.Message.ConnectionTypeR\nconnection\"i\n\x0bMessageType\
|
||||
\x12\r\n\tPUT_VALUE\x10\0\x12\r\n\tGET_VALUE\x10\x01\x12\x10\n\x0cADD_PR\
|
||||
OVIDER\x10\x02\x12\x11\n\rGET_PROVIDERS\x10\x03\x12\r\n\tFIND_NODE\x10\
|
||||
\x04\x12\x08\n\x04PING\x10\x05\"W\n\x0eConnectionType\x12\x11\n\rNOT_CON\
|
||||
NECTED\x10\0\x12\r\n\tCONNECTED\x10\x01\x12\x0f\n\x0bCAN_CONNECT\x10\x02\
|
||||
\x12\x12\n\x0eCANNOT_CONNECT\x10\x03J\xc9\x10\n\x06\x12\x04\0\0>\x01\n\
|
||||
\x08\n\x01\x0c\x12\x03\0\0\x12\n\x08\n\x01\x02\x12\x03\x01\x08\x0e\n\t\n\
|
||||
\x02\x03\0\x12\x03\x03\x07\x15\n\n\n\x02\x04\0\x12\x04\x05\0>\x01\n\n\n\
|
||||
\x03\x04\0\x01\x12\x03\x05\x08\x0f\n\x0c\n\x04\x04\0\x04\0\x12\x04\x06\
|
||||
\x08\r\t\n\x0c\n\x05\x04\0\x04\0\x01\x12\x03\x06\r\x18\n\r\n\x06\x04\0\
|
||||
\x04\0\x02\0\x12\x03\x07\x10\x1e\n\x0e\n\x07\x04\0\x04\0\x02\0\x01\x12\
|
||||
\x03\x07\x10\x19\n\x0e\n\x07\x04\0\x04\0\x02\0\x02\x12\x03\x07\x1c\x1d\n\
|
||||
\r\n\x06\x04\0\x04\0\x02\x01\x12\x03\x08\x10\x1e\n\x0e\n\x07\x04\0\x04\0\
|
||||
\x02\x01\x01\x12\x03\x08\x10\x19\n\x0e\n\x07\x04\0\x04\0\x02\x01\x02\x12\
|
||||
\x03\x08\x1c\x1d\n\r\n\x06\x04\0\x04\0\x02\x02\x12\x03\t\x10!\n\x0e\n\
|
||||
\x07\x04\0\x04\0\x02\x02\x01\x12\x03\t\x10\x1c\n\x0e\n\x07\x04\0\x04\0\
|
||||
\x02\x02\x02\x12\x03\t\x1f\x20\n\r\n\x06\x04\0\x04\0\x02\x03\x12\x03\n\
|
||||
\x10\"\n\x0e\n\x07\x04\0\x04\0\x02\x03\x01\x12\x03\n\x10\x1d\n\x0e\n\x07\
|
||||
\x04\0\x04\0\x02\x03\x02\x12\x03\n\x20!\n\r\n\x06\x04\0\x04\0\x02\x04\
|
||||
\x12\x03\x0b\x10\x1e\n\x0e\n\x07\x04\0\x04\0\x02\x04\x01\x12\x03\x0b\x10\
|
||||
\x19\n\x0e\n\x07\x04\0\x04\0\x02\x04\x02\x12\x03\x0b\x1c\x1d\n\r\n\x06\
|
||||
\x04\0\x04\0\x02\x05\x12\x03\x0c\x10\x19\n\x0e\n\x07\x04\0\x04\0\x02\x05\
|
||||
\x01\x12\x03\x0c\x10\x14\n\x0e\n\x07\x04\0\x04\0\x02\x05\x02\x12\x03\x0c\
|
||||
\x17\x18\n\x0c\n\x04\x04\0\x04\x01\x12\x04\x0f\x08\x1c\t\n\x0c\n\x05\x04\
|
||||
\0\x04\x01\x01\x12\x03\x0f\r\x1b\n^\n\x06\x04\0\x04\x01\x02\0\x12\x03\
|
||||
\x11\x10\"\x1aO\x20sender\x20does\x20not\x20have\x20a\x20connection\x20t\
|
||||
o\x20peer,\x20and\x20no\x20extra\x20information\x20(default)\n\n\x0e\n\
|
||||
\x07\x04\0\x04\x01\x02\0\x01\x12\x03\x11\x10\x1d\n\x0e\n\x07\x04\0\x04\
|
||||
\x01\x02\0\x02\x12\x03\x11\x20!\n5\n\x06\x04\0\x04\x01\x02\x01\x12\x03\
|
||||
\x14\x10\x1e\x1a&\x20sender\x20has\x20a\x20live\x20connection\x20to\x20p\
|
||||
eer\n\n\x0e\n\x07\x04\0\x04\x01\x02\x01\x01\x12\x03\x14\x10\x19\n\x0e\n\
|
||||
\x07\x04\0\x04\x01\x02\x01\x02\x12\x03\x14\x1c\x1d\n2\n\x06\x04\0\x04\
|
||||
\x01\x02\x02\x12\x03\x17\x10\x20\x1a#\x20sender\x20recently\x20connected\
|
||||
\x20to\x20peer\n\n\x0e\n\x07\x04\0\x04\x01\x02\x02\x01\x12\x03\x17\x10\
|
||||
\x1b\n\x0e\n\x07\x04\0\x04\x01\x02\x02\x02\x12\x03\x17\x1e\x1f\n\xa7\x01\
|
||||
\n\x06\x04\0\x04\x01\x02\x03\x12\x03\x1b\x10#\x1a\x97\x01\x20sender\x20r\
|
||||
ecently\x20tried\x20to\x20connect\x20to\x20peer\x20repeatedly\x20but\x20\
|
||||
failed\x20to\x20connect\n\x20(\"try\"\x20here\x20is\x20loose,\x20but\x20\
|
||||
this\x20should\x20signal\x20\"made\x20strong\x20effort,\x20failed\")\n\n\
|
||||
\x0e\n\x07\x04\0\x04\x01\x02\x03\x01\x12\x03\x1b\x10\x1e\n\x0e\n\x07\x04\
|
||||
\0\x04\x01\x02\x03\x02\x12\x03\x1b!\"\n\x0c\n\x04\x04\0\x03\0\x12\x04\
|
||||
\x1e\x08'\t\n\x0c\n\x05\x04\0\x03\0\x01\x12\x03\x1e\x10\x14\n$\n\x06\x04\
|
||||
\0\x03\0\x02\0\x12\x03\x20\x10&\x1a\x15\x20ID\x20of\x20a\x20given\x20pee\
|
||||
r.\n\n\x0e\n\x07\x04\0\x03\0\x02\0\x04\x12\x03\x20\x10\x18\n\x0e\n\x07\
|
||||
\x04\0\x03\0\x02\0\x05\x12\x03\x20\x19\x1e\n\x0e\n\x07\x04\0\x03\0\x02\0\
|
||||
\x01\x12\x03\x20\x1f!\n\x0e\n\x07\x04\0\x03\0\x02\0\x03\x12\x03\x20$%\n,\
|
||||
\n\x06\x04\0\x03\0\x02\x01\x12\x03#\x10)\x1a\x1d\x20multiaddrs\x20for\
|
||||
\x20a\x20given\x20peer\n\n\x0e\n\x07\x04\0\x03\0\x02\x01\x04\x12\x03#\
|
||||
\x10\x18\n\x0e\n\x07\x04\0\x03\0\x02\x01\x05\x12\x03#\x19\x1e\n\x0e\n\
|
||||
\x07\x04\0\x03\0\x02\x01\x01\x12\x03#\x1f$\n\x0e\n\x07\x04\0\x03\0\x02\
|
||||
\x01\x03\x12\x03#'(\nP\n\x06\x04\0\x03\0\x02\x02\x12\x03&\x107\x1aA\x20u\
|
||||
sed\x20to\x20signal\x20the\x20sender's\x20connection\x20capabilities\x20\
|
||||
to\x20the\x20peer\n\n\x0e\n\x07\x04\0\x03\0\x02\x02\x04\x12\x03&\x10\x18\
|
||||
\n\x0e\n\x07\x04\0\x03\0\x02\x02\x06\x12\x03&\x19'\n\x0e\n\x07\x04\0\x03\
|
||||
\0\x02\x02\x01\x12\x03&(2\n\x0e\n\x07\x04\0\x03\0\x02\x02\x03\x12\x03&56\
|
||||
\n2\n\x04\x04\0\x02\0\x12\x03*\x08&\x1a%\x20defines\x20what\x20type\x20o\
|
||||
f\x20message\x20it\x20is.\n\n\x0c\n\x05\x04\0\x02\0\x04\x12\x03*\x08\x10\
|
||||
\n\x0c\n\x05\x04\0\x02\0\x06\x12\x03*\x11\x1c\n\x0c\n\x05\x04\0\x02\0\
|
||||
\x01\x12\x03*\x1d!\n\x0c\n\x05\x04\0\x02\0\x03\x12\x03*$%\nO\n\x04\x04\0\
|
||||
\x02\x01\x12\x03-\x08,\x1aB\x20defines\x20what\x20coral\x20cluster\x20le\
|
||||
vel\x20this\x20query/response\x20belongs\x20to.\n\n\x0c\n\x05\x04\0\x02\
|
||||
\x01\x04\x12\x03-\x08\x10\n\x0c\n\x05\x04\0\x02\x01\x05\x12\x03-\x11\x16\
|
||||
\n\x0c\n\x05\x04\0\x02\x01\x01\x12\x03-\x17&\n\x0c\n\x05\x04\0\x02\x01\
|
||||
\x03\x12\x03-)+\nw\n\x04\x04\0\x02\x02\x12\x031\x08\x1f\x1aj\x20Used\x20\
|
||||
to\x20specify\x20the\x20key\x20associated\x20with\x20this\x20message.\n\
|
||||
\x20PUT_VALUE,\x20GET_VALUE,\x20ADD_PROVIDER,\x20GET_PROVIDERS\n\n\x0c\n\
|
||||
\x05\x04\0\x02\x02\x04\x12\x031\x08\x10\n\x0c\n\x05\x04\0\x02\x02\x05\
|
||||
\x12\x031\x11\x16\n\x0c\n\x05\x04\0\x02\x02\x01\x12\x031\x17\x1a\n\x0c\n\
|
||||
\x05\x04\0\x02\x02\x03\x12\x031\x1d\x1e\n;\n\x04\x04\0\x02\x03\x12\x035\
|
||||
\x08-\x1a.\x20Used\x20to\x20return\x20a\x20value\n\x20PUT_VALUE,\x20GET_\
|
||||
VALUE\n\n\x0c\n\x05\x04\0\x02\x03\x04\x12\x035\x08\x10\n\x0c\n\x05\x04\0\
|
||||
\x02\x03\x06\x12\x035\x11!\n\x0c\n\x05\x04\0\x02\x03\x01\x12\x035\"(\n\
|
||||
\x0c\n\x05\x04\0\x02\x03\x03\x12\x035+,\nc\n\x04\x04\0\x02\x04\x12\x039\
|
||||
\x08&\x1aV\x20Used\x20to\x20return\x20peers\x20closer\x20to\x20a\x20key\
|
||||
\x20in\x20a\x20query\n\x20GET_VALUE,\x20GET_PROVIDERS,\x20FIND_NODE\n\n\
|
||||
\x0c\n\x05\x04\0\x02\x04\x04\x12\x039\x08\x10\n\x0c\n\x05\x04\0\x02\x04\
|
||||
\x06\x12\x039\x11\x15\n\x0c\n\x05\x04\0\x02\x04\x01\x12\x039\x16!\n\x0c\
|
||||
\n\x05\x04\0\x02\x04\x03\x12\x039$%\nO\n\x04\x04\0\x02\x05\x12\x03=\x08(\
|
||||
\x1aB\x20Used\x20to\x20return\x20Providers\n\x20GET_VALUE,\x20ADD_PROVID\
|
||||
ER,\x20GET_PROVIDERS\n\n\x0c\n\x05\x04\0\x02\x05\x04\x12\x03=\x08\x10\n\
|
||||
\x0c\n\x05\x04\0\x02\x05\x06\x12\x03=\x11\x15\n\x0c\n\x05\x04\0\x02\x05\
|
||||
\x01\x12\x03=\x16#\n\x0c\n\x05\x04\0\x02\x05\x03\x12\x03=&'\
|
||||
";
|
||||
|
||||
static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto,
|
||||
};
|
||||
|
||||
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
|
||||
::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
|
||||
unsafe {
|
||||
file_descriptor_proto_lazy.get(|| {
|
||||
parse_descriptor_proto()
|
||||
})
|
||||
}
|
||||
}
|
22
protocols/kad/src/protobuf_structs/mod.rs
Normal file
22
protocols/kad/src/protobuf_structs/mod.rs
Normal file
@ -0,0 +1,22 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
pub mod dht;
|
||||
pub mod record;
|
453
protocols/kad/src/protobuf_structs/record.rs
Normal file
453
protocols/kad/src/protobuf_structs/record.rs
Normal file
@ -0,0 +1,453 @@
|
||||
// This file is generated by rust-protobuf 2.0.2. Do not edit
|
||||
// @generated
|
||||
|
||||
// https://github.com/Manishearth/rust-clippy/issues/702
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(clippy)]
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
#![allow(box_pointers)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(trivial_casts)]
|
||||
#![allow(unsafe_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_results)]
|
||||
|
||||
use protobuf::Message as Message_imported_for_functions;
|
||||
use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Record {
|
||||
// message fields
|
||||
key: ::protobuf::SingularField<::std::string::String>,
|
||||
value: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
author: ::protobuf::SingularField<::std::string::String>,
|
||||
signature: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
timeReceived: ::protobuf::SingularField<::std::string::String>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Record {
|
||||
pub fn new() -> Record {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional string key = 1;
|
||||
|
||||
pub fn clear_key(&mut self) {
|
||||
self.key.clear();
|
||||
}
|
||||
|
||||
pub fn has_key(&self) -> bool {
|
||||
self.key.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_key(&mut self, v: ::std::string::String) {
|
||||
self.key = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_key(&mut self) -> &mut ::std::string::String {
|
||||
if self.key.is_none() {
|
||||
self.key.set_default();
|
||||
}
|
||||
self.key.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_key(&mut self) -> ::std::string::String {
|
||||
self.key.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_key(&self) -> &str {
|
||||
match self.key.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional bytes value = 2;
|
||||
|
||||
pub fn clear_value(&mut self) {
|
||||
self.value.clear();
|
||||
}
|
||||
|
||||
pub fn has_value(&self) -> bool {
|
||||
self.value.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_value(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.value = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_value(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.value.is_none() {
|
||||
self.value.set_default();
|
||||
}
|
||||
self.value.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_value(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.value.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_value(&self) -> &[u8] {
|
||||
match self.value.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional string author = 3;
|
||||
|
||||
pub fn clear_author(&mut self) {
|
||||
self.author.clear();
|
||||
}
|
||||
|
||||
pub fn has_author(&self) -> bool {
|
||||
self.author.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_author(&mut self, v: ::std::string::String) {
|
||||
self.author = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_author(&mut self) -> &mut ::std::string::String {
|
||||
if self.author.is_none() {
|
||||
self.author.set_default();
|
||||
}
|
||||
self.author.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_author(&mut self) -> ::std::string::String {
|
||||
self.author.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_author(&self) -> &str {
|
||||
match self.author.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional bytes signature = 4;
|
||||
|
||||
pub fn clear_signature(&mut self) {
|
||||
self.signature.clear();
|
||||
}
|
||||
|
||||
pub fn has_signature(&self) -> bool {
|
||||
self.signature.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_signature(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.signature = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_signature(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.signature.is_none() {
|
||||
self.signature.set_default();
|
||||
}
|
||||
self.signature.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_signature(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.signature.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_signature(&self) -> &[u8] {
|
||||
match self.signature.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional string timeReceived = 5;
|
||||
|
||||
pub fn clear_timeReceived(&mut self) {
|
||||
self.timeReceived.clear();
|
||||
}
|
||||
|
||||
pub fn has_timeReceived(&self) -> bool {
|
||||
self.timeReceived.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_timeReceived(&mut self, v: ::std::string::String) {
|
||||
self.timeReceived = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_timeReceived(&mut self) -> &mut ::std::string::String {
|
||||
if self.timeReceived.is_none() {
|
||||
self.timeReceived.set_default();
|
||||
}
|
||||
self.timeReceived.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_timeReceived(&mut self) -> ::std::string::String {
|
||||
self.timeReceived.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_timeReceived(&self) -> &str {
|
||||
match self.timeReceived.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Record {
|
||||
fn is_initialized(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.key)?;
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.value)?;
|
||||
},
|
||||
3 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.author)?;
|
||||
},
|
||||
4 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.signature)?;
|
||||
},
|
||||
5 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.timeReceived)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(ref v) = self.key.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(1, &v);
|
||||
}
|
||||
if let Some(ref v) = self.value.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
if let Some(ref v) = self.author.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(3, &v);
|
||||
}
|
||||
if let Some(ref v) = self.signature.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(4, &v);
|
||||
}
|
||||
if let Some(ref v) = self.timeReceived.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(5, &v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(ref v) = self.key.as_ref() {
|
||||
os.write_string(1, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.value.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.author.as_ref() {
|
||||
os.write_string(3, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.signature.as_ref() {
|
||||
os.write_bytes(4, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.timeReceived.as_ref() {
|
||||
os.write_string(5, &v)?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Record {
|
||||
Record::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"key",
|
||||
|m: &Record| { &m.key },
|
||||
|m: &mut Record| { &mut m.key },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"value",
|
||||
|m: &Record| { &m.value },
|
||||
|m: &mut Record| { &mut m.value },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"author",
|
||||
|m: &Record| { &m.author },
|
||||
|m: &mut Record| { &mut m.author },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"signature",
|
||||
|m: &Record| { &m.signature },
|
||||
|m: &mut Record| { &mut m.signature },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"timeReceived",
|
||||
|m: &Record| { &m.timeReceived },
|
||||
|m: &mut Record| { &mut m.timeReceived },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Record>(
|
||||
"Record",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Record {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Record> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Record,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Record::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Record {
|
||||
fn clear(&mut self) {
|
||||
self.clear_key();
|
||||
self.clear_value();
|
||||
self.clear_author();
|
||||
self.clear_signature();
|
||||
self.clear_timeReceived();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Record {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Record {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
static file_descriptor_proto_data: &'static [u8] = b"\
|
||||
\n\x0crecord.proto\x12\trecord.pb\"\x8a\x01\n\x06Record\x12\x10\n\x03key\
|
||||
\x18\x01\x20\x01(\tR\x03key\x12\x14\n\x05value\x18\x02\x20\x01(\x0cR\x05\
|
||||
value\x12\x16\n\x06author\x18\x03\x20\x01(\tR\x06author\x12\x1c\n\tsigna\
|
||||
ture\x18\x04\x20\x01(\x0cR\tsignature\x12\"\n\x0ctimeReceived\x18\x05\
|
||||
\x20\x01(\tR\x0ctimeReceivedJ\xac\x05\n\x06\x12\x04\0\0\x14\x01\n\x08\n\
|
||||
\x01\x0c\x12\x03\0\0\x12\n\x08\n\x01\x02\x12\x03\x01\x08\x11\nX\n\x02\
|
||||
\x04\0\x12\x04\x05\0\x14\x01\x1aL\x20Record\x20represents\x20a\x20dht\
|
||||
\x20record\x20that\x20contains\x20a\x20value\n\x20for\x20a\x20key\x20val\
|
||||
ue\x20pair\n\n\n\n\x03\x04\0\x01\x12\x03\x05\x08\x0e\n2\n\x04\x04\0\x02\
|
||||
\0\x12\x03\x07\x08\x20\x1a%\x20The\x20key\x20that\x20references\x20this\
|
||||
\x20record\n\n\x0c\n\x05\x04\0\x02\0\x04\x12\x03\x07\x08\x10\n\x0c\n\x05\
|
||||
\x04\0\x02\0\x05\x12\x03\x07\x11\x17\n\x0c\n\x05\x04\0\x02\0\x01\x12\x03\
|
||||
\x07\x18\x1b\n\x0c\n\x05\x04\0\x02\0\x03\x12\x03\x07\x1e\x1f\n6\n\x04\
|
||||
\x04\0\x02\x01\x12\x03\n\x08!\x1a)\x20The\x20actual\x20value\x20this\x20\
|
||||
record\x20is\x20storing\n\n\x0c\n\x05\x04\0\x02\x01\x04\x12\x03\n\x08\
|
||||
\x10\n\x0c\n\x05\x04\0\x02\x01\x05\x12\x03\n\x11\x16\n\x0c\n\x05\x04\0\
|
||||
\x02\x01\x01\x12\x03\n\x17\x1c\n\x0c\n\x05\x04\0\x02\x01\x03\x12\x03\n\
|
||||
\x1f\x20\n-\n\x04\x04\0\x02\x02\x12\x03\r\x08#\x1a\x20\x20hash\x20of\x20\
|
||||
the\x20authors\x20public\x20key\n\n\x0c\n\x05\x04\0\x02\x02\x04\x12\x03\
|
||||
\r\x08\x10\n\x0c\n\x05\x04\0\x02\x02\x05\x12\x03\r\x11\x17\n\x0c\n\x05\
|
||||
\x04\0\x02\x02\x01\x12\x03\r\x18\x1e\n\x0c\n\x05\x04\0\x02\x02\x03\x12\
|
||||
\x03\r!\"\n7\n\x04\x04\0\x02\x03\x12\x03\x10\x08%\x1a*\x20A\x20PKI\x20si\
|
||||
gnature\x20for\x20the\x20key+value+author\n\n\x0c\n\x05\x04\0\x02\x03\
|
||||
\x04\x12\x03\x10\x08\x10\n\x0c\n\x05\x04\0\x02\x03\x05\x12\x03\x10\x11\
|
||||
\x16\n\x0c\n\x05\x04\0\x02\x03\x01\x12\x03\x10\x17\x20\n\x0c\n\x05\x04\0\
|
||||
\x02\x03\x03\x12\x03\x10#$\n<\n\x04\x04\0\x02\x04\x12\x03\x13\x08)\x1a/\
|
||||
\x20Time\x20the\x20record\x20was\x20received,\x20set\x20by\x20receiver\n\
|
||||
\n\x0c\n\x05\x04\0\x02\x04\x04\x12\x03\x13\x08\x10\n\x0c\n\x05\x04\0\x02\
|
||||
\x04\x05\x12\x03\x13\x11\x17\n\x0c\n\x05\x04\0\x02\x04\x01\x12\x03\x13\
|
||||
\x18$\n\x0c\n\x05\x04\0\x02\x04\x03\x12\x03\x13'(\
|
||||
";
|
||||
|
||||
static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto,
|
||||
};
|
||||
|
||||
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
|
||||
::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
|
||||
unsafe {
|
||||
file_descriptor_proto_lazy.get(|| {
|
||||
parse_descriptor_proto()
|
||||
})
|
||||
}
|
||||
}
|
381
protocols/kad/src/protocol.rs
Normal file
381
protocols/kad/src/protocol.rs
Normal file
@ -0,0 +1,381 @@
|
||||
// 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.
|
||||
|
||||
//! Provides the `KadMsg` enum of all the possible messages transmitted with the Kademlia protocol,
|
||||
//! and the `KademliaProtocolConfig` connection upgrade whose output is a
|
||||
//! `Stream<Item = KadMsg> + Sink<SinkItem = KadMsg>`.
|
||||
//!
|
||||
//! The `Stream` component is used to poll the underlying transport, and the `Sink` component is
|
||||
//! used to send messages.
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::{future, sink, Sink, stream, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, Multiaddr, PeerId};
|
||||
use protobuf::{self, Message};
|
||||
use protobuf_structs;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::iter;
|
||||
use tokio_codec::Framed;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use unsigned_varint::codec;
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
|
||||
pub enum KadConnectionType {
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl From<protobuf_structs::dht::Message_ConnectionType> for KadConnectionType {
|
||||
#[inline]
|
||||
fn from(raw: protobuf_structs::dht::Message_ConnectionType) -> KadConnectionType {
|
||||
use protobuf_structs::dht::Message_ConnectionType::*;
|
||||
match raw {
|
||||
NOT_CONNECTED => KadConnectionType::NotConnected,
|
||||
CONNECTED => KadConnectionType::Connected,
|
||||
CAN_CONNECT => KadConnectionType::CanConnect,
|
||||
CANNOT_CONNECT => KadConnectionType::CannotConnect,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<protobuf_structs::dht::Message_ConnectionType> for KadConnectionType {
|
||||
#[inline]
|
||||
fn into(self) -> protobuf_structs::dht::Message_ConnectionType {
|
||||
use protobuf_structs::dht::Message_ConnectionType::*;
|
||||
match self {
|
||||
KadConnectionType::NotConnected => NOT_CONNECTED,
|
||||
KadConnectionType::Connected => CONNECTED,
|
||||
KadConnectionType::CanConnect => CAN_CONNECT,
|
||||
KadConnectionType::CannotConnect => CANNOT_CONNECT,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Information about a peer, as known by the sender.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct KadPeer {
|
||||
pub node_id: PeerId,
|
||||
/// The multiaddresses that are known for that peer.
|
||||
pub multiaddrs: Vec<Multiaddr>,
|
||||
pub connection_ty: KadConnectionType,
|
||||
}
|
||||
|
||||
impl KadPeer {
|
||||
// Builds a `KadPeer` from its raw protobuf equivalent.
|
||||
// TODO: use TryFrom once stable
|
||||
fn from_peer(peer: &mut protobuf_structs::dht::Message_Peer) -> Result<KadPeer, IoError> {
|
||||
// TODO: this is in fact a CID ; not sure if this should be handled in `from_bytes` or
|
||||
// as a special case here
|
||||
let node_id = PeerId::from_bytes(peer.get_id().to_vec())
|
||||
.map_err(|_| IoError::new(IoErrorKind::InvalidData, "invalid peer id"))?;
|
||||
|
||||
let mut addrs = Vec::with_capacity(peer.get_addrs().len());
|
||||
for addr in peer.take_addrs().into_iter() {
|
||||
let as_ma = Multiaddr::from_bytes(addr)
|
||||
.map_err(|err| IoError::new(IoErrorKind::InvalidData, err))?;
|
||||
addrs.push(as_ma);
|
||||
}
|
||||
debug_assert_eq!(addrs.len(), addrs.capacity());
|
||||
|
||||
let connection_ty = peer.get_connection().into();
|
||||
|
||||
Ok(KadPeer {
|
||||
node_id: node_id,
|
||||
multiaddrs: addrs,
|
||||
connection_ty: connection_ty,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<protobuf_structs::dht::Message_Peer> for KadPeer {
|
||||
fn into(self) -> protobuf_structs::dht::Message_Peer {
|
||||
let mut out = protobuf_structs::dht::Message_Peer::new();
|
||||
out.set_id(self.node_id.into_bytes());
|
||||
for addr in self.multiaddrs {
|
||||
out.mut_addrs().push(addr.into_bytes());
|
||||
}
|
||||
out.set_connection(self.connection_ty.into());
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration for a Kademlia connection upgrade. When applied to a connection, turns this
|
||||
/// connection into a `Stream + Sink` whose items are of type `KadMsg`.
|
||||
#[derive(Debug, Default, Copy, Clone)]
|
||||
pub struct KademliaProtocolConfig;
|
||||
|
||||
impl<C, Maf> ConnectionUpgrade<C, Maf> for KademliaProtocolConfig
|
||||
where
|
||||
C: AsyncRead + AsyncWrite + 'static, // TODO: 'static :-/
|
||||
{
|
||||
type Output = KadStreamSink<C>;
|
||||
type MultiaddrFuture = Maf;
|
||||
type Future = future::FutureResult<(Self::Output, Self::MultiaddrFuture), IoError>;
|
||||
type NamesIter = iter::Once<(Bytes, ())>;
|
||||
type UpgradeIdentifier = ();
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/ipfs/kad/1.0.0".into(), ()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn upgrade(self, incoming: C, _: (), _: Endpoint, addr: Maf) -> Self::Future {
|
||||
future::ok((kademlia_protocol(incoming), addr))
|
||||
}
|
||||
}
|
||||
|
||||
type KadStreamSink<S> = stream::AndThen<sink::With<stream::FromErr<Framed<S, codec::UviBytes<Vec<u8>>>, IoError>, KadMsg, fn(KadMsg) -> Result<Vec<u8>, IoError>, Result<Vec<u8>, IoError>>, fn(BytesMut) -> Result<KadMsg, IoError>, Result<KadMsg, IoError>>;
|
||||
|
||||
// Upgrades a socket to use the Kademlia protocol.
|
||||
fn kademlia_protocol<S>(
|
||||
socket: S,
|
||||
) -> KadStreamSink<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
Framed::new(socket, codec::UviBytes::default())
|
||||
.from_err::<IoError>()
|
||||
.with::<_, fn(_) -> _, _>(|request| -> Result<_, IoError> {
|
||||
let proto_struct = msg_to_proto(request);
|
||||
Ok(proto_struct.write_to_bytes().unwrap()) // TODO: error?
|
||||
})
|
||||
.and_then::<fn(_) -> _, _>(|bytes| {
|
||||
let response = protobuf::parse_from_bytes(&bytes)?;
|
||||
proto_to_msg(response)
|
||||
})
|
||||
}
|
||||
|
||||
/// Message that we can send to a peer or received from a peer.
|
||||
// TODO: document the rest
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum KadMsg {
|
||||
/// Ping request or response.
|
||||
Ping,
|
||||
/// Target must save the given record, can be queried later with `GetValueReq`.
|
||||
PutValue {
|
||||
/// Identifier of the record.
|
||||
key: Vec<u8>,
|
||||
/// The record itself.
|
||||
record: (), //record: protobuf_structs::record::Record, // TODO: no
|
||||
},
|
||||
GetValueReq {
|
||||
/// Identifier of the record.
|
||||
key: Vec<u8>,
|
||||
},
|
||||
GetValueRes {
|
||||
/// Identifier of the returned record.
|
||||
key: Vec<u8>,
|
||||
record: (), //record: Option<protobuf_structs::record::Record>, // TODO: no
|
||||
closer_peers: Vec<KadPeer>,
|
||||
},
|
||||
/// 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.
|
||||
FindNodeReq {
|
||||
/// Identifier of the node.
|
||||
key: Vec<u8>,
|
||||
},
|
||||
/// Response to a `FindNodeReq`.
|
||||
FindNodeRes {
|
||||
/// Results of the request.
|
||||
closer_peers: Vec<KadPeer>,
|
||||
},
|
||||
}
|
||||
|
||||
// Turns a type-safe kadmelia message into the corresponding row protobuf message.
|
||||
fn msg_to_proto(kad_msg: KadMsg) -> protobuf_structs::dht::Message {
|
||||
match kad_msg {
|
||||
KadMsg::Ping => {
|
||||
let mut msg = protobuf_structs::dht::Message::new();
|
||||
msg.set_field_type(protobuf_structs::dht::Message_MessageType::PING);
|
||||
msg
|
||||
}
|
||||
KadMsg::PutValue { key, .. } => {
|
||||
let mut msg = protobuf_structs::dht::Message::new();
|
||||
msg.set_field_type(protobuf_structs::dht::Message_MessageType::PUT_VALUE);
|
||||
msg.set_key(key);
|
||||
//msg.set_record(record); // TODO:
|
||||
msg
|
||||
}
|
||||
KadMsg::GetValueReq { key } => {
|
||||
let mut msg = protobuf_structs::dht::Message::new();
|
||||
msg.set_field_type(protobuf_structs::dht::Message_MessageType::GET_VALUE);
|
||||
msg.set_key(key);
|
||||
msg.set_clusterLevelRaw(10);
|
||||
msg
|
||||
}
|
||||
KadMsg::GetValueRes { .. } => unimplemented!(), // TODO:
|
||||
KadMsg::FindNodeReq { key } => {
|
||||
let mut msg = protobuf_structs::dht::Message::new();
|
||||
msg.set_field_type(protobuf_structs::dht::Message_MessageType::FIND_NODE);
|
||||
msg.set_key(key);
|
||||
msg.set_clusterLevelRaw(10);
|
||||
msg
|
||||
}
|
||||
KadMsg::FindNodeRes { closer_peers } => {
|
||||
// TODO: if empty, the remote will think it's a request
|
||||
// TODO: not good, possibly exposed in the API
|
||||
assert!(!closer_peers.is_empty());
|
||||
let mut msg = protobuf_structs::dht::Message::new();
|
||||
msg.set_field_type(protobuf_structs::dht::Message_MessageType::FIND_NODE);
|
||||
msg.set_clusterLevelRaw(9);
|
||||
for peer in closer_peers {
|
||||
msg.mut_closerPeers().push(peer.into());
|
||||
}
|
||||
msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Turns a raw Kademlia message into a type-safe message.
|
||||
fn proto_to_msg(mut message: protobuf_structs::dht::Message) -> Result<KadMsg, IoError> {
|
||||
match message.get_field_type() {
|
||||
protobuf_structs::dht::Message_MessageType::PING => Ok(KadMsg::Ping),
|
||||
|
||||
protobuf_structs::dht::Message_MessageType::PUT_VALUE => {
|
||||
let key = message.take_key();
|
||||
let _record = message.take_record();
|
||||
Ok(KadMsg::PutValue {
|
||||
key: key,
|
||||
record: (),
|
||||
})
|
||||
}
|
||||
|
||||
protobuf_structs::dht::Message_MessageType::GET_VALUE => {
|
||||
let key = message.take_key();
|
||||
Ok(KadMsg::GetValueReq { key: key })
|
||||
}
|
||||
|
||||
protobuf_structs::dht::Message_MessageType::FIND_NODE => {
|
||||
if message.get_closerPeers().is_empty() {
|
||||
Ok(KadMsg::FindNodeReq {
|
||||
key: message.take_key(),
|
||||
})
|
||||
|
||||
} else {
|
||||
// TODO: for now we don't parse the peer properly, so it is possible that we get
|
||||
// parsing errors for peers even when they are valid ; we ignore these
|
||||
// errors for now, but ultimately we should just error altogether
|
||||
let closer_peers = message.mut_closerPeers()
|
||||
.iter_mut()
|
||||
.filter_map(|peer| KadPeer::from_peer(peer).ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Ok(KadMsg::FindNodeRes {
|
||||
closer_peers,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
protobuf_structs::dht::Message_MessageType::GET_PROVIDERS
|
||||
| protobuf_structs::dht::Message_MessageType::ADD_PROVIDER => {
|
||||
// These messages don't seem to be used in the protocol in practice, so if we receive
|
||||
// them we suppose that it's a mistake in the protocol usage.
|
||||
Err(IoError::new(
|
||||
IoErrorKind::InvalidData,
|
||||
"received an unsupported kad message type",
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate libp2p_tcp_transport;
|
||||
extern crate tokio_current_thread;
|
||||
|
||||
use self::libp2p_tcp_transport::TcpConfig;
|
||||
use futures::{Future, Sink, Stream};
|
||||
use libp2p_core::{Transport, PeerId, PublicKey};
|
||||
use protocol::{KadConnectionType, KadMsg, KademliaProtocolConfig, KadPeer};
|
||||
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::PutValue {
|
||||
key: vec![1, 2, 3, 4],
|
||||
record: (),
|
||||
});
|
||||
test_one(KadMsg::GetValueReq {
|
||||
key: vec![10, 11, 12],
|
||||
});
|
||||
test_one(KadMsg::FindNodeReq {
|
||||
key: vec![9, 12, 0, 245, 245, 201, 28, 95],
|
||||
});
|
||||
test_one(KadMsg::FindNodeRes {
|
||||
closer_peers: vec![
|
||||
KadPeer {
|
||||
node_id: PeerId::from_public_key(PublicKey::Rsa(vec![93, 80, 12, 250])),
|
||||
multiaddrs: vec!["/ip4/100.101.102.103/tcp/20105".parse().unwrap()],
|
||||
connection_ty: KadConnectionType::Connected,
|
||||
},
|
||||
],
|
||||
});
|
||||
// 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 || {
|
||||
let transport = TcpConfig::new().with_upgrade(KademliaProtocolConfig);
|
||||
|
||||
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)
|
||||
.and_then(|(client, _)| client.unwrap().map(|v| v.0))
|
||||
.and_then(|proto| proto.into_future().map_err(|(err, _)| err).map(|(v, _)| v))
|
||||
.map(|recv_msg| {
|
||||
assert_eq!(recv_msg.unwrap(), msg_server);
|
||||
()
|
||||
});
|
||||
|
||||
let _ = tokio_current_thread::block_on_all(future).unwrap();
|
||||
});
|
||||
|
||||
let transport = TcpConfig::new().with_upgrade(KademliaProtocolConfig);
|
||||
|
||||
let future = transport
|
||||
.dial(rx.recv().unwrap())
|
||||
.unwrap_or_else(|_| panic!())
|
||||
.and_then(|proto| proto.0.send(msg_client))
|
||||
.map(|_| ());
|
||||
|
||||
let _ = tokio_current_thread::block_on_all(future).unwrap();
|
||||
bg_thread.join().unwrap();
|
||||
}
|
||||
}
|
||||
}
|
365
protocols/kad/src/query.rs
Normal file
365
protocols/kad/src/query.rs
Normal file
@ -0,0 +1,365 @@
|
||||
// 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.
|
||||
|
||||
//! This module handles performing iterative queries about the network.
|
||||
|
||||
use fnv::FnvHashSet;
|
||||
use futures::{future, Future, stream, Stream};
|
||||
use kbucket::KBucketsPeerId;
|
||||
use libp2p_core::PeerId;
|
||||
use multiaddr::{AddrComponent, Multiaddr};
|
||||
use protocol;
|
||||
use rand;
|
||||
use smallvec::SmallVec;
|
||||
use std::cmp::Ordering;
|
||||
use std::io::Error as IoError;
|
||||
use std::mem;
|
||||
|
||||
/// Parameters of a query. Allows plugging the query-related code with the rest of the
|
||||
/// infrastructure.
|
||||
pub struct QueryParams<FBuckets, FFindNode> {
|
||||
/// Identifier of the local peer.
|
||||
pub local_id: PeerId,
|
||||
/// Called whenever we need to obtain the peers closest to a certain peer.
|
||||
pub kbuckets_find_closest: FBuckets,
|
||||
/// Level of parallelism for networking. If this is `N`, then we can dial `N` nodes at a time.
|
||||
pub parallelism: usize,
|
||||
/// Called whenever we want to send a `FIND_NODE` RPC query.
|
||||
pub find_node: FFindNode,
|
||||
}
|
||||
|
||||
/// Event that happens during a query.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum QueryEvent<TOut> {
|
||||
/// Learned about new mutiaddresses for the given peers.
|
||||
NewKnownMultiaddrs(Vec<(PeerId, Vec<Multiaddr>)>),
|
||||
/// Finished the processing of the query. Contains the result.
|
||||
Finished(TOut),
|
||||
}
|
||||
|
||||
/// Starts a query for an iterative `FIND_NODE` request.
|
||||
#[inline]
|
||||
pub fn find_node<'a, FBuckets, FFindNode>(
|
||||
query_params: QueryParams<FBuckets, FFindNode>,
|
||||
searched_key: PeerId,
|
||||
) -> Box<Stream<Item = QueryEvent<Vec<PeerId>>, Error = IoError> + 'a>
|
||||
where
|
||||
FBuckets: Fn(PeerId) -> Vec<PeerId> + 'a + Clone,
|
||||
FFindNode: Fn(Multiaddr, PeerId) -> Box<Future<Item = Vec<protocol::Peer>, Error = IoError>> + 'a + Clone,
|
||||
{
|
||||
query(query_params, searched_key, 20) // TODO: constant
|
||||
}
|
||||
|
||||
/// Refreshes a specific bucket by performing an iterative `FIND_NODE` on a random ID of this
|
||||
/// bucket.
|
||||
///
|
||||
/// Returns a dummy no-op future if `bucket_num` is out of range.
|
||||
pub fn refresh<'a, FBuckets, FFindNode>(
|
||||
query_params: QueryParams<FBuckets, FFindNode>,
|
||||
bucket_num: usize,
|
||||
) -> Box<Stream<Item = QueryEvent<()>, Error = IoError> + 'a>
|
||||
where
|
||||
FBuckets: Fn(PeerId) -> Vec<PeerId> + 'a + Clone,
|
||||
FFindNode: Fn(Multiaddr, PeerId) -> Box<Future<Item = Vec<protocol::Peer>, Error = IoError>> + 'a + Clone,
|
||||
{
|
||||
let peer_id = match gen_random_id(&query_params.local_id, bucket_num) {
|
||||
Ok(p) => p,
|
||||
Err(()) => return Box::new(stream::once(Ok(QueryEvent::Finished(())))),
|
||||
};
|
||||
|
||||
let stream = find_node(query_params, peer_id).map(|event| {
|
||||
match event {
|
||||
QueryEvent::NewKnownMultiaddrs(peers) => QueryEvent::NewKnownMultiaddrs(peers),
|
||||
QueryEvent::Finished(_) => QueryEvent::Finished(()),
|
||||
}
|
||||
});
|
||||
|
||||
Box::new(stream) as Box<_>
|
||||
}
|
||||
|
||||
// Generates a random `PeerId` that belongs to the given bucket.
|
||||
//
|
||||
// Returns an error if `bucket_num` is out of range.
|
||||
fn gen_random_id(my_id: &PeerId, bucket_num: usize) -> Result<PeerId, ()> {
|
||||
let my_id_len = my_id.as_bytes().len();
|
||||
|
||||
// TODO: this 2 is magic here ; it is the length of the hash of the multihash
|
||||
let bits_diff = bucket_num + 1;
|
||||
if bits_diff > 8 * (my_id_len - 2) {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let mut random_id = [0; 64];
|
||||
for byte in 0..my_id_len {
|
||||
match byte.cmp(&(my_id_len - bits_diff / 8 - 1)) {
|
||||
Ordering::Less => {
|
||||
random_id[byte] = my_id.as_bytes()[byte];
|
||||
}
|
||||
Ordering::Equal => {
|
||||
let mask: u8 = (1 << (bits_diff % 8)) - 1;
|
||||
random_id[byte] = (my_id.as_bytes()[byte] & !mask) | (rand::random::<u8>() & mask);
|
||||
}
|
||||
Ordering::Greater => {
|
||||
random_id[byte] = rand::random();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let peer_id = PeerId::from_bytes(random_id[..my_id_len].to_owned())
|
||||
.expect("randomly-generated peer ID should always be valid");
|
||||
Ok(peer_id)
|
||||
}
|
||||
|
||||
// Generic query-performing function.
|
||||
fn query<'a, FBuckets, FFindNode>(
|
||||
query_params: QueryParams<FBuckets, FFindNode>,
|
||||
searched_key: PeerId,
|
||||
num_results: usize,
|
||||
) -> Box<Stream<Item = QueryEvent<Vec<PeerId>>, Error = IoError> + 'a>
|
||||
where
|
||||
FBuckets: Fn(PeerId) -> Vec<PeerId> + 'a + Clone,
|
||||
FFindNode: Fn(Multiaddr, PeerId) -> Box<Future<Item = Vec<protocol::Peer>, Error = IoError>> + 'a + Clone,
|
||||
{
|
||||
debug!("Start query for {:?} ; num results = {}", searched_key, num_results);
|
||||
|
||||
// State of the current iterative process.
|
||||
struct State<'a> {
|
||||
// At which stage we are.
|
||||
stage: Stage,
|
||||
// Final output of the iteration.
|
||||
result: Vec<PeerId>,
|
||||
// For each open connection, a future with the response of the remote.
|
||||
// Note that don't use a `SmallVec` here because `select_all` produces a `Vec`.
|
||||
current_attempts_fut: Vec<Box<Future<Item = Vec<protocol::Peer>, Error = IoError> + 'a>>,
|
||||
// For each open connection, the peer ID that we are connected to.
|
||||
// Must always have the same length as `current_attempts_fut`.
|
||||
current_attempts_addrs: SmallVec<[PeerId; 32]>,
|
||||
// Nodes that need to be attempted.
|
||||
pending_nodes: Vec<PeerId>,
|
||||
// Peers that we tried to contact but failed.
|
||||
failed_to_contact: FnvHashSet<PeerId>,
|
||||
}
|
||||
|
||||
// General stage of the state.
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
enum Stage {
|
||||
// We are still in the first step of the algorithm where we try to find the closest node.
|
||||
FirstStep,
|
||||
// We are contacting the k closest nodes in order to fill the list with enough results.
|
||||
SecondStep,
|
||||
// The results are complete, and the next stream iteration will produce the outcome.
|
||||
FinishingNextIter,
|
||||
// We are finished and the stream shouldn't return anything anymore.
|
||||
Finished,
|
||||
}
|
||||
|
||||
let initial_state = State {
|
||||
stage: Stage::FirstStep,
|
||||
result: Vec::with_capacity(num_results),
|
||||
current_attempts_fut: Vec::new(),
|
||||
current_attempts_addrs: SmallVec::new(),
|
||||
pending_nodes: {
|
||||
let kbuckets_find_closest = query_params.kbuckets_find_closest.clone();
|
||||
kbuckets_find_closest(searched_key.clone()) // TODO: suboptimal
|
||||
},
|
||||
failed_to_contact: Default::default(),
|
||||
};
|
||||
|
||||
let parallelism = query_params.parallelism;
|
||||
|
||||
// Start of the iterative process.
|
||||
let stream = stream::unfold(initial_state, move |mut state| -> Option<_> {
|
||||
match state.stage {
|
||||
Stage::FinishingNextIter => {
|
||||
let result = mem::replace(&mut state.result, Vec::new());
|
||||
debug!("Query finished with {} results", result.len());
|
||||
state.stage = Stage::Finished;
|
||||
let future = future::ok((Some(QueryEvent::Finished(result)), state));
|
||||
return Some(future::Either::A(future));
|
||||
},
|
||||
Stage::Finished => {
|
||||
return None;
|
||||
},
|
||||
_ => ()
|
||||
};
|
||||
|
||||
let searched_key = searched_key.clone();
|
||||
let find_node_rpc = query_params.find_node.clone();
|
||||
|
||||
// Find out which nodes to contact at this iteration.
|
||||
let to_contact = {
|
||||
let wanted_len = if state.stage == Stage::FirstStep {
|
||||
parallelism.saturating_sub(state.current_attempts_fut.len())
|
||||
} else {
|
||||
num_results.saturating_sub(state.current_attempts_fut.len())
|
||||
};
|
||||
let mut to_contact = SmallVec::<[_; 16]>::new();
|
||||
while to_contact.len() < wanted_len && !state.pending_nodes.is_empty() {
|
||||
// Move the first element of `pending_nodes` to `to_contact`, but ignore nodes that
|
||||
// are already part of the results or of a current attempt or if we failed to
|
||||
// contact it before.
|
||||
let peer = state.pending_nodes.remove(0);
|
||||
if state.result.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
if state.current_attempts_addrs.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
if state.failed_to_contact.iter().any(|p| p == &peer) {
|
||||
continue;
|
||||
}
|
||||
to_contact.push(peer);
|
||||
}
|
||||
to_contact
|
||||
};
|
||||
|
||||
debug!("New query round ; {} queries in progress ; contacting {} new peers",
|
||||
state.current_attempts_fut.len(),
|
||||
to_contact.len());
|
||||
|
||||
// For each node in `to_contact`, start an RPC query and a corresponding entry in the two
|
||||
// `state.current_attempts_*` fields.
|
||||
for peer in to_contact {
|
||||
let multiaddr: Multiaddr = AddrComponent::P2P(peer.clone().into_bytes()).into();
|
||||
|
||||
let searched_key2 = searched_key.clone();
|
||||
let current_attempt = find_node_rpc(multiaddr.clone(), searched_key2); // TODO: suboptimal
|
||||
state.current_attempts_addrs.push(peer.clone());
|
||||
state
|
||||
.current_attempts_fut
|
||||
.push(Box::new(current_attempt) as Box<_>);
|
||||
}
|
||||
debug_assert_eq!(
|
||||
state.current_attempts_addrs.len(),
|
||||
state.current_attempts_fut.len()
|
||||
);
|
||||
|
||||
// Extract `current_attempts_fut` so that we can pass it to `select_all`. We will push the
|
||||
// values back when inside the loop.
|
||||
let current_attempts_fut = mem::replace(&mut state.current_attempts_fut, Vec::new());
|
||||
if current_attempts_fut.is_empty() {
|
||||
// If `current_attempts_fut` is empty, then `select_all` would panic. It happens
|
||||
// when we have no additional node to query.
|
||||
debug!("Finishing query early because no additional node available");
|
||||
state.stage = Stage::FinishingNextIter;
|
||||
let future = future::ok((None, state));
|
||||
return Some(future::Either::A(future));
|
||||
}
|
||||
|
||||
// This is the future that continues or breaks the `loop_fn`.
|
||||
let future = future::select_all(current_attempts_fut.into_iter()).then(move |result| {
|
||||
let (message, trigger_idx, other_current_attempts) = match result {
|
||||
Err((err, trigger_idx, other_current_attempts)) => {
|
||||
(Err(err), trigger_idx, other_current_attempts)
|
||||
}
|
||||
Ok((message, trigger_idx, other_current_attempts)) => {
|
||||
(Ok(message), trigger_idx, other_current_attempts)
|
||||
}
|
||||
};
|
||||
|
||||
// Putting back the extracted elements in `state`.
|
||||
let remote_id = state.current_attempts_addrs.remove(trigger_idx);
|
||||
debug_assert!(state.current_attempts_fut.is_empty());
|
||||
state.current_attempts_fut = other_current_attempts;
|
||||
|
||||
// `message` contains the reason why the current future was woken up.
|
||||
let closer_peers = match message {
|
||||
Ok(msg) => msg,
|
||||
Err(err) => {
|
||||
trace!("RPC query failed for {:?}: {:?}", remote_id, err);
|
||||
state.failed_to_contact.insert(remote_id);
|
||||
return future::ok((None, state));
|
||||
}
|
||||
};
|
||||
|
||||
// Inserting the node we received a response from into `state.result`.
|
||||
// The code is non-trivial because `state.result` is ordered by distance and is limited
|
||||
// by `num_results` elements.
|
||||
if let Some(insert_pos) = state.result.iter().position(|e| {
|
||||
e.distance_with(&searched_key) >= remote_id.distance_with(&searched_key)
|
||||
}) {
|
||||
if state.result[insert_pos] != remote_id {
|
||||
if state.result.len() >= num_results {
|
||||
state.result.pop();
|
||||
}
|
||||
state.result.insert(insert_pos, remote_id);
|
||||
}
|
||||
} else if state.result.len() < num_results {
|
||||
state.result.push(remote_id);
|
||||
}
|
||||
|
||||
// The loop below will set this variable to `true` if we find a new element to put at
|
||||
// the top of the result. This would mean that we have to continue looping.
|
||||
let mut local_nearest_node_updated = false;
|
||||
|
||||
// Update `state` with the actual content of the message.
|
||||
let mut new_known_multiaddrs = Vec::with_capacity(closer_peers.len());
|
||||
for mut peer in closer_peers {
|
||||
// Update the peerstore with the information sent by
|
||||
// the remote.
|
||||
{
|
||||
let multiaddrs = mem::replace(&mut peer.multiaddrs, Vec::new());
|
||||
trace!("Reporting multiaddresses for {:?}: {:?}", peer.node_id, multiaddrs);
|
||||
new_known_multiaddrs.push((peer.node_id.clone(), multiaddrs));
|
||||
}
|
||||
|
||||
if peer.node_id.distance_with(&searched_key)
|
||||
<= state.result[0].distance_with(&searched_key)
|
||||
{
|
||||
local_nearest_node_updated = true;
|
||||
}
|
||||
|
||||
if state.result.iter().any(|ma| ma == &peer.node_id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Insert the node into `pending_nodes` at the right position, or do not
|
||||
// insert it if it is already in there.
|
||||
if let Some(insert_pos) = state.pending_nodes.iter().position(|e| {
|
||||
e.distance_with(&searched_key) >= peer.node_id.distance_with(&searched_key)
|
||||
}) {
|
||||
if state.pending_nodes[insert_pos] != peer.node_id {
|
||||
state.pending_nodes.insert(insert_pos, peer.node_id.clone());
|
||||
}
|
||||
} else {
|
||||
state.pending_nodes.push(peer.node_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
if state.result.len() >= num_results
|
||||
|| (state.stage != Stage::FirstStep && state.current_attempts_fut.is_empty())
|
||||
{
|
||||
state.stage = Stage::FinishingNextIter;
|
||||
|
||||
} else {
|
||||
if !local_nearest_node_updated {
|
||||
trace!("Loop didn't update closer node ; jumping to step 2");
|
||||
state.stage = Stage::SecondStep;
|
||||
}
|
||||
}
|
||||
|
||||
future::ok((Some(QueryEvent::NewKnownMultiaddrs(new_known_multiaddrs)), state))
|
||||
});
|
||||
|
||||
Some(future::Either::B(future))
|
||||
}).filter_map(|val| val);
|
||||
|
||||
Box::new(stream) as Box<_>
|
||||
}
|
22
protocols/ping/Cargo.toml
Normal file
22
protocols/ping/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "libp2p-ping"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
bytes = "0.4"
|
||||
libp2p-core = { path = "../../core" }
|
||||
log = "0.4.1"
|
||||
multiaddr = { path = "../../misc/multiaddr" }
|
||||
multistream-select = { path = "../../misc/multistream-select" }
|
||||
futures = "0.1"
|
||||
parking_lot = "0.6"
|
||||
rand = "0.5"
|
||||
tokio-codec = "0.1"
|
||||
tokio-io = "0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
libp2p-tcp-transport = { path = "../../transports/tcp" }
|
||||
tokio-current-thread = "0.1"
|
||||
tokio-tcp = "0.1"
|
452
protocols/ping/src/lib.rs
Normal file
452
protocols/ping/src/lib.rs
Normal file
@ -0,0 +1,452 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! Handles the `/ipfs/ping/1.0.0` protocol. This allows pinging a remote node and waiting for an
|
||||
//! answer.
|
||||
//!
|
||||
//! # Usage
|
||||
//!
|
||||
//! Create a `Ping` struct, which implements the `ConnectionUpgrade` trait. When used as a
|
||||
//! connection upgrade, it will produce a tuple of type `(Pinger, impl Future<Item = ()>)` which
|
||||
//! are named the *pinger* and the *ponger*.
|
||||
//!
|
||||
//! The *pinger* has a method named `ping` which will send a ping to the remote, while the *ponger*
|
||||
//! is a future that will process the data received on the socket and will be signalled only when
|
||||
//! the connection closes.
|
||||
//!
|
||||
//! # About timeouts
|
||||
//!
|
||||
//! For technical reasons, this crate doesn't handle timeouts. The action of pinging returns a
|
||||
//! future that is signalled only when the remote answers. If the remote is not responsive, the
|
||||
//! future will never be signalled.
|
||||
//!
|
||||
//! For implementation reasons, resources allocated for a ping are only ever fully reclaimed after
|
||||
//! a pong has been received by the remote. Therefore if you repeatidely ping a non-responsive
|
||||
//! remote you will end up using more and memory memory (albeit the amount is very very small every
|
||||
//! time), even if you destroy the future returned by `ping`.
|
||||
//!
|
||||
//! This is probably not a problem in practice, because the nature of the ping protocol is to
|
||||
//! determine whether a remote is still alive, and any reasonable user of this crate will close
|
||||
//! connections to non-responsive remotes.
|
||||
//!
|
||||
//! # Example
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate futures;
|
||||
//! extern crate libp2p_ping;
|
||||
//! extern crate libp2p_core;
|
||||
//! extern crate libp2p_tcp_transport;
|
||||
//! extern crate tokio_current_thread;
|
||||
//!
|
||||
//! use futures::Future;
|
||||
//! use libp2p_ping::{Ping, PingOutput};
|
||||
//! use libp2p_core::Transport;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! let ping_finished_future = libp2p_tcp_transport::TcpConfig::new()
|
||||
//! .with_upgrade(Ping)
|
||||
//! .dial("127.0.0.1:12345".parse::<libp2p_core::Multiaddr>().unwrap()).unwrap_or_else(|_| panic!())
|
||||
//! .and_then(|(out, _)| {
|
||||
//! match out {
|
||||
//! PingOutput::Ponger(processing) => Box::new(processing) as Box<Future<Item = _, Error = _>>,
|
||||
//! PingOutput::Pinger { mut pinger, processing } => {
|
||||
//! let f = pinger.ping().map_err(|_| panic!()).select(processing).map(|_| ()).map_err(|(err, _)| err);
|
||||
//! Box::new(f) as Box<Future<Item = _, Error = _>>
|
||||
//! },
|
||||
//! }
|
||||
//! });
|
||||
//!
|
||||
//! // Runs until the ping arrives.
|
||||
//! tokio_current_thread::block_on_all(ping_finished_future).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
|
||||
extern crate bytes;
|
||||
extern crate futures;
|
||||
extern crate libp2p_core;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate multistream_select;
|
||||
extern crate parking_lot;
|
||||
extern crate rand;
|
||||
extern crate tokio_codec;
|
||||
extern crate tokio_io;
|
||||
|
||||
use bytes::{BufMut, Bytes, BytesMut};
|
||||
use futures::future::{loop_fn, FutureResult, IntoFuture, Loop};
|
||||
use futures::sync::{mpsc, oneshot};
|
||||
use futures::{Future, Sink, Stream};
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint};
|
||||
use parking_lot::Mutex;
|
||||
use rand::{distributions::Standard, prelude::*, rngs::EntropyRng};
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::io::Error as IoError;
|
||||
use std::iter;
|
||||
use std::sync::Arc;
|
||||
use tokio_codec::{Decoder, Encoder, Framed};
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
/// Represents a prototype for an upgrade to handle the ping protocol.
|
||||
///
|
||||
/// According to the design of libp2p, this struct would normally contain the configuration options
|
||||
/// for the protocol, but in the case of `Ping` no configuration is required.
|
||||
#[derive(Debug, Copy, Clone, Default)]
|
||||
pub struct Ping;
|
||||
|
||||
pub enum PingOutput {
|
||||
/// We are on the dialer side.
|
||||
Pinger {
|
||||
/// Object to use in order to ping the remote.
|
||||
pinger: Pinger,
|
||||
/// Future that drives the processing of the pings.
|
||||
processing: Box<Future<Item = (), Error = IoError>>,
|
||||
},
|
||||
/// We are on the listening side.
|
||||
Ponger(Box<Future<Item = (), Error = IoError>>),
|
||||
}
|
||||
|
||||
impl<C, Maf> ConnectionUpgrade<C, Maf> for Ping
|
||||
where
|
||||
C: AsyncRead + AsyncWrite + 'static,
|
||||
{
|
||||
type NamesIter = iter::Once<(Bytes, Self::UpgradeIdentifier)>;
|
||||
type UpgradeIdentifier = ();
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/ipfs/ping/1.0.0".into(), ()))
|
||||
}
|
||||
|
||||
type Output = PingOutput;
|
||||
type MultiaddrFuture = Maf;
|
||||
type Future = FutureResult<(Self::Output, Self::MultiaddrFuture), IoError>;
|
||||
|
||||
#[inline]
|
||||
fn upgrade(
|
||||
self,
|
||||
socket: C,
|
||||
_: Self::UpgradeIdentifier,
|
||||
endpoint: Endpoint,
|
||||
remote_addr: Maf,
|
||||
) -> Self::Future {
|
||||
let out = match endpoint {
|
||||
Endpoint::Dialer => upgrade_as_dialer(socket),
|
||||
Endpoint::Listener => upgrade_as_listener(socket),
|
||||
};
|
||||
|
||||
Ok((out, remote_addr)).into_future()
|
||||
}
|
||||
}
|
||||
|
||||
/// Upgrades a connection from the dialer side.
|
||||
fn upgrade_as_dialer(socket: impl AsyncRead + AsyncWrite + 'static) -> PingOutput {
|
||||
// # How does it work?
|
||||
//
|
||||
// All the actual processing is performed by the *ponger*.
|
||||
// We use a channel in order to send ping requests from the pinger to the ponger.
|
||||
|
||||
let (tx, rx) = mpsc::channel(8);
|
||||
// Ignore the errors if `tx` closed.
|
||||
let rx = rx.then(|r| Ok(r.ok())).filter_map(|a| a);
|
||||
|
||||
let pinger = Pinger {
|
||||
send: tx,
|
||||
rng: EntropyRng::default(),
|
||||
};
|
||||
|
||||
// Hashmap that associates outgoing payloads to one-shot senders.
|
||||
// TODO: can't figure out how to make it work without using an Arc/Mutex
|
||||
let expected_pongs = Arc::new(Mutex::new(HashMap::with_capacity(4)));
|
||||
|
||||
let sink_stream = Framed::new(socket, Codec).map(|msg| Message::Received(msg.freeze()));
|
||||
let (sink, stream) = sink_stream.split();
|
||||
|
||||
let future = loop_fn((sink, stream.select(rx)), move |(sink, stream)| {
|
||||
let expected_pongs = expected_pongs.clone();
|
||||
|
||||
stream
|
||||
.into_future()
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(move |(message, stream)| {
|
||||
let mut expected_pongs = expected_pongs.lock();
|
||||
|
||||
if let Some(message) = message {
|
||||
match message {
|
||||
Message::Ping(payload, finished) => {
|
||||
// Ping requested by the user through the `Pinger`.
|
||||
debug!("Sending ping with payload {:?}", payload);
|
||||
|
||||
expected_pongs.insert(payload.clone(), finished);
|
||||
Box::new(
|
||||
sink.send(payload)
|
||||
.map(|sink| Loop::Continue((sink, stream))),
|
||||
) as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
Message::Received(payload) => {
|
||||
// Received a payload from the remote.
|
||||
if let Some(fut) = expected_pongs.remove(&payload) {
|
||||
// Payload was ours. Signalling future.
|
||||
// Errors can happen if the user closed the receiving end of
|
||||
// the future, which is fine to ignore.
|
||||
debug!("Received pong (payload={:?}) ; ping fufilled", payload);
|
||||
let _ = fut.send(());
|
||||
Box::new(Ok(Loop::Continue((sink, stream))).into_future())
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
} else {
|
||||
// Payload was unexpected. Closing connection.
|
||||
debug!("Received invalid payload ({:?}) ; closing", payload);
|
||||
Box::new(Ok(Loop::Break(())).into_future())
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Box::new(Ok(Loop::Break(())).into_future()) as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
PingOutput::Pinger {
|
||||
pinger,
|
||||
processing: Box::new(future) as Box<_>,
|
||||
}
|
||||
}
|
||||
|
||||
/// Upgrades a connection from the listener side.
|
||||
fn upgrade_as_listener(socket: impl AsyncRead + AsyncWrite + 'static) -> PingOutput {
|
||||
let sink_stream = Framed::new(socket, Codec);
|
||||
let (sink, stream) = sink_stream.split();
|
||||
|
||||
let future = loop_fn((sink, stream), move |(sink, stream)| {
|
||||
stream
|
||||
.into_future()
|
||||
.map_err(|(err, _)| err)
|
||||
.and_then(move |(payload, stream)| {
|
||||
if let Some(payload) = payload {
|
||||
// Received a payload from the remote.
|
||||
debug!("Received ping (payload={:?}) ; sending back", payload);
|
||||
Box::new(
|
||||
sink.send(payload.freeze())
|
||||
.map(|sink| Loop::Continue((sink, stream))),
|
||||
) as Box<Future<Item = _, Error = _>>
|
||||
} else {
|
||||
// Connection was closed
|
||||
Box::new(Ok(Loop::Break(())).into_future()) as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
PingOutput::Ponger(Box::new(future) as Box<_>)
|
||||
}
|
||||
|
||||
/// Controller for the ping service. Makes it possible to send pings to the remote.
|
||||
pub struct Pinger {
|
||||
send: mpsc::Sender<Message>,
|
||||
rng: EntropyRng,
|
||||
}
|
||||
|
||||
impl Pinger {
|
||||
/// Sends a ping. Returns a future that is signaled when a pong is received.
|
||||
///
|
||||
/// **Note**: Please be aware that there is no timeout on the ping. You should handle the
|
||||
/// timeout yourself when you call this function.
|
||||
pub fn ping(&mut self) -> Box<Future<Item = (), Error = Box<Error + Send + Sync>>> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
let payload: [u8; 32] = self.rng.sample(Standard);
|
||||
debug!("Preparing for ping with payload {:?}", payload);
|
||||
// Ignore errors if the ponger has been already destroyed. The returned future will never
|
||||
// be signalled.
|
||||
let fut = self
|
||||
.send
|
||||
.clone()
|
||||
.send(Message::Ping(Bytes::from(payload.to_vec()), tx))
|
||||
.from_err()
|
||||
.and_then(|_| rx.from_err());
|
||||
Box::new(fut) as Box<_>
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Pinger {
|
||||
fn clone(&self) -> Pinger {
|
||||
Pinger {
|
||||
send: self.send.clone(),
|
||||
rng: EntropyRng::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Message {
|
||||
Ping(Bytes, oneshot::Sender<()>),
|
||||
Received(Bytes),
|
||||
}
|
||||
|
||||
// Implementation of the `Codec` trait of tokio-io. Splits frames into groups of 32 bytes.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
||||
struct Codec;
|
||||
|
||||
impl Decoder for Codec {
|
||||
type Item = BytesMut;
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn decode(&mut self, buf: &mut BytesMut) -> Result<Option<BytesMut>, IoError> {
|
||||
if buf.len() >= 32 {
|
||||
Ok(Some(buf.split_to(32)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Encoder for Codec {
|
||||
type Item = Bytes;
|
||||
type Error = IoError;
|
||||
|
||||
#[inline]
|
||||
fn encode(&mut self, mut data: Bytes, buf: &mut BytesMut) -> Result<(), IoError> {
|
||||
if data.len() != 0 {
|
||||
let split = 32 * (1 + ((data.len() - 1) / 32));
|
||||
buf.reserve(split);
|
||||
buf.put(data.split_to(split));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate tokio_current_thread;
|
||||
extern crate tokio_tcp;
|
||||
|
||||
use self::tokio_tcp::TcpListener;
|
||||
use self::tokio_tcp::TcpStream;
|
||||
use super::{Ping, PingOutput};
|
||||
use futures::future::{self, join_all};
|
||||
use futures::Future;
|
||||
use futures::Stream;
|
||||
use libp2p_core::{ConnectionUpgrade, Endpoint, Multiaddr};
|
||||
use std::io::Error as IoError;
|
||||
|
||||
// TODO: rewrite tests with the MemoryTransport
|
||||
|
||||
#[test]
|
||||
fn ping_pong() {
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let listener_addr = listener.local_addr().unwrap();
|
||||
|
||||
let server = listener
|
||||
.incoming()
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(|(c, _)| {
|
||||
Ping.upgrade(
|
||||
c.unwrap(),
|
||||
(),
|
||||
Endpoint::Listener,
|
||||
future::ok::<Multiaddr, IoError>("/ip4/127.0.0.1/tcp/10000".parse().unwrap()),
|
||||
)
|
||||
})
|
||||
.and_then(|(out, _)| match out {
|
||||
PingOutput::Ponger(service) => service,
|
||||
_ => unreachable!(),
|
||||
});
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(|c| {
|
||||
Ping.upgrade(
|
||||
c,
|
||||
(),
|
||||
Endpoint::Dialer,
|
||||
future::ok::<Multiaddr, IoError>("/ip4/127.0.0.1/tcp/10000".parse().unwrap()),
|
||||
)
|
||||
})
|
||||
.and_then(|(out, _)| match out {
|
||||
PingOutput::Pinger {
|
||||
mut pinger,
|
||||
processing,
|
||||
} => pinger
|
||||
.ping()
|
||||
.map_err(|_| panic!())
|
||||
.select(processing)
|
||||
.map_err(|_| panic!()),
|
||||
_ => unreachable!(),
|
||||
})
|
||||
.map(|_| ());
|
||||
|
||||
tokio_current_thread::block_on_all(server.select(client).map_err(|_| panic!())).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multipings() {
|
||||
// Check that we can send multiple pings in a row and it will still work.
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let listener_addr = listener.local_addr().unwrap();
|
||||
|
||||
let server = listener
|
||||
.incoming()
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(|(c, _)| {
|
||||
Ping.upgrade(
|
||||
c.unwrap(),
|
||||
(),
|
||||
Endpoint::Listener,
|
||||
future::ok::<Multiaddr, IoError>("/ip4/127.0.0.1/tcp/10000".parse().unwrap()),
|
||||
)
|
||||
})
|
||||
.and_then(|(out, _)| match out {
|
||||
PingOutput::Ponger(service) => service,
|
||||
_ => unreachable!(),
|
||||
});
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(|c| {
|
||||
Ping.upgrade(
|
||||
c,
|
||||
(),
|
||||
Endpoint::Dialer,
|
||||
future::ok::<Multiaddr, IoError>("/ip4/127.0.0.1/tcp/10000".parse().unwrap()),
|
||||
)
|
||||
})
|
||||
.and_then(|(out, _)| match out {
|
||||
PingOutput::Pinger {
|
||||
mut pinger,
|
||||
processing,
|
||||
} => {
|
||||
let pings = (0..20).map(move |_| pinger.ping().map_err(|_| ()));
|
||||
|
||||
join_all(pings)
|
||||
.map(|_| ())
|
||||
.map_err(|_| panic!())
|
||||
.select(processing)
|
||||
.map(|_| ())
|
||||
.map_err(|_| panic!())
|
||||
}
|
||||
_ => unreachable!(),
|
||||
});
|
||||
|
||||
tokio_current_thread::block_on_all(server.select(client)).unwrap_or_else(|_| panic!());
|
||||
}
|
||||
}
|
33
protocols/secio/Cargo.toml
Normal file
33
protocols/secio/Cargo.toml
Normal file
@ -0,0 +1,33 @@
|
||||
[package]
|
||||
name = "libp2p-secio"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
license = "MIT"
|
||||
|
||||
[dependencies]
|
||||
asn1_der = "0.5"
|
||||
bytes = "0.4"
|
||||
futures = "0.1"
|
||||
libp2p-core = { path = "../../core" }
|
||||
log = "0.4.1"
|
||||
protobuf = "2.0.2"
|
||||
rand = "0.3.17"
|
||||
ring = { version = "0.13.2", features = ["rsa_signing"] }
|
||||
aes-ctr = "0.1.0"
|
||||
aesni = { version = "0.4.1", features = ["nocheck"], optional = true }
|
||||
ctr = { version = "0.1", optional = true }
|
||||
lazy_static = { version = "0.2.11", optional = true }
|
||||
rw-stream-sink = { path = "../../misc/rw-stream-sink" }
|
||||
eth-secp256k1 = { git = "https://github.com/paritytech/rust-secp256k1", optional = true }
|
||||
tokio-io = "0.1.0"
|
||||
untrusted = "0.6.2"
|
||||
|
||||
[features]
|
||||
default = ["secp256k1"]
|
||||
secp256k1 = ["eth-secp256k1"]
|
||||
aes-all = ["ctr","aesni","lazy_static"]
|
||||
|
||||
[dev-dependencies]
|
||||
libp2p-tcp-transport = { path = "../../transports/tcp" }
|
||||
tokio-current-thread = "0.1"
|
||||
tokio-tcp = "0.1"
|
13
protocols/secio/regen_structs_proto.sh
Executable file
13
protocols/secio/regen_structs_proto.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
# This script regenerates the `src/structs_proto.rs` file from `structs.proto`.
|
||||
|
||||
sudo docker run --rm -v `pwd`:/usr/code:z -w /usr/code rust /bin/bash -c " \
|
||||
apt-get update; \
|
||||
apt-get install -y protobuf-compiler; \
|
||||
cargo install --version 2.0.2 protobuf-codegen; \
|
||||
protoc --rust_out . structs.proto"
|
||||
|
||||
sudo chown $USER:$USER *.rs
|
||||
|
||||
mv -f structs.rs ./src/structs_proto.rs
|
95
protocols/secio/src/algo_support.rs
Normal file
95
protocols/secio/src/algo_support.rs
Normal file
@ -0,0 +1,95 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! This module contains some utilities for algorithm support exchange.
|
||||
//!
|
||||
//! One important part of the SECIO handshake is negotiating algorithms. This is what this module
|
||||
//! helps you with.
|
||||
|
||||
macro_rules! supported_impl {
|
||||
($mod_name:ident: $ty:ty, $($name:expr => $val:expr),*,) => (
|
||||
pub mod $mod_name {
|
||||
use std::cmp::Ordering;
|
||||
#[allow(unused_imports)]
|
||||
use stream_cipher::KeySize;
|
||||
#[allow(unused_imports)]
|
||||
use ring::{agreement, digest};
|
||||
use error::SecioError;
|
||||
|
||||
/// String to advertise to the remote.
|
||||
pub const PROPOSITION_STRING: &'static str = concat_comma!($($name),*);
|
||||
|
||||
/// Choose which algorithm to use based on the remote's advertised list.
|
||||
pub fn select_best(hashes_ordering: Ordering, input: &str) -> Result<$ty, SecioError> {
|
||||
match hashes_ordering {
|
||||
Ordering::Less | Ordering::Equal => {
|
||||
for second_elem in input.split(',') {
|
||||
$(
|
||||
if $name == second_elem {
|
||||
return Ok($val);
|
||||
}
|
||||
)+
|
||||
}
|
||||
},
|
||||
Ordering::Greater => {
|
||||
$(
|
||||
for second_elem in input.split(',') {
|
||||
if $name == second_elem {
|
||||
return Ok($val);
|
||||
}
|
||||
}
|
||||
)+
|
||||
},
|
||||
};
|
||||
|
||||
Err(SecioError::NoSupportIntersection(PROPOSITION_STRING, input.to_owned()))
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Concatenates several strings with commas.
|
||||
macro_rules! concat_comma {
|
||||
($first:expr, $($rest:expr),*) => (
|
||||
concat!($first $(, ',', $rest)*)
|
||||
);
|
||||
}
|
||||
|
||||
// TODO: there's no library in the Rust ecosystem that supports P-521, but the Go & JS
|
||||
// implementations advertise it
|
||||
supported_impl!(
|
||||
exchanges: &'static agreement::Algorithm,
|
||||
"P-256" => &agreement::ECDH_P256,
|
||||
"P-384" => &agreement::ECDH_P384,
|
||||
);
|
||||
|
||||
// TODO: the Go & JS implementations advertise Blowfish ; however doing so in Rust leads to
|
||||
// runtime errors
|
||||
supported_impl!(
|
||||
ciphers: KeySize,
|
||||
"AES-128" => KeySize::KeySize128,
|
||||
"AES-256" => KeySize::KeySize256,
|
||||
);
|
||||
|
||||
supported_impl!(
|
||||
hashes: &'static digest::Algorithm,
|
||||
"SHA256" => &digest::SHA256,
|
||||
"SHA512" => &digest::SHA512,
|
||||
);
|
128
protocols/secio/src/codec/decode.rs
Normal file
128
protocols/secio/src/codec/decode.rs
Normal file
@ -0,0 +1,128 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! Individual messages decoding.
|
||||
|
||||
use bytes::BytesMut;
|
||||
use super::StreamCipher;
|
||||
|
||||
use error::SecioError;
|
||||
use futures::sink::Sink;
|
||||
use futures::stream::Stream;
|
||||
use futures::Async;
|
||||
use futures::Poll;
|
||||
use futures::StartSend;
|
||||
use ring::hmac;
|
||||
|
||||
/// Wraps around a `Stream<Item = BytesMut>`. The buffers produced by the underlying stream
|
||||
/// are decoded using the cipher and hmac.
|
||||
///
|
||||
/// This struct implements `Stream`, whose stream item are frames of data without the length
|
||||
/// prefix. The mechanism for removing the length prefix and splitting the incoming data into
|
||||
/// frames isn't handled by this module.
|
||||
///
|
||||
/// Also implements `Sink` for convenience.
|
||||
pub struct DecoderMiddleware<S> {
|
||||
cipher_state: StreamCipher,
|
||||
hmac_key: hmac::VerificationKey,
|
||||
// TODO: when a new version of ring is released, we can use `hmac_key.digest_algorithm().output_len` instead
|
||||
hmac_num_bytes: usize,
|
||||
raw_stream: S,
|
||||
}
|
||||
|
||||
impl<S> DecoderMiddleware<S> {
|
||||
#[inline]
|
||||
pub fn new(
|
||||
raw_stream: S,
|
||||
cipher: StreamCipher,
|
||||
hmac_key: hmac::VerificationKey,
|
||||
hmac_num_bytes: usize, // TODO: remove this parameter
|
||||
) -> DecoderMiddleware<S> {
|
||||
DecoderMiddleware {
|
||||
cipher_state: cipher,
|
||||
hmac_key,
|
||||
raw_stream,
|
||||
hmac_num_bytes,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Stream for DecoderMiddleware<S>
|
||||
where
|
||||
S: Stream<Item = BytesMut>,
|
||||
S::Error: Into<SecioError>,
|
||||
{
|
||||
type Item = Vec<u8>;
|
||||
type Error = SecioError;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
let frame = match self.raw_stream.poll() {
|
||||
Ok(Async::Ready(Some(t))) => t,
|
||||
Ok(Async::Ready(None)) => return Ok(Async::Ready(None)),
|
||||
Ok(Async::NotReady) => return Ok(Async::NotReady),
|
||||
Err(err) => return Err(err.into()),
|
||||
};
|
||||
|
||||
// TODO: when a new version of ring is released, we can use `hmac_key.digest_algorithm().output_len` instead
|
||||
let hmac_num_bytes = self.hmac_num_bytes;
|
||||
|
||||
if frame.len() < hmac_num_bytes {
|
||||
debug!("frame too short when decoding secio frame");
|
||||
return Err(SecioError::FrameTooShort);
|
||||
}
|
||||
let content_length = frame.len() - hmac_num_bytes;
|
||||
{
|
||||
let (crypted_data, expected_hash) = frame.split_at(content_length);
|
||||
debug_assert_eq!(expected_hash.len(), hmac_num_bytes);
|
||||
|
||||
if hmac::verify(&self.hmac_key, crypted_data, expected_hash).is_err() {
|
||||
debug!("hmac mismatch when decoding secio frame");
|
||||
return Err(SecioError::HmacNotMatching);
|
||||
}
|
||||
}
|
||||
|
||||
let mut data_buf = frame.to_vec();
|
||||
data_buf.truncate(content_length);
|
||||
self.cipher_state
|
||||
.try_apply_keystream(&mut data_buf)
|
||||
.map_err::<SecioError,_>(|e|e.into())?;
|
||||
|
||||
Ok(Async::Ready(Some(data_buf)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Sink for DecoderMiddleware<S>
|
||||
where
|
||||
S: Sink,
|
||||
{
|
||||
type SinkItem = S::SinkItem;
|
||||
type SinkError = S::SinkError;
|
||||
|
||||
#[inline]
|
||||
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||
self.raw_stream.start_send(item)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
||||
self.raw_stream.poll_complete()
|
||||
}
|
||||
}
|
93
protocols/secio/src/codec/encode.rs
Normal file
93
protocols/secio/src/codec/encode.rs
Normal file
@ -0,0 +1,93 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! Individual messages encoding.
|
||||
|
||||
use bytes::BytesMut;
|
||||
use super::StreamCipher;
|
||||
use futures::sink::Sink;
|
||||
use futures::stream::Stream;
|
||||
use futures::Poll;
|
||||
use futures::StartSend;
|
||||
use ring::hmac;
|
||||
|
||||
/// Wraps around a `Sink`. Encodes the buffers passed to it and passes it to the underlying sink.
|
||||
///
|
||||
/// This struct implements `Sink`. It expects individual frames of data, and outputs individual
|
||||
/// frames as well, most notably without the length prefix. The mechanism for adding the length
|
||||
/// prefix is not covered by this module.
|
||||
///
|
||||
/// Also implements `Stream` for convenience.
|
||||
pub struct EncoderMiddleware<S> {
|
||||
cipher_state: StreamCipher,
|
||||
hmac_key: hmac::SigningKey,
|
||||
raw_sink: S,
|
||||
}
|
||||
|
||||
impl<S> EncoderMiddleware<S> {
|
||||
pub fn new(
|
||||
raw_sink: S,
|
||||
cipher: StreamCipher,
|
||||
hmac_key: hmac::SigningKey,
|
||||
) -> EncoderMiddleware<S> {
|
||||
EncoderMiddleware {
|
||||
cipher_state: cipher,
|
||||
hmac_key,
|
||||
raw_sink,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Sink for EncoderMiddleware<S>
|
||||
where
|
||||
S: Sink<SinkItem = BytesMut>,
|
||||
{
|
||||
type SinkItem = BytesMut;
|
||||
type SinkError = S::SinkError;
|
||||
|
||||
fn start_send(&mut self, mut data_buf: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||
|
||||
// TODO if SinkError gets refactor to SecioError,
|
||||
// then use try_apply_keystream
|
||||
self.cipher_state.apply_keystream(&mut data_buf[..]);
|
||||
let signature = hmac::sign(&self.hmac_key, &data_buf[..]);
|
||||
data_buf.extend_from_slice(signature.as_ref());
|
||||
self.raw_sink.start_send(data_buf)
|
||||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
||||
self.raw_sink.poll_complete()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Stream for EncoderMiddleware<S>
|
||||
where
|
||||
S: Stream,
|
||||
{
|
||||
type Item = S::Item;
|
||||
type Error = S::Error;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
self.raw_sink.poll()
|
||||
}
|
||||
}
|
172
protocols/secio/src/codec/mod.rs
Normal file
172
protocols/secio/src/codec/mod.rs
Normal file
@ -0,0 +1,172 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! Individual messages encoding and decoding. Use this after the algorithms have been
|
||||
//! successfully negotiated.
|
||||
|
||||
use self::decode::DecoderMiddleware;
|
||||
use self::encode::EncoderMiddleware;
|
||||
|
||||
use aes_ctr::stream_cipher::StreamCipherCore;
|
||||
use ring::hmac;
|
||||
use tokio_io::codec::length_delimited;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
|
||||
mod decode;
|
||||
mod encode;
|
||||
|
||||
/// Type returned by `full_codec`.
|
||||
pub type FullCodec<S> = DecoderMiddleware<EncoderMiddleware<length_delimited::Framed<S>>>;
|
||||
|
||||
pub type StreamCipher = Box<dyn StreamCipherCore + Send>;
|
||||
|
||||
|
||||
/// Takes control of `socket`. Returns an object that implements `future::Sink` and
|
||||
/// `future::Stream`. The `Stream` and `Sink` produce and accept `BytesMut` objects.
|
||||
///
|
||||
/// The conversion between the stream/sink items and the socket is done with the given cipher and
|
||||
/// hash algorithm (which are generally decided during the handshake).
|
||||
pub fn full_codec<S>(
|
||||
socket: length_delimited::Framed<S>,
|
||||
cipher_encoding: StreamCipher,
|
||||
encoding_hmac: hmac::SigningKey,
|
||||
cipher_decoder: StreamCipher,
|
||||
decoding_hmac: hmac::VerificationKey,
|
||||
) -> FullCodec<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
let hmac_num_bytes = encoding_hmac.digest_algorithm().output_len;
|
||||
let encoder = EncoderMiddleware::new(socket, cipher_encoding, encoding_hmac);
|
||||
DecoderMiddleware::new(encoder, cipher_decoder, decoding_hmac, hmac_num_bytes)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate tokio_current_thread;
|
||||
extern crate tokio_tcp;
|
||||
use self::tokio_tcp::TcpListener;
|
||||
use self::tokio_tcp::TcpStream;
|
||||
use stream_cipher::{ctr, KeySize};
|
||||
use super::full_codec;
|
||||
use super::DecoderMiddleware;
|
||||
use super::EncoderMiddleware;
|
||||
use bytes::BytesMut;
|
||||
use error::SecioError;
|
||||
use futures::sync::mpsc::channel;
|
||||
use futures::{Future, Sink, Stream};
|
||||
use rand;
|
||||
use ring::digest::SHA256;
|
||||
use ring::hmac::SigningKey;
|
||||
use ring::hmac::VerificationKey;
|
||||
use std::io::Error as IoError;
|
||||
use tokio_io::codec::length_delimited::Framed;
|
||||
|
||||
const NULL_IV : [u8; 16] = [0;16];
|
||||
|
||||
#[test]
|
||||
fn raw_encode_then_decode() {
|
||||
let (data_tx, data_rx) = channel::<BytesMut>(256);
|
||||
let data_tx = data_tx.sink_map_err::<_, IoError>(|_| panic!());
|
||||
let data_rx = data_rx.map_err::<IoError, _>(|_| panic!());
|
||||
|
||||
let cipher_key: [u8; 32] = rand::random();
|
||||
let hmac_key: [u8; 32] = rand::random();
|
||||
|
||||
|
||||
let encoder = EncoderMiddleware::new(
|
||||
data_tx,
|
||||
ctr(KeySize::KeySize256, &cipher_key, &NULL_IV[..]),
|
||||
SigningKey::new(&SHA256, &hmac_key),
|
||||
);
|
||||
let decoder = DecoderMiddleware::new(
|
||||
data_rx,
|
||||
ctr(KeySize::KeySize256, &cipher_key, &NULL_IV[..]),
|
||||
VerificationKey::new(&SHA256, &hmac_key),
|
||||
32,
|
||||
);
|
||||
|
||||
let data = b"hello world";
|
||||
|
||||
let data_sent = encoder.send(BytesMut::from(data.to_vec())).from_err();
|
||||
let data_received = decoder.into_future().map(|(n, _)| n).map_err(|(e, _)| e);
|
||||
|
||||
let (_, decoded) = tokio_current_thread::block_on_all(data_sent.join(data_received))
|
||||
.map_err(|_| ())
|
||||
.unwrap();
|
||||
assert_eq!(&decoded.unwrap()[..], &data[..]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn full_codec_encode_then_decode() {
|
||||
let cipher_key: [u8; 32] = rand::random();
|
||||
let cipher_key_clone = cipher_key.clone();
|
||||
let hmac_key: [u8; 32] = rand::random();
|
||||
let hmac_key_clone = hmac_key.clone();
|
||||
let data = b"hello world";
|
||||
let data_clone = data.clone();
|
||||
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let listener_addr = listener.local_addr().unwrap();
|
||||
|
||||
let server = listener.incoming().into_future().map_err(|(e, _)| e).map(
|
||||
move |(connec, _)| {
|
||||
let connec = Framed::new(connec.unwrap());
|
||||
|
||||
full_codec(
|
||||
connec,
|
||||
ctr(KeySize::KeySize256, &cipher_key, &NULL_IV[..]),
|
||||
SigningKey::new(&SHA256, &hmac_key),
|
||||
ctr(KeySize::KeySize256, &cipher_key, &NULL_IV[..]),
|
||||
VerificationKey::new(&SHA256, &hmac_key),
|
||||
)
|
||||
},
|
||||
);
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.map(move |stream| {
|
||||
let stream = Framed::new(stream);
|
||||
|
||||
full_codec(
|
||||
stream,
|
||||
ctr(KeySize::KeySize256, &cipher_key_clone, &NULL_IV[..]),
|
||||
SigningKey::new(&SHA256, &hmac_key_clone),
|
||||
ctr(KeySize::KeySize256, &cipher_key_clone, &NULL_IV[..]),
|
||||
VerificationKey::new(&SHA256, &hmac_key_clone),
|
||||
)
|
||||
});
|
||||
|
||||
let fin = server
|
||||
.join(client)
|
||||
.from_err::<SecioError>()
|
||||
.and_then(|(server, client)| {
|
||||
client
|
||||
.send(BytesMut::from(&data_clone[..]))
|
||||
.map(move |_| server)
|
||||
.from_err()
|
||||
})
|
||||
.and_then(|server| server.into_future().map_err(|(e, _)| e.into()))
|
||||
.map(|recved| recved.0.unwrap().to_vec());
|
||||
|
||||
let received = tokio_current_thread::block_on_all(fin).unwrap();
|
||||
assert_eq!(received, data);
|
||||
}
|
||||
}
|
126
protocols/secio/src/error.rs
Normal file
126
protocols/secio/src/error.rs
Normal file
@ -0,0 +1,126 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! Defines the `SecioError` enum that groups all possible errors in SECIO.
|
||||
|
||||
use aes_ctr::stream_cipher::LoopError;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io::Error as IoError;
|
||||
|
||||
/// Error at the SECIO layer communication.
|
||||
#[derive(Debug)]
|
||||
pub enum SecioError {
|
||||
/// I/O error.
|
||||
IoError(IoError),
|
||||
|
||||
/// Failed to parse one of the handshake protobuf messages.
|
||||
HandshakeParsingFailure,
|
||||
|
||||
/// There is no protocol supported by both the local and remote hosts.
|
||||
NoSupportIntersection(&'static str, String),
|
||||
|
||||
/// Failed to generate nonce.
|
||||
NonceGenerationFailed,
|
||||
|
||||
/// Failed to generate ephemeral key.
|
||||
EphemeralKeyGenerationFailed,
|
||||
|
||||
/// Failed to sign a message with our local private key.
|
||||
SigningFailure,
|
||||
|
||||
/// The signature of the exchange packet doesn't verify the remote public key.
|
||||
SignatureVerificationFailed,
|
||||
|
||||
/// Failed to generate the secret shared key from the ephemeral key.
|
||||
SecretGenerationFailed,
|
||||
|
||||
/// The final check of the handshake failed.
|
||||
NonceVerificationFailed,
|
||||
|
||||
/// Error with block cipher.
|
||||
CipherError(LoopError),
|
||||
|
||||
/// The received frame was of invalid length.
|
||||
FrameTooShort,
|
||||
|
||||
/// The hashes of the message didn't match.
|
||||
HmacNotMatching,
|
||||
}
|
||||
|
||||
impl error::Error for SecioError {
|
||||
#[inline]
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
SecioError::IoError(_) => "I/O error",
|
||||
SecioError::HandshakeParsingFailure => {
|
||||
"Failed to parse one of the handshake protobuf messages"
|
||||
}
|
||||
SecioError::NoSupportIntersection(_, _) => {
|
||||
"There is no protocol supported by both the local and remote hosts"
|
||||
}
|
||||
SecioError::NonceGenerationFailed => "Failed to generate nonce",
|
||||
SecioError::EphemeralKeyGenerationFailed => "Failed to generate ephemeral key",
|
||||
SecioError::SigningFailure => "Failed to sign a message with our local private key",
|
||||
SecioError::SignatureVerificationFailed => {
|
||||
"The signature of the exchange packet doesn't verify the remote public key"
|
||||
}
|
||||
SecioError::SecretGenerationFailed => {
|
||||
"Failed to generate the secret shared key from the ephemeral key"
|
||||
}
|
||||
SecioError::NonceVerificationFailed => "The final check of the handshake failed",
|
||||
SecioError::CipherError(_) => "Error while decoding/encoding data",
|
||||
SecioError::FrameTooShort => "The received frame was of invalid length",
|
||||
SecioError::HmacNotMatching => "The hashes of the message didn't match",
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
SecioError::IoError(ref err) => Some(err),
|
||||
// TODO: The type doesn't implement `Error`
|
||||
/*SecioError::CipherError(ref err) => {
|
||||
Some(err)
|
||||
},*/
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SecioError {
|
||||
#[inline]
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||
write!(fmt, "{}", error::Error::description(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LoopError> for SecioError {
|
||||
#[inline]
|
||||
fn from(err: LoopError) -> SecioError {
|
||||
SecioError::CipherError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<IoError> for SecioError {
|
||||
#[inline]
|
||||
fn from(err: IoError) -> SecioError {
|
||||
SecioError::IoError(err)
|
||||
}
|
||||
}
|
685
protocols/secio/src/handshake.rs
Normal file
685
protocols/secio/src/handshake.rs
Normal file
@ -0,0 +1,685 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
use algo_support;
|
||||
use bytes::BytesMut;
|
||||
use codec::{full_codec, FullCodec};
|
||||
use stream_cipher::{KeySize, ctr};
|
||||
use error::SecioError;
|
||||
use futures::future;
|
||||
use futures::sink::Sink;
|
||||
use futures::stream::Stream;
|
||||
use futures::Future;
|
||||
use libp2p_core::PublicKey;
|
||||
use protobuf::parse_from_bytes as protobuf_parse_from_bytes;
|
||||
use protobuf::Message as ProtobufMessage;
|
||||
use ring::agreement::EphemeralPrivateKey;
|
||||
use ring::hmac::{SigningContext, SigningKey, VerificationKey};
|
||||
use ring::rand::SecureRandom;
|
||||
use ring::signature::verify as signature_verify;
|
||||
use ring::signature::{ED25519, RSASigningState, RSA_PKCS1_2048_8192_SHA256, RSA_PKCS1_SHA256};
|
||||
use ring::{agreement, digest, rand};
|
||||
#[cfg(feature = "secp256k1")]
|
||||
use secp256k1;
|
||||
use std::cmp::{self, Ordering};
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::mem;
|
||||
use structs_proto::{Exchange, Propose};
|
||||
use tokio_io::codec::length_delimited;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use untrusted::Input as UntrustedInput;
|
||||
use {SecioKeyPair, SecioKeyPairInner};
|
||||
|
||||
/// Performs a handshake on the given socket.
|
||||
///
|
||||
/// This function expects that the remote is identified with `remote_public_key`, and the remote
|
||||
/// will expect that we are identified with `local_key`.Any mismatch somewhere will produce a
|
||||
/// `SecioError`.
|
||||
///
|
||||
/// On success, returns an object that implements the `Sink` and `Stream` trait whose items are
|
||||
/// buffers of data, plus the public key of the remote, plus the ephemeral public key used during
|
||||
/// negotiation.
|
||||
pub fn handshake<'a, S: 'a>(
|
||||
socket: S,
|
||||
local_key: SecioKeyPair,
|
||||
) -> Box<Future<Item = (FullCodec<S>, PublicKey, Vec<u8>), Error = SecioError> + 'a>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
// TODO: could be rewritten as a coroutine once coroutines land in stable Rust
|
||||
|
||||
// This struct contains the whole context of a handshake, and is filled progressively
|
||||
// throughout the various parts of the handshake.
|
||||
struct HandshakeContext {
|
||||
// Filled with this function's parameters.
|
||||
local_key: SecioKeyPair,
|
||||
|
||||
rng: rand::SystemRandom,
|
||||
// Locally-generated random number. The array size can be changed without any repercussion.
|
||||
local_nonce: [u8; 16],
|
||||
|
||||
// Our local proposition's raw bytes.
|
||||
local_public_key_in_protobuf_bytes: Vec<u8>,
|
||||
local_proposition_bytes: Vec<u8>,
|
||||
|
||||
// The remote proposition's raw bytes.
|
||||
remote_proposition_bytes: BytesMut,
|
||||
remote_public_key_in_protobuf_bytes: Vec<u8>,
|
||||
remote_public_key: Option<PublicKey>,
|
||||
|
||||
// The remote peer's version of `local_nonce`.
|
||||
// If the NONCE size is actually part of the protocol, we can change this to a fixed-size
|
||||
// array instead of a `Vec`.
|
||||
remote_nonce: Vec<u8>,
|
||||
|
||||
// Set to `ordering(
|
||||
// hash(concat(remote-pubkey, local-none)),
|
||||
// hash(concat(local-pubkey, remote-none))
|
||||
// )`.
|
||||
// `Ordering::Equal` is an invalid value (as it would mean we're talking to ourselves).
|
||||
//
|
||||
// Since everything is symmetrical, this value is used to determine what should be ours
|
||||
// and what should be the remote's.
|
||||
hashes_ordering: Ordering,
|
||||
|
||||
// Crypto algorithms chosen for the communication.
|
||||
chosen_exchange: Option<&'static agreement::Algorithm>,
|
||||
// We only support AES for now, so store just a key size.
|
||||
chosen_cipher: Option<KeySize>,
|
||||
chosen_hash: Option<&'static digest::Algorithm>,
|
||||
|
||||
// Ephemeral key generated for the handshake and then thrown away.
|
||||
local_tmp_priv_key: Option<EphemeralPrivateKey>,
|
||||
local_tmp_pub_key: Vec<u8>,
|
||||
}
|
||||
|
||||
let context = HandshakeContext {
|
||||
local_key,
|
||||
rng: rand::SystemRandom::new(),
|
||||
local_nonce: Default::default(),
|
||||
local_public_key_in_protobuf_bytes: Vec::new(),
|
||||
local_proposition_bytes: Vec::new(),
|
||||
remote_proposition_bytes: BytesMut::new(),
|
||||
remote_public_key_in_protobuf_bytes: Vec::new(),
|
||||
remote_public_key: None,
|
||||
remote_nonce: Vec::new(),
|
||||
hashes_ordering: Ordering::Equal,
|
||||
chosen_exchange: None,
|
||||
chosen_cipher: None,
|
||||
chosen_hash: None,
|
||||
local_tmp_priv_key: None,
|
||||
local_tmp_pub_key: Vec::new(),
|
||||
};
|
||||
|
||||
// The handshake messages all start with a 4-bytes message length prefix.
|
||||
let socket = length_delimited::Builder::new()
|
||||
.big_endian()
|
||||
.length_field_length(4)
|
||||
.new_framed(socket);
|
||||
|
||||
let future = future::ok::<_, SecioError>(context)
|
||||
// Generate our nonce.
|
||||
.and_then(|mut context| {
|
||||
context.rng.fill(&mut context.local_nonce)
|
||||
.map_err(|_| SecioError::NonceGenerationFailed)?;
|
||||
trace!("starting handshake ; local nonce = {:?}", context.local_nonce);
|
||||
Ok(context)
|
||||
})
|
||||
|
||||
// Send our proposition with our nonce, public key and supported protocols.
|
||||
.and_then(|mut context| {
|
||||
context.local_public_key_in_protobuf_bytes = context.local_key.to_public_key().into_protobuf_encoding();
|
||||
|
||||
let mut proposition = Propose::new();
|
||||
proposition.set_rand(context.local_nonce.to_vec());
|
||||
proposition.set_pubkey(context.local_public_key_in_protobuf_bytes.clone());
|
||||
proposition.set_exchanges(algo_support::exchanges::PROPOSITION_STRING.into());
|
||||
proposition.set_ciphers(algo_support::ciphers::PROPOSITION_STRING.into());
|
||||
proposition.set_hashes(algo_support::hashes::PROPOSITION_STRING.into());
|
||||
let proposition_bytes = proposition.write_to_bytes().unwrap();
|
||||
context.local_proposition_bytes = proposition_bytes.clone();
|
||||
|
||||
trace!("sending proposition to remote");
|
||||
|
||||
socket.send(BytesMut::from(proposition_bytes.clone()))
|
||||
.from_err()
|
||||
.map(|s| (s, context))
|
||||
})
|
||||
|
||||
// Receive the remote's proposition.
|
||||
.and_then(move |(socket, mut context)| {
|
||||
socket.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(move |(prop_raw, socket)| {
|
||||
match prop_raw {
|
||||
Some(p) => context.remote_proposition_bytes = p,
|
||||
None => {
|
||||
let err = IoError::new(IoErrorKind::BrokenPipe, "unexpected eof");
|
||||
debug!("unexpected eof while waiting for remote's proposition");
|
||||
return Err(err.into())
|
||||
},
|
||||
};
|
||||
|
||||
let mut prop = match protobuf_parse_from_bytes::<Propose>(
|
||||
&context.remote_proposition_bytes
|
||||
) {
|
||||
Ok(prop) => prop,
|
||||
Err(_) => {
|
||||
debug!("failed to parse remote's proposition protobuf message");
|
||||
return Err(SecioError::HandshakeParsingFailure);
|
||||
}
|
||||
};
|
||||
context.remote_public_key_in_protobuf_bytes = prop.take_pubkey();
|
||||
let pubkey = match PublicKey::from_protobuf_encoding(&context.remote_public_key_in_protobuf_bytes) {
|
||||
Ok(p) => p,
|
||||
Err(_) => {
|
||||
debug!("failed to parse remote's proposition's pubkey protobuf");
|
||||
return Err(SecioError::HandshakeParsingFailure);
|
||||
},
|
||||
};
|
||||
|
||||
context.remote_nonce = prop.take_rand();
|
||||
context.remote_public_key = Some(pubkey);
|
||||
trace!("received proposition from remote ; pubkey = {:?} ; nonce = {:?}",
|
||||
context.remote_public_key, context.remote_nonce);
|
||||
Ok((prop, socket, context))
|
||||
})
|
||||
})
|
||||
|
||||
// Decide which algorithms to use (thanks to the remote's proposition).
|
||||
.and_then(move |(remote_prop, socket, mut context)| {
|
||||
// In order to determine which protocols to use, we compute two hashes and choose
|
||||
// based on which hash is larger.
|
||||
context.hashes_ordering = {
|
||||
let oh1 = {
|
||||
let mut ctx = digest::Context::new(&digest::SHA256);
|
||||
ctx.update(&context.remote_public_key_in_protobuf_bytes);
|
||||
ctx.update(&context.local_nonce);
|
||||
ctx.finish()
|
||||
};
|
||||
|
||||
let oh2 = {
|
||||
let mut ctx = digest::Context::new(&digest::SHA256);
|
||||
ctx.update(&context.local_public_key_in_protobuf_bytes);
|
||||
ctx.update(&context.remote_nonce);
|
||||
ctx.finish()
|
||||
};
|
||||
|
||||
oh1.as_ref().cmp(&oh2.as_ref())
|
||||
};
|
||||
|
||||
context.chosen_exchange = {
|
||||
let list = &remote_prop.get_exchanges();
|
||||
Some(match algo_support::exchanges::select_best(context.hashes_ordering, list) {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
debug!("failed to select an exchange protocol");
|
||||
return Err(err);
|
||||
}
|
||||
})
|
||||
};
|
||||
context.chosen_cipher = {
|
||||
let list = &remote_prop.get_ciphers();
|
||||
Some(match algo_support::ciphers::select_best(context.hashes_ordering, list) {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
debug!("failed to select a cipher protocol");
|
||||
return Err(err);
|
||||
}
|
||||
})
|
||||
};
|
||||
context.chosen_hash = {
|
||||
let list = &remote_prop.get_hashes();
|
||||
Some(match algo_support::hashes::select_best(context.hashes_ordering, list) {
|
||||
Ok(a) => a,
|
||||
Err(err) => {
|
||||
debug!("failed to select a hash protocol");
|
||||
return Err(err);
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
Ok((socket, context))
|
||||
})
|
||||
|
||||
// Generate an ephemeral key for the negotiation.
|
||||
.and_then(|(socket, context)| {
|
||||
match EphemeralPrivateKey::generate(context.chosen_exchange.as_ref().unwrap(), &context.rng) {
|
||||
Ok(tmp_priv_key) => Ok((socket, context, tmp_priv_key)),
|
||||
Err(_) => {
|
||||
debug!("failed to generate ECDH key");
|
||||
Err(SecioError::EphemeralKeyGenerationFailed)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// Send the ephemeral pub key to the remote in an `Exchange` struct. The `Exchange` also
|
||||
// contains a signature of the two propositions encoded with our static public key.
|
||||
.and_then(|(socket, mut context, tmp_priv)| {
|
||||
let exchange = {
|
||||
let mut local_tmp_pub_key: Vec<u8> = (0 .. tmp_priv.public_key_len()).map(|_| 0).collect();
|
||||
tmp_priv.compute_public_key(&mut local_tmp_pub_key).unwrap();
|
||||
context.local_tmp_priv_key = Some(tmp_priv);
|
||||
|
||||
let mut data_to_sign = context.local_proposition_bytes.clone();
|
||||
data_to_sign.extend_from_slice(&context.remote_proposition_bytes);
|
||||
data_to_sign.extend_from_slice(&local_tmp_pub_key);
|
||||
|
||||
let mut exchange = Exchange::new();
|
||||
exchange.set_epubkey(local_tmp_pub_key.clone());
|
||||
exchange.set_signature({
|
||||
match context.local_key.inner {
|
||||
SecioKeyPairInner::Rsa { ref private, .. } => {
|
||||
let mut state = match RSASigningState::new(private.clone()) {
|
||||
Ok(s) => s,
|
||||
Err(_) => {
|
||||
debug!("failed to sign local exchange");
|
||||
return Err(SecioError::SigningFailure);
|
||||
},
|
||||
};
|
||||
let mut signature = vec![0; private.public_modulus_len()];
|
||||
match state.sign(&RSA_PKCS1_SHA256, &context.rng, &data_to_sign,
|
||||
&mut signature)
|
||||
{
|
||||
Ok(_) => (),
|
||||
Err(_) => {
|
||||
debug!("failed to sign local exchange");
|
||||
return Err(SecioError::SigningFailure);
|
||||
},
|
||||
};
|
||||
|
||||
signature
|
||||
},
|
||||
SecioKeyPairInner::Ed25519 { ref key_pair } => {
|
||||
let signature = key_pair.sign(&data_to_sign);
|
||||
signature.as_ref().to_owned()
|
||||
},
|
||||
#[cfg(feature = "secp256k1")]
|
||||
SecioKeyPairInner::Secp256k1 { ref private } => {
|
||||
let data_to_sign = digest::digest(&digest::SHA256, &data_to_sign);
|
||||
let message = secp256k1::Message::from_slice(data_to_sign.as_ref())
|
||||
.expect("digest output length doesn't match secp256k1 input length");
|
||||
let secp256k1 = secp256k1::Secp256k1::with_caps(secp256k1::ContextFlag::SignOnly);
|
||||
secp256k1
|
||||
.sign(&message, private)
|
||||
.expect("failed to sign message")
|
||||
.serialize_der(&secp256k1)
|
||||
},
|
||||
}
|
||||
});
|
||||
exchange
|
||||
};
|
||||
|
||||
let local_exch = exchange.write_to_bytes()
|
||||
.expect("can only fail if the protobuf msg is malformed, which can't happen for \
|
||||
this message in particular");
|
||||
Ok((BytesMut::from(local_exch), socket, context))
|
||||
})
|
||||
|
||||
// Send our local `Exchange`.
|
||||
.and_then(|(local_exch, socket, context)| {
|
||||
trace!("sending exchange to remote");
|
||||
socket.send(local_exch)
|
||||
.from_err()
|
||||
.map(|s| (s, context))
|
||||
})
|
||||
|
||||
// Receive the remote's `Exchange`.
|
||||
.and_then(move |(socket, context)| {
|
||||
socket.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(move |(raw, socket)| {
|
||||
let raw = match raw {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
let err = IoError::new(IoErrorKind::BrokenPipe, "unexpected eof");
|
||||
debug!("unexpected eof while waiting for remote's exchange");
|
||||
return Err(err.into())
|
||||
},
|
||||
};
|
||||
|
||||
let remote_exch = match protobuf_parse_from_bytes::<Exchange>(&raw) {
|
||||
Ok(e) => e,
|
||||
Err(err) => {
|
||||
debug!("failed to parse remote's exchange protobuf ; {:?}", err);
|
||||
return Err(SecioError::HandshakeParsingFailure);
|
||||
}
|
||||
};
|
||||
|
||||
trace!("received and decoded the remote's exchange");
|
||||
Ok((remote_exch, socket, context))
|
||||
})
|
||||
})
|
||||
|
||||
// Check the validity of the remote's `Exchange`. This verifies that the remote was really
|
||||
// the sender of its proposition, and that it is the owner of both its global and ephemeral
|
||||
// keys.
|
||||
.and_then(|(remote_exch, socket, context)| {
|
||||
let mut data_to_verify = context.remote_proposition_bytes.clone();
|
||||
data_to_verify.extend_from_slice(&context.local_proposition_bytes);
|
||||
data_to_verify.extend_from_slice(remote_exch.get_epubkey());
|
||||
|
||||
match context.remote_public_key {
|
||||
Some(PublicKey::Rsa(ref remote_public_key)) => {
|
||||
// TODO: The ring library doesn't like some stuff in our DER public key,
|
||||
// therefore we scrap the first 24 bytes of the key. A proper fix would
|
||||
// be to write a DER parser, but that's not trivial.
|
||||
match signature_verify(&RSA_PKCS1_2048_8192_SHA256,
|
||||
UntrustedInput::from(&remote_public_key[24..]),
|
||||
UntrustedInput::from(&data_to_verify),
|
||||
UntrustedInput::from(remote_exch.get_signature()))
|
||||
{
|
||||
Ok(()) => (),
|
||||
Err(_) => {
|
||||
debug!("failed to verify the remote's signature");
|
||||
return Err(SecioError::SignatureVerificationFailed)
|
||||
},
|
||||
}
|
||||
},
|
||||
Some(PublicKey::Ed25519(ref remote_public_key)) => {
|
||||
match signature_verify(&ED25519,
|
||||
UntrustedInput::from(remote_public_key),
|
||||
UntrustedInput::from(&data_to_verify),
|
||||
UntrustedInput::from(remote_exch.get_signature()))
|
||||
{
|
||||
Ok(()) => (),
|
||||
Err(_) => {
|
||||
debug!("failed to verify the remote's signature");
|
||||
return Err(SecioError::SignatureVerificationFailed)
|
||||
},
|
||||
}
|
||||
},
|
||||
#[cfg(feature = "secp256k1")]
|
||||
Some(PublicKey::Secp256k1(ref remote_public_key)) => {
|
||||
let data_to_verify = digest::digest(&digest::SHA256, &data_to_verify);
|
||||
let message = secp256k1::Message::from_slice(data_to_verify.as_ref())
|
||||
.expect("digest output length doesn't match secp256k1 input length");
|
||||
let secp256k1 = secp256k1::Secp256k1::with_caps(secp256k1::ContextFlag::VerifyOnly);
|
||||
let signature = secp256k1::Signature::from_der(&secp256k1, remote_exch.get_signature());
|
||||
let remote_public_key = secp256k1::key::PublicKey::from_slice(&secp256k1, remote_public_key);
|
||||
if let (Ok(signature), Ok(remote_public_key)) = (signature, remote_public_key) {
|
||||
match secp256k1.verify(&message, &signature, &remote_public_key) {
|
||||
Ok(()) => (),
|
||||
Err(_) => {
|
||||
debug!("failed to verify the remote's signature");
|
||||
return Err(SecioError::SignatureVerificationFailed)
|
||||
},
|
||||
}
|
||||
} else {
|
||||
debug!("remote's secp256k1 signature has wrong format");
|
||||
return Err(SecioError::SignatureVerificationFailed)
|
||||
}
|
||||
},
|
||||
#[cfg(not(feature = "secp256k1"))]
|
||||
Some(PublicKey::Secp256k1(_)) => {
|
||||
debug!("support for secp256k1 was disabled at compile-time");
|
||||
return Err(SecioError::SignatureVerificationFailed);
|
||||
},
|
||||
None => unreachable!("we store a Some in the remote public key before reaching \
|
||||
this point")
|
||||
};
|
||||
|
||||
trace!("successfully verified the remote's signature");
|
||||
Ok((remote_exch, socket, context))
|
||||
})
|
||||
|
||||
// Generate a key from the local ephemeral private key and the remote ephemeral public key,
|
||||
// derive from it a ciper key, an iv, and a hmac key, and build the encoder/decoder.
|
||||
.and_then(|(remote_exch, socket, mut context)| {
|
||||
let local_priv_key = context.local_tmp_priv_key.take()
|
||||
.expect("we filled this Option earlier, and extract it now");
|
||||
let codec = agreement::agree_ephemeral(local_priv_key,
|
||||
&context.chosen_exchange.unwrap(),
|
||||
UntrustedInput::from(remote_exch.get_epubkey()),
|
||||
SecioError::SecretGenerationFailed,
|
||||
|key_material| {
|
||||
let key = SigningKey::new(context.chosen_hash.unwrap(), key_material);
|
||||
|
||||
let chosen_cipher = context.chosen_cipher.unwrap();
|
||||
let (cipher_key_size, iv_size) = match chosen_cipher {
|
||||
KeySize::KeySize128 => (16, 16),
|
||||
KeySize::KeySize256 => (32, 16),
|
||||
};
|
||||
|
||||
let mut longer_key = vec![0u8; 2 * (iv_size + cipher_key_size + 20)];
|
||||
stretch_key(&key, &mut longer_key);
|
||||
|
||||
let (local_infos, remote_infos) = {
|
||||
let (first_half, second_half) = longer_key.split_at(longer_key.len() / 2);
|
||||
match context.hashes_ordering {
|
||||
Ordering::Equal => panic!(),
|
||||
Ordering::Less => (second_half, first_half),
|
||||
Ordering::Greater => (first_half, second_half),
|
||||
}
|
||||
};
|
||||
|
||||
let (encoding_cipher, encoding_hmac) = {
|
||||
let (iv, rest) = local_infos.split_at(iv_size);
|
||||
let (cipher_key, mac_key) = rest.split_at(cipher_key_size);
|
||||
let hmac = SigningKey::new(&context.chosen_hash.unwrap(), mac_key);
|
||||
let cipher = ctr(chosen_cipher, cipher_key, iv);
|
||||
(cipher, hmac)
|
||||
};
|
||||
|
||||
let (decoding_cipher, decoding_hmac) = {
|
||||
let (iv, rest) = remote_infos.split_at(iv_size);
|
||||
let (cipher_key, mac_key) = rest.split_at(cipher_key_size);
|
||||
let hmac = VerificationKey::new(&context.chosen_hash.unwrap(), mac_key);
|
||||
let cipher = ctr(chosen_cipher, cipher_key, iv);
|
||||
(cipher, hmac)
|
||||
};
|
||||
|
||||
Ok(full_codec(socket, encoding_cipher, encoding_hmac,
|
||||
decoding_cipher, decoding_hmac))
|
||||
});
|
||||
|
||||
match codec {
|
||||
Ok(c) => Ok((c, context)),
|
||||
Err(err) => {
|
||||
debug!("failed to generate shared secret with remote");
|
||||
Err(err)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
// We send back their nonce to check if the connection works.
|
||||
.and_then(|(codec, mut context)| {
|
||||
let remote_nonce = mem::replace(&mut context.remote_nonce, Vec::new());
|
||||
trace!("checking encryption by sending back remote's nonce");
|
||||
codec.send(BytesMut::from(remote_nonce))
|
||||
.map(|s| (s, context))
|
||||
.from_err()
|
||||
})
|
||||
|
||||
// Check that the received nonce is correct.
|
||||
.and_then(|(codec, context)| {
|
||||
codec.into_future()
|
||||
.map_err(|(e, _)| e)
|
||||
.and_then(move |(nonce, rest)| {
|
||||
match nonce {
|
||||
Some(ref n) if n == &context.local_nonce => {
|
||||
trace!("secio handshake success");
|
||||
Ok((rest, context.remote_public_key.expect("we stored a Some earlier"), context.local_tmp_pub_key))
|
||||
},
|
||||
None => {
|
||||
debug!("unexpected eof during nonce check");
|
||||
Err(IoError::new(IoErrorKind::BrokenPipe, "unexpected eof").into())
|
||||
},
|
||||
_ => {
|
||||
debug!("failed nonce verification with remote");
|
||||
Err(SecioError::NonceVerificationFailed)
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
Box::new(future)
|
||||
}
|
||||
|
||||
// Custom algorithm translated from reference implementations. Needs to be the same algorithm
|
||||
// amongst all implementations.
|
||||
fn stretch_key(key: &SigningKey, result: &mut [u8]) {
|
||||
const SEED: &[u8] = b"key expansion";
|
||||
|
||||
let mut init_ctxt = SigningContext::with_key(key);
|
||||
init_ctxt.update(SEED);
|
||||
let mut a = init_ctxt.sign();
|
||||
|
||||
let mut j = 0;
|
||||
while j < result.len() {
|
||||
let mut context = SigningContext::with_key(key);
|
||||
context.update(a.as_ref());
|
||||
context.update(SEED);
|
||||
let b = context.sign();
|
||||
|
||||
let todo = cmp::min(b.as_ref().len(), result.len() - j);
|
||||
|
||||
result[j..j + todo].copy_from_slice(&b.as_ref()[..todo]);
|
||||
|
||||
j += todo;
|
||||
|
||||
let mut context = SigningContext::with_key(key);
|
||||
context.update(a.as_ref());
|
||||
a = context.sign();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
extern crate tokio_current_thread;
|
||||
extern crate tokio_tcp;
|
||||
use self::tokio_tcp::TcpListener;
|
||||
use self::tokio_tcp::TcpStream;
|
||||
use super::handshake;
|
||||
use super::stretch_key;
|
||||
use futures::Future;
|
||||
use futures::Stream;
|
||||
use ring::digest::SHA256;
|
||||
use ring::hmac::SigningKey;
|
||||
use SecioKeyPair;
|
||||
|
||||
#[test]
|
||||
fn handshake_with_self_succeeds_rsa() {
|
||||
let key1 = {
|
||||
let private = include_bytes!("../tests/test-rsa-private-key.pk8");
|
||||
let public = include_bytes!("../tests/test-rsa-public-key.der").to_vec();
|
||||
SecioKeyPair::rsa_from_pkcs8(private, public).unwrap()
|
||||
};
|
||||
|
||||
let key2 = {
|
||||
let private = include_bytes!("../tests/test-rsa-private-key-2.pk8");
|
||||
let public = include_bytes!("../tests/test-rsa-public-key-2.der").to_vec();
|
||||
SecioKeyPair::rsa_from_pkcs8(private, public).unwrap()
|
||||
};
|
||||
|
||||
handshake_with_self_succeeds(key1, key2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn handshake_with_self_succeeds_ed25519() {
|
||||
let key1 = SecioKeyPair::ed25519_generated().unwrap();
|
||||
let key2 = SecioKeyPair::ed25519_generated().unwrap();
|
||||
handshake_with_self_succeeds(key1, key2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "secp256k1")]
|
||||
fn handshake_with_self_succeeds_secp256k1() {
|
||||
let key1 = {
|
||||
let key = include_bytes!("../tests/test-secp256k1-private-key.der");
|
||||
SecioKeyPair::secp256k1_from_der(&key[..]).unwrap()
|
||||
};
|
||||
|
||||
let key2 = {
|
||||
let key = include_bytes!("../tests/test-secp256k1-private-key-2.der");
|
||||
SecioKeyPair::secp256k1_from_der(&key[..]).unwrap()
|
||||
};
|
||||
|
||||
handshake_with_self_succeeds(key1, key2);
|
||||
}
|
||||
|
||||
fn handshake_with_self_succeeds(key1: SecioKeyPair, key2: SecioKeyPair) {
|
||||
let listener = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
|
||||
let listener_addr = listener.local_addr().unwrap();
|
||||
|
||||
let server = listener
|
||||
.incoming()
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e.into())
|
||||
.and_then(move |(connec, _)| handshake(connec.unwrap(), key1));
|
||||
|
||||
let client = TcpStream::connect(&listener_addr)
|
||||
.map_err(|e| e.into())
|
||||
.and_then(move |stream| handshake(stream, key2));
|
||||
|
||||
tokio_current_thread::block_on_all(server.join(client)).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stretch() {
|
||||
let mut output = [0u8; 32];
|
||||
|
||||
let key1 = SigningKey::new(&SHA256, &[]);
|
||||
stretch_key(&key1, &mut output);
|
||||
assert_eq!(
|
||||
&output,
|
||||
&[
|
||||
103, 144, 60, 199, 85, 145, 239, 71, 79, 198, 85, 164, 32, 53, 143, 205, 50, 48,
|
||||
153, 10, 37, 32, 85, 1, 226, 61, 193, 1, 154, 120, 207, 80,
|
||||
]
|
||||
);
|
||||
|
||||
let key2 = SigningKey::new(
|
||||
&SHA256,
|
||||
&[
|
||||
157, 166, 80, 144, 77, 193, 198, 6, 23, 220, 87, 220, 191, 72, 168, 197, 54, 33,
|
||||
219, 225, 84, 156, 165, 37, 149, 224, 244, 32, 170, 79, 125, 35, 171, 26, 178, 176,
|
||||
92, 168, 22, 27, 205, 44, 229, 61, 152, 21, 222, 81, 241, 81, 116, 236, 74, 166,
|
||||
89, 145, 5, 162, 108, 230, 55, 54, 9, 17,
|
||||
],
|
||||
);
|
||||
stretch_key(&key2, &mut output);
|
||||
assert_eq!(
|
||||
&output,
|
||||
&[
|
||||
39, 151, 182, 63, 180, 175, 224, 139, 42, 131, 130, 116, 55, 146, 62, 31, 157, 95,
|
||||
217, 15, 73, 81, 10, 83, 243, 141, 64, 227, 103, 144, 99, 121,
|
||||
]
|
||||
);
|
||||
|
||||
let key3 = SigningKey::new(
|
||||
&SHA256,
|
||||
&[
|
||||
98, 219, 94, 104, 97, 70, 139, 13, 185, 110, 56, 36, 66, 3, 80, 224, 32, 205, 102,
|
||||
170, 59, 32, 140, 245, 86, 102, 231, 68, 85, 249, 227, 243, 57, 53, 171, 36, 62,
|
||||
225, 178, 74, 89, 142, 151, 94, 183, 231, 208, 166, 244, 130, 130, 209, 248, 65,
|
||||
19, 48, 127, 127, 55, 82, 117, 154, 124, 108,
|
||||
],
|
||||
);
|
||||
stretch_key(&key3, &mut output);
|
||||
assert_eq!(
|
||||
&output,
|
||||
&[
|
||||
28, 39, 158, 206, 164, 16, 211, 194, 99, 43, 208, 36, 24, 141, 90, 93, 157, 236,
|
||||
238, 111, 170, 0, 60, 11, 49, 174, 177, 121, 30, 12, 182, 25,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
394
protocols/secio/src/lib.rs
Normal file
394
protocols/secio/src/lib.rs
Normal file
@ -0,0 +1,394 @@
|
||||
// Copyright 2017 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.
|
||||
|
||||
//! The `secio` protocol is a middleware that will encrypt and decrypt communications going
|
||||
//! through a socket (or anything that implements `AsyncRead + AsyncWrite`).
|
||||
//!
|
||||
//! # Connection upgrade
|
||||
//!
|
||||
//! The `SecioConfig` struct implements the `ConnectionUpgrade` trait. You can apply it over a
|
||||
//! `Transport` by using the `with_upgrade` method. The returned object will also implement
|
||||
//! `Transport` and will automatically apply the secio protocol over any connection that is opened
|
||||
//! through it.
|
||||
//!
|
||||
//! ```no_run
|
||||
//! extern crate futures;
|
||||
//! extern crate tokio_current_thread;
|
||||
//! extern crate tokio_io;
|
||||
//! extern crate libp2p_core;
|
||||
//! extern crate libp2p_secio;
|
||||
//! extern crate libp2p_tcp_transport;
|
||||
//!
|
||||
//! # fn main() {
|
||||
//! use futures::Future;
|
||||
//! use libp2p_secio::{SecioConfig, SecioKeyPair, SecioOutput};
|
||||
//! use libp2p_core::{Multiaddr, Transport, upgrade};
|
||||
//! use libp2p_tcp_transport::TcpConfig;
|
||||
//! use tokio_io::io::write_all;
|
||||
//!
|
||||
//! let transport = TcpConfig::new()
|
||||
//! .with_upgrade({
|
||||
//! # let private_key = b"";
|
||||
//! //let private_key = include_bytes!("test-rsa-private-key.pk8");
|
||||
//! # let public_key = vec![];
|
||||
//! //let public_key = include_bytes!("test-rsa-public-key.der").to_vec();
|
||||
//! let upgrade = SecioConfig {
|
||||
//! // See the documentation of `SecioKeyPair`.
|
||||
//! key: SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
||||
//! };
|
||||
//!
|
||||
//! upgrade::map(upgrade, |out: SecioOutput<_>| out.stream)
|
||||
//! });
|
||||
//!
|
||||
//! let future = transport.dial("/ip4/127.0.0.1/tcp/12345".parse::<Multiaddr>().unwrap())
|
||||
//! .unwrap_or_else(|_| panic!("Unable to dial node"))
|
||||
//! .and_then(|(connection, _)| {
|
||||
//! // Sends "hello world" on the connection, will be encrypted.
|
||||
//! write_all(connection, "hello world")
|
||||
//! });
|
||||
//!
|
||||
//! tokio_current_thread::block_on_all(future).unwrap();
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! # Manual usage
|
||||
//!
|
||||
//! > **Note**: You are encouraged to use `SecioConfig` as described above.
|
||||
//!
|
||||
//! You can add the `secio` layer over a socket by calling `SecioMiddleware::handshake()`. This
|
||||
//! method will perform a handshake with the host, and return a future that corresponds to the
|
||||
//! moment when the handshake succeeds or errored. On success, the future produces a
|
||||
//! `SecioMiddleware` that implements `Sink` and `Stream` and can be used to send packets of data.
|
||||
//!
|
||||
|
||||
extern crate aes_ctr;
|
||||
#[cfg(feature = "secp256k1")]
|
||||
extern crate asn1_der;
|
||||
extern crate bytes;
|
||||
extern crate futures;
|
||||
extern crate libp2p_core;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate protobuf;
|
||||
extern crate rand;
|
||||
extern crate ring;
|
||||
extern crate rw_stream_sink;
|
||||
#[cfg(feature = "secp256k1")]
|
||||
extern crate secp256k1;
|
||||
extern crate tokio_io;
|
||||
extern crate untrusted;
|
||||
|
||||
#[cfg(feature = "aes-all")]
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
pub use self::error::SecioError;
|
||||
|
||||
#[cfg(feature = "secp256k1")]
|
||||
use asn1_der::{traits::FromDerEncoded, traits::FromDerObject, DerObject};
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use futures::stream::MapErr as StreamMapErr;
|
||||
use futures::{Future, Poll, Sink, StartSend, Stream};
|
||||
use libp2p_core::{PeerId, PublicKey};
|
||||
use ring::rand::SystemRandom;
|
||||
use ring::signature::{Ed25519KeyPair, RSAKeyPair};
|
||||
use rw_stream_sink::RwStreamSink;
|
||||
use std::error::Error;
|
||||
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
||||
use std::iter;
|
||||
use std::sync::Arc;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use untrusted::Input;
|
||||
|
||||
mod algo_support;
|
||||
mod codec;
|
||||
mod error;
|
||||
mod handshake;
|
||||
mod structs_proto;
|
||||
mod stream_cipher;
|
||||
|
||||
/// Implementation of the `ConnectionUpgrade` trait of `libp2p_core`. Automatically applies
|
||||
/// secio on any connection.
|
||||
#[derive(Clone)]
|
||||
pub struct SecioConfig {
|
||||
/// Private and public keys of the local node.
|
||||
pub key: SecioKeyPair,
|
||||
}
|
||||
|
||||
/// Private and public keys of the local node.
|
||||
///
|
||||
/// # Generating offline keys with OpenSSL
|
||||
///
|
||||
/// ## RSA
|
||||
///
|
||||
/// Generating the keys:
|
||||
///
|
||||
/// ```ignore
|
||||
/// openssl genrsa -out private.pem 2048
|
||||
/// openssl rsa -in private.pem -outform DER -pubout -out public.der
|
||||
/// openssl pkcs8 -in private.pem -topk8 -nocrypt -out private.pk8
|
||||
/// rm private.pem # optional
|
||||
/// ```
|
||||
///
|
||||
/// Loading the keys:
|
||||
///
|
||||
/// ```ignore
|
||||
/// let key_pair = SecioKeyPair::rsa_from_pkcs8(include_bytes!("private.pk8"),
|
||||
/// include_bytes!("public.der"));
|
||||
/// ```
|
||||
///
|
||||
#[derive(Clone)]
|
||||
pub struct SecioKeyPair {
|
||||
inner: SecioKeyPairInner,
|
||||
}
|
||||
|
||||
impl SecioKeyPair {
|
||||
/// Builds a `SecioKeyPair` from a PKCS8 private key and public key.
|
||||
pub fn rsa_from_pkcs8<P>(
|
||||
private: &[u8],
|
||||
public: P,
|
||||
) -> Result<SecioKeyPair, Box<Error + Send + Sync>>
|
||||
where
|
||||
P: Into<Vec<u8>>,
|
||||
{
|
||||
let private = RSAKeyPair::from_pkcs8(Input::from(&private[..])).map_err(Box::new)?;
|
||||
|
||||
Ok(SecioKeyPair {
|
||||
inner: SecioKeyPairInner::Rsa {
|
||||
public: public.into(),
|
||||
private: Arc::new(private),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds a `SecioKeyPair` from a PKCS8 ED25519 private key.
|
||||
pub fn ed25519_from_pkcs8<K>(key: K) -> Result<SecioKeyPair, Box<Error + Send + Sync>>
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
{
|
||||
let key_pair = Ed25519KeyPair::from_pkcs8(Input::from(key.as_ref())).map_err(Box::new)?;
|
||||
|
||||
Ok(SecioKeyPair {
|
||||
inner: SecioKeyPairInner::Ed25519 {
|
||||
key_pair: Arc::new(key_pair),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/// Generates a new Ed25519 key pair and uses it.
|
||||
pub fn ed25519_generated() -> Result<SecioKeyPair, Box<Error + Send + Sync>> {
|
||||
let rng = SystemRandom::new();
|
||||
let gen = Ed25519KeyPair::generate_pkcs8(&rng).map_err(Box::new)?;
|
||||
Ok(SecioKeyPair::ed25519_from_pkcs8(&gen[..])
|
||||
.expect("failed to parse generated Ed25519 key"))
|
||||
}
|
||||
|
||||
/// Builds a `SecioKeyPair` from a raw secp256k1 32 bytes private key.
|
||||
#[cfg(feature = "secp256k1")]
|
||||
pub fn secp256k1_raw_key<K>(key: K) -> Result<SecioKeyPair, Box<Error + Send + Sync>>
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
{
|
||||
let secp = secp256k1::Secp256k1::with_caps(secp256k1::ContextFlag::None);
|
||||
let private = secp256k1::key::SecretKey::from_slice(&secp, key.as_ref())?;
|
||||
|
||||
Ok(SecioKeyPair {
|
||||
inner: SecioKeyPairInner::Secp256k1 { private },
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds a `SecioKeyPair` from a secp256k1 private key in DER format.
|
||||
#[cfg(feature = "secp256k1")]
|
||||
pub fn secp256k1_from_der<K>(key: K) -> Result<SecioKeyPair, Box<Error + Send + Sync>>
|
||||
where
|
||||
K: AsRef<[u8]>,
|
||||
{
|
||||
// See ECPrivateKey in https://tools.ietf.org/html/rfc5915
|
||||
let obj: Vec<DerObject> =
|
||||
FromDerEncoded::with_der_encoded(key.as_ref()).map_err(|err| err.to_string())?;
|
||||
let priv_key_obj = obj.into_iter()
|
||||
.nth(1)
|
||||
.ok_or_else(|| "Not enough elements in DER".to_string())?;
|
||||
let private_key: Vec<u8> =
|
||||
FromDerObject::from_der_object(priv_key_obj).map_err(|err| err.to_string())?;
|
||||
SecioKeyPair::secp256k1_raw_key(&private_key)
|
||||
}
|
||||
|
||||
/// Returns the public key corresponding to this key pair.
|
||||
pub fn to_public_key(&self) -> PublicKey {
|
||||
match self.inner {
|
||||
SecioKeyPairInner::Rsa { ref public, .. } => PublicKey::Rsa(public.clone()),
|
||||
SecioKeyPairInner::Ed25519 { ref key_pair } => {
|
||||
PublicKey::Ed25519(key_pair.public_key_bytes().to_vec())
|
||||
}
|
||||
#[cfg(feature = "secp256k1")]
|
||||
SecioKeyPairInner::Secp256k1 { ref private } => {
|
||||
let secp = secp256k1::Secp256k1::with_caps(secp256k1::ContextFlag::SignOnly);
|
||||
let pubkey = secp256k1::key::PublicKey::from_secret_key(&secp, private)
|
||||
.expect("wrong secp256k1 private key ; type safety violated");
|
||||
PublicKey::Secp256k1(pubkey.serialize_vec(&secp, true).to_vec())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds a `PeerId` corresponding to the public key of this key pair.
|
||||
#[inline]
|
||||
pub fn to_peer_id(&self) -> PeerId {
|
||||
self.to_public_key().into_peer_id()
|
||||
}
|
||||
|
||||
// TODO: method to save generated key on disk?
|
||||
}
|
||||
|
||||
// Inner content of `SecioKeyPair`.
|
||||
#[derive(Clone)]
|
||||
enum SecioKeyPairInner {
|
||||
Rsa {
|
||||
public: Vec<u8>,
|
||||
// We use an `Arc` so that we can clone the enum.
|
||||
private: Arc<RSAKeyPair>,
|
||||
},
|
||||
Ed25519 {
|
||||
// We use an `Arc` so that we can clone the enum.
|
||||
key_pair: Arc<Ed25519KeyPair>,
|
||||
},
|
||||
#[cfg(feature = "secp256k1")]
|
||||
Secp256k1 { private: secp256k1::key::SecretKey },
|
||||
}
|
||||
|
||||
/// Output of the secio protocol.
|
||||
pub struct SecioOutput<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
/// The encrypted stream.
|
||||
pub stream: RwStreamSink<StreamMapErr<SecioMiddleware<S>, fn(SecioError) -> IoError>>,
|
||||
/// The public key of the remote.
|
||||
pub remote_key: PublicKey,
|
||||
/// Ephemeral public key used during the negotiation.
|
||||
pub ephemeral_public_key: Vec<u8>,
|
||||
}
|
||||
|
||||
impl<S, Maf> libp2p_core::ConnectionUpgrade<S, Maf> for SecioConfig
|
||||
where
|
||||
S: AsyncRead + AsyncWrite + 'static, // TODO: 'static :(
|
||||
Maf: 'static, // TODO: 'static :(
|
||||
{
|
||||
type Output = SecioOutput<S>;
|
||||
type MultiaddrFuture = Maf;
|
||||
type Future = Box<Future<Item = (Self::Output, Maf), Error = IoError>>;
|
||||
type NamesIter = iter::Once<(Bytes, ())>;
|
||||
type UpgradeIdentifier = ();
|
||||
|
||||
#[inline]
|
||||
fn protocol_names(&self) -> Self::NamesIter {
|
||||
iter::once(("/secio/1.0.0".into(), ()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn upgrade(
|
||||
self,
|
||||
incoming: S,
|
||||
_: (),
|
||||
_: libp2p_core::Endpoint,
|
||||
remote_addr: Maf,
|
||||
) -> Self::Future {
|
||||
debug!("Starting secio upgrade");
|
||||
|
||||
let fut = SecioMiddleware::handshake(incoming, self.key);
|
||||
let wrapped = fut.map(|(stream_sink, pubkey, ephemeral)| {
|
||||
let mapped = stream_sink.map_err(map_err as fn(_) -> _);
|
||||
SecioOutput {
|
||||
stream: RwStreamSink::new(mapped),
|
||||
remote_key: pubkey,
|
||||
ephemeral_public_key: ephemeral,
|
||||
}
|
||||
}).map_err(map_err);
|
||||
Box::new(wrapped.map(move |out| (out, remote_addr)))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn map_err(err: SecioError) -> IoError {
|
||||
debug!("error during secio handshake {:?}", err);
|
||||
IoError::new(IoErrorKind::InvalidData, err)
|
||||
}
|
||||
|
||||
/// Wraps around an object that implements `AsyncRead` and `AsyncWrite`.
|
||||
///
|
||||
/// Implements `Sink` and `Stream` whose items are frames of data. Each frame is encoded
|
||||
/// individually, so you are encouraged to group data in few frames if possible.
|
||||
pub struct SecioMiddleware<S> {
|
||||
inner: codec::FullCodec<S>,
|
||||
}
|
||||
|
||||
impl<S> SecioMiddleware<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
/// Attempts to perform a handshake on the given socket.
|
||||
///
|
||||
/// On success, produces a `SecioMiddleware` that can then be used to encode/decode
|
||||
/// communications, plus the public key of the remote, plus the ephemeral public key.
|
||||
pub fn handshake<'a>(
|
||||
socket: S,
|
||||
key_pair: SecioKeyPair,
|
||||
) -> Box<Future<Item = (SecioMiddleware<S>, PublicKey, Vec<u8>), Error = SecioError> + 'a>
|
||||
where
|
||||
S: 'a,
|
||||
{
|
||||
let fut = handshake::handshake(socket, key_pair).map(|(inner, pubkey, ephemeral)| {
|
||||
let inner = SecioMiddleware { inner };
|
||||
(inner, pubkey, ephemeral)
|
||||
});
|
||||
|
||||
Box::new(fut)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Sink for SecioMiddleware<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type SinkItem = BytesMut;
|
||||
type SinkError = IoError;
|
||||
|
||||
#[inline]
|
||||
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||
self.inner.start_send(item)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
||||
self.inner.poll_complete()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> Stream for SecioMiddleware<S>
|
||||
where
|
||||
S: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Item = Vec<u8>;
|
||||
type Error = SecioError;
|
||||
|
||||
#[inline]
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
self.inner.poll()
|
||||
}
|
||||
}
|
135
protocols/secio/src/stream_cipher.rs
Normal file
135
protocols/secio/src/stream_cipher.rs
Normal file
@ -0,0 +1,135 @@
|
||||
// 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.
|
||||
|
||||
use super::codec::StreamCipher;
|
||||
use aes_ctr::stream_cipher::generic_array::GenericArray;
|
||||
use aes_ctr::stream_cipher::NewFixStreamCipher;
|
||||
use aes_ctr::{Aes128Ctr, Aes256Ctr};
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum KeySize {
|
||||
KeySize128,
|
||||
KeySize256,
|
||||
}
|
||||
|
||||
/// Returns your stream cipher depending on `KeySize`.
|
||||
#[cfg(not(all(feature = "aes-all", any(target_arch = "x86_64", target_arch = "x86"))))]
|
||||
pub fn ctr(key_size: KeySize, key: &[u8], iv: &[u8]) -> StreamCipher {
|
||||
ctr_int(key_size, key, iv)
|
||||
}
|
||||
|
||||
/// Returns your stream cipher depending on `KeySize`.
|
||||
#[cfg(all(feature = "aes-all", any(target_arch = "x86_64", target_arch = "x86")))]
|
||||
pub fn ctr(key_size: KeySize, key: &[u8], iv: &[u8]) -> StreamCipher {
|
||||
if *aes_alt::AES_NI {
|
||||
aes_alt::ctr_alt(key_size, key, iv)
|
||||
} else {
|
||||
ctr_int(key_size, key, iv)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(all(feature = "aes-all", any(target_arch = "x86_64", target_arch = "x86")))]
|
||||
mod aes_alt {
|
||||
extern crate ctr;
|
||||
extern crate aesni;
|
||||
use ::codec::StreamCipher;
|
||||
use self::ctr::Ctr128;
|
||||
use self::aesni::{Aes128, Aes256};
|
||||
use self::ctr::stream_cipher::NewFixStreamCipher;
|
||||
use self::ctr::stream_cipher::generic_array::GenericArray;
|
||||
use super::KeySize;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref AES_NI: bool = is_x86_feature_detected!("aes")
|
||||
&& is_x86_feature_detected!("sse2")
|
||||
&& is_x86_feature_detected!("sse3");
|
||||
|
||||
}
|
||||
|
||||
/// AES-128 in CTR mode
|
||||
pub type Aes128Ctr = Ctr128<Aes128>;
|
||||
/// AES-256 in CTR mode
|
||||
pub type Aes256Ctr = Ctr128<Aes256>;
|
||||
/// Returns alternate stream cipher if target functionalities does not allow standard one.
|
||||
/// Eg : aes without sse
|
||||
pub fn ctr_alt(key_size: KeySize, key: &[u8], iv: &[u8]) -> StreamCipher {
|
||||
match key_size {
|
||||
KeySize::KeySize128 => Box::new(Aes128Ctr::new(
|
||||
GenericArray::from_slice(key),
|
||||
GenericArray::from_slice(iv),
|
||||
)),
|
||||
KeySize::KeySize256 => Box::new(Aes256Ctr::new(
|
||||
GenericArray::from_slice(key),
|
||||
GenericArray::from_slice(iv),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn ctr_int(key_size: KeySize, key: &[u8], iv: &[u8]) -> StreamCipher {
|
||||
match key_size {
|
||||
KeySize::KeySize128 => Box::new(Aes128Ctr::new(
|
||||
GenericArray::from_slice(key),
|
||||
GenericArray::from_slice(iv),
|
||||
)),
|
||||
KeySize::KeySize256 => Box::new(Aes256Ctr::new(
|
||||
GenericArray::from_slice(key),
|
||||
GenericArray::from_slice(iv),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(
|
||||
feature = "aes-all",
|
||||
any(target_arch = "x86_64", target_arch = "x86"),
|
||||
))]
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{KeySize, ctr};
|
||||
|
||||
#[test]
|
||||
fn assert_non_native_run() {
|
||||
// this test is for asserting aes unsuported opcode does not break on old cpu
|
||||
let key = [0;16];
|
||||
let iv = [0;16];
|
||||
|
||||
let mut aes = ctr(KeySize::KeySize128, &key, &iv);
|
||||
let mut content = [0;16];
|
||||
assert!(aes
|
||||
.try_apply_keystream(&mut content).is_ok());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// aesni compile check for aes-all (aes-all import aesni through aes_ctr only if those checks pass)
|
||||
#[cfg(all(
|
||||
feature = "aes-all",
|
||||
any(target_arch = "x86_64", target_arch = "x86"),
|
||||
any(target_feature = "aes", target_feature = "ssse3"),
|
||||
))]
|
||||
compile_error!(
|
||||
"aes-all must be compile without aes and sse3 flags : currently \
|
||||
is_x86_feature_detected macro will not detect feature correctly otherwhise. \
|
||||
RUSTFLAGS=\"-C target-feature=+aes,+ssse3\" enviromental variable. \
|
||||
For x86 target arch additionally enable sse2 target feature."
|
||||
);
|
681
protocols/secio/src/structs_proto.rs
Normal file
681
protocols/secio/src/structs_proto.rs
Normal file
@ -0,0 +1,681 @@
|
||||
// This file is generated by rust-protobuf 2.0.2. Do not edit
|
||||
// @generated
|
||||
|
||||
// https://github.com/Manishearth/rust-clippy/issues/702
|
||||
#![allow(unknown_lints)]
|
||||
#![allow(clippy)]
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
|
||||
#![allow(box_pointers)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(non_upper_case_globals)]
|
||||
#![allow(trivial_casts)]
|
||||
#![allow(unsafe_code)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(unused_results)]
|
||||
|
||||
use protobuf::Message as Message_imported_for_functions;
|
||||
use protobuf::ProtobufEnum as ProtobufEnum_imported_for_functions;
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Propose {
|
||||
// message fields
|
||||
rand: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
pubkey: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
exchanges: ::protobuf::SingularField<::std::string::String>,
|
||||
ciphers: ::protobuf::SingularField<::std::string::String>,
|
||||
hashes: ::protobuf::SingularField<::std::string::String>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Propose {
|
||||
pub fn new() -> Propose {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional bytes rand = 1;
|
||||
|
||||
pub fn clear_rand(&mut self) {
|
||||
self.rand.clear();
|
||||
}
|
||||
|
||||
pub fn has_rand(&self) -> bool {
|
||||
self.rand.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_rand(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.rand = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_rand(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.rand.is_none() {
|
||||
self.rand.set_default();
|
||||
}
|
||||
self.rand.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_rand(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.rand.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_rand(&self) -> &[u8] {
|
||||
match self.rand.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional bytes pubkey = 2;
|
||||
|
||||
pub fn clear_pubkey(&mut self) {
|
||||
self.pubkey.clear();
|
||||
}
|
||||
|
||||
pub fn has_pubkey(&self) -> bool {
|
||||
self.pubkey.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_pubkey(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.pubkey = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_pubkey(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.pubkey.is_none() {
|
||||
self.pubkey.set_default();
|
||||
}
|
||||
self.pubkey.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_pubkey(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.pubkey.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_pubkey(&self) -> &[u8] {
|
||||
match self.pubkey.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional string exchanges = 3;
|
||||
|
||||
pub fn clear_exchanges(&mut self) {
|
||||
self.exchanges.clear();
|
||||
}
|
||||
|
||||
pub fn has_exchanges(&self) -> bool {
|
||||
self.exchanges.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_exchanges(&mut self, v: ::std::string::String) {
|
||||
self.exchanges = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_exchanges(&mut self) -> &mut ::std::string::String {
|
||||
if self.exchanges.is_none() {
|
||||
self.exchanges.set_default();
|
||||
}
|
||||
self.exchanges.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_exchanges(&mut self) -> ::std::string::String {
|
||||
self.exchanges.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_exchanges(&self) -> &str {
|
||||
match self.exchanges.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional string ciphers = 4;
|
||||
|
||||
pub fn clear_ciphers(&mut self) {
|
||||
self.ciphers.clear();
|
||||
}
|
||||
|
||||
pub fn has_ciphers(&self) -> bool {
|
||||
self.ciphers.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_ciphers(&mut self, v: ::std::string::String) {
|
||||
self.ciphers = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_ciphers(&mut self) -> &mut ::std::string::String {
|
||||
if self.ciphers.is_none() {
|
||||
self.ciphers.set_default();
|
||||
}
|
||||
self.ciphers.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_ciphers(&mut self) -> ::std::string::String {
|
||||
self.ciphers.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_ciphers(&self) -> &str {
|
||||
match self.ciphers.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
// optional string hashes = 5;
|
||||
|
||||
pub fn clear_hashes(&mut self) {
|
||||
self.hashes.clear();
|
||||
}
|
||||
|
||||
pub fn has_hashes(&self) -> bool {
|
||||
self.hashes.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_hashes(&mut self, v: ::std::string::String) {
|
||||
self.hashes = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_hashes(&mut self) -> &mut ::std::string::String {
|
||||
if self.hashes.is_none() {
|
||||
self.hashes.set_default();
|
||||
}
|
||||
self.hashes.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_hashes(&mut self) -> ::std::string::String {
|
||||
self.hashes.take().unwrap_or_else(|| ::std::string::String::new())
|
||||
}
|
||||
|
||||
pub fn get_hashes(&self) -> &str {
|
||||
match self.hashes.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Propose {
|
||||
fn is_initialized(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.rand)?;
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.pubkey)?;
|
||||
},
|
||||
3 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.exchanges)?;
|
||||
},
|
||||
4 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.ciphers)?;
|
||||
},
|
||||
5 => {
|
||||
::protobuf::rt::read_singular_string_into(wire_type, is, &mut self.hashes)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(ref v) = self.rand.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(1, &v);
|
||||
}
|
||||
if let Some(ref v) = self.pubkey.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
if let Some(ref v) = self.exchanges.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(3, &v);
|
||||
}
|
||||
if let Some(ref v) = self.ciphers.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(4, &v);
|
||||
}
|
||||
if let Some(ref v) = self.hashes.as_ref() {
|
||||
my_size += ::protobuf::rt::string_size(5, &v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(ref v) = self.rand.as_ref() {
|
||||
os.write_bytes(1, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.pubkey.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.exchanges.as_ref() {
|
||||
os.write_string(3, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.ciphers.as_ref() {
|
||||
os.write_string(4, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.hashes.as_ref() {
|
||||
os.write_string(5, &v)?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Propose {
|
||||
Propose::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"rand",
|
||||
|m: &Propose| { &m.rand },
|
||||
|m: &mut Propose| { &mut m.rand },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"pubkey",
|
||||
|m: &Propose| { &m.pubkey },
|
||||
|m: &mut Propose| { &mut m.pubkey },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"exchanges",
|
||||
|m: &Propose| { &m.exchanges },
|
||||
|m: &mut Propose| { &mut m.exchanges },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"ciphers",
|
||||
|m: &Propose| { &m.ciphers },
|
||||
|m: &mut Propose| { &mut m.ciphers },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeString>(
|
||||
"hashes",
|
||||
|m: &Propose| { &m.hashes },
|
||||
|m: &mut Propose| { &mut m.hashes },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Propose>(
|
||||
"Propose",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Propose {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Propose> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Propose,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Propose::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Propose {
|
||||
fn clear(&mut self) {
|
||||
self.clear_rand();
|
||||
self.clear_pubkey();
|
||||
self.clear_exchanges();
|
||||
self.clear_ciphers();
|
||||
self.clear_hashes();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Propose {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Propose {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq,Clone,Default)]
|
||||
pub struct Exchange {
|
||||
// message fields
|
||||
epubkey: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
signature: ::protobuf::SingularField<::std::vec::Vec<u8>>,
|
||||
// special fields
|
||||
unknown_fields: ::protobuf::UnknownFields,
|
||||
cached_size: ::protobuf::CachedSize,
|
||||
}
|
||||
|
||||
impl Exchange {
|
||||
pub fn new() -> Exchange {
|
||||
::std::default::Default::default()
|
||||
}
|
||||
|
||||
// optional bytes epubkey = 1;
|
||||
|
||||
pub fn clear_epubkey(&mut self) {
|
||||
self.epubkey.clear();
|
||||
}
|
||||
|
||||
pub fn has_epubkey(&self) -> bool {
|
||||
self.epubkey.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_epubkey(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.epubkey = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_epubkey(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.epubkey.is_none() {
|
||||
self.epubkey.set_default();
|
||||
}
|
||||
self.epubkey.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_epubkey(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.epubkey.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_epubkey(&self) -> &[u8] {
|
||||
match self.epubkey.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
|
||||
// optional bytes signature = 2;
|
||||
|
||||
pub fn clear_signature(&mut self) {
|
||||
self.signature.clear();
|
||||
}
|
||||
|
||||
pub fn has_signature(&self) -> bool {
|
||||
self.signature.is_some()
|
||||
}
|
||||
|
||||
// Param is passed by value, moved
|
||||
pub fn set_signature(&mut self, v: ::std::vec::Vec<u8>) {
|
||||
self.signature = ::protobuf::SingularField::some(v);
|
||||
}
|
||||
|
||||
// Mutable pointer to the field.
|
||||
// If field is not initialized, it is initialized with default value first.
|
||||
pub fn mut_signature(&mut self) -> &mut ::std::vec::Vec<u8> {
|
||||
if self.signature.is_none() {
|
||||
self.signature.set_default();
|
||||
}
|
||||
self.signature.as_mut().unwrap()
|
||||
}
|
||||
|
||||
// Take field
|
||||
pub fn take_signature(&mut self) -> ::std::vec::Vec<u8> {
|
||||
self.signature.take().unwrap_or_else(|| ::std::vec::Vec::new())
|
||||
}
|
||||
|
||||
pub fn get_signature(&self) -> &[u8] {
|
||||
match self.signature.as_ref() {
|
||||
Some(v) => &v,
|
||||
None => &[],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Message for Exchange {
|
||||
fn is_initialized(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn merge_from(&mut self, is: &mut ::protobuf::CodedInputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
while !is.eof()? {
|
||||
let (field_number, wire_type) = is.read_tag_unpack()?;
|
||||
match field_number {
|
||||
1 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.epubkey)?;
|
||||
},
|
||||
2 => {
|
||||
::protobuf::rt::read_singular_bytes_into(wire_type, is, &mut self.signature)?;
|
||||
},
|
||||
_ => {
|
||||
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
|
||||
},
|
||||
};
|
||||
}
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
// Compute sizes of nested messages
|
||||
#[allow(unused_variables)]
|
||||
fn compute_size(&self) -> u32 {
|
||||
let mut my_size = 0;
|
||||
if let Some(ref v) = self.epubkey.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(1, &v);
|
||||
}
|
||||
if let Some(ref v) = self.signature.as_ref() {
|
||||
my_size += ::protobuf::rt::bytes_size(2, &v);
|
||||
}
|
||||
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
|
||||
self.cached_size.set(my_size);
|
||||
my_size
|
||||
}
|
||||
|
||||
fn write_to_with_cached_sizes(&self, os: &mut ::protobuf::CodedOutputStream) -> ::protobuf::ProtobufResult<()> {
|
||||
if let Some(ref v) = self.epubkey.as_ref() {
|
||||
os.write_bytes(1, &v)?;
|
||||
}
|
||||
if let Some(ref v) = self.signature.as_ref() {
|
||||
os.write_bytes(2, &v)?;
|
||||
}
|
||||
os.write_unknown_fields(self.get_unknown_fields())?;
|
||||
::std::result::Result::Ok(())
|
||||
}
|
||||
|
||||
fn get_cached_size(&self) -> u32 {
|
||||
self.cached_size.get()
|
||||
}
|
||||
|
||||
fn get_unknown_fields(&self) -> &::protobuf::UnknownFields {
|
||||
&self.unknown_fields
|
||||
}
|
||||
|
||||
fn mut_unknown_fields(&mut self) -> &mut ::protobuf::UnknownFields {
|
||||
&mut self.unknown_fields
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &::std::any::Any {
|
||||
self as &::std::any::Any
|
||||
}
|
||||
fn as_any_mut(&mut self) -> &mut ::std::any::Any {
|
||||
self as &mut ::std::any::Any
|
||||
}
|
||||
fn into_any(self: Box<Self>) -> ::std::boxed::Box<::std::any::Any> {
|
||||
self
|
||||
}
|
||||
|
||||
fn descriptor(&self) -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
Self::descriptor_static()
|
||||
}
|
||||
|
||||
fn new() -> Exchange {
|
||||
Exchange::new()
|
||||
}
|
||||
|
||||
fn descriptor_static() -> &'static ::protobuf::reflect::MessageDescriptor {
|
||||
static mut descriptor: ::protobuf::lazy::Lazy<::protobuf::reflect::MessageDescriptor> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::reflect::MessageDescriptor,
|
||||
};
|
||||
unsafe {
|
||||
descriptor.get(|| {
|
||||
let mut fields = ::std::vec::Vec::new();
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"epubkey",
|
||||
|m: &Exchange| { &m.epubkey },
|
||||
|m: &mut Exchange| { &mut m.epubkey },
|
||||
));
|
||||
fields.push(::protobuf::reflect::accessor::make_singular_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
|
||||
"signature",
|
||||
|m: &Exchange| { &m.signature },
|
||||
|m: &mut Exchange| { &mut m.signature },
|
||||
));
|
||||
::protobuf::reflect::MessageDescriptor::new::<Exchange>(
|
||||
"Exchange",
|
||||
fields,
|
||||
file_descriptor_proto()
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn default_instance() -> &'static Exchange {
|
||||
static mut instance: ::protobuf::lazy::Lazy<Exchange> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const Exchange,
|
||||
};
|
||||
unsafe {
|
||||
instance.get(Exchange::new)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::Clear for Exchange {
|
||||
fn clear(&mut self) {
|
||||
self.clear_epubkey();
|
||||
self.clear_signature();
|
||||
self.unknown_fields.clear();
|
||||
}
|
||||
}
|
||||
|
||||
impl ::std::fmt::Debug for Exchange {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
::protobuf::text_format::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl ::protobuf::reflect::ProtobufValue for Exchange {
|
||||
fn as_ref(&self) -> ::protobuf::reflect::ProtobufValueRef {
|
||||
::protobuf::reflect::ProtobufValueRef::Message(self)
|
||||
}
|
||||
}
|
||||
|
||||
static file_descriptor_proto_data: &'static [u8] = b"\
|
||||
\n\rstructs.proto\x12\x08spipe.pb\"\x85\x01\n\x07Propose\x12\x12\n\x04ra\
|
||||
nd\x18\x01\x20\x01(\x0cR\x04rand\x12\x16\n\x06pubkey\x18\x02\x20\x01(\
|
||||
\x0cR\x06pubkey\x12\x1c\n\texchanges\x18\x03\x20\x01(\tR\texchanges\x12\
|
||||
\x18\n\x07ciphers\x18\x04\x20\x01(\tR\x07ciphers\x12\x16\n\x06hashes\x18\
|
||||
\x05\x20\x01(\tR\x06hashes\"B\n\x08Exchange\x12\x18\n\x07epubkey\x18\x01\
|
||||
\x20\x01(\x0cR\x07epubkey\x12\x1c\n\tsignature\x18\x02\x20\x01(\x0cR\tsi\
|
||||
gnatureJ\xa5\x04\n\x06\x12\x04\0\0\r\x01\n\x08\n\x01\x02\x12\x03\0\x08\
|
||||
\x10\n\n\n\x02\x04\0\x12\x04\x02\0\x08\x01\n\n\n\x03\x04\0\x01\x12\x03\
|
||||
\x02\x08\x0f\n\x0b\n\x04\x04\0\x02\0\x12\x03\x03\x08\x20\n\x0c\n\x05\x04\
|
||||
\0\x02\0\x04\x12\x03\x03\x08\x10\n\x0c\n\x05\x04\0\x02\0\x05\x12\x03\x03\
|
||||
\x11\x16\n\x0c\n\x05\x04\0\x02\0\x01\x12\x03\x03\x17\x1b\n\x0c\n\x05\x04\
|
||||
\0\x02\0\x03\x12\x03\x03\x1e\x1f\n\x0b\n\x04\x04\0\x02\x01\x12\x03\x04\
|
||||
\x08\"\n\x0c\n\x05\x04\0\x02\x01\x04\x12\x03\x04\x08\x10\n\x0c\n\x05\x04\
|
||||
\0\x02\x01\x05\x12\x03\x04\x11\x16\n\x0c\n\x05\x04\0\x02\x01\x01\x12\x03\
|
||||
\x04\x17\x1d\n\x0c\n\x05\x04\0\x02\x01\x03\x12\x03\x04\x20!\n\x0b\n\x04\
|
||||
\x04\0\x02\x02\x12\x03\x05\x08&\n\x0c\n\x05\x04\0\x02\x02\x04\x12\x03\
|
||||
\x05\x08\x10\n\x0c\n\x05\x04\0\x02\x02\x05\x12\x03\x05\x11\x17\n\x0c\n\
|
||||
\x05\x04\0\x02\x02\x01\x12\x03\x05\x18!\n\x0c\n\x05\x04\0\x02\x02\x03\
|
||||
\x12\x03\x05$%\n\x0b\n\x04\x04\0\x02\x03\x12\x03\x06\x08$\n\x0c\n\x05\
|
||||
\x04\0\x02\x03\x04\x12\x03\x06\x08\x10\n\x0c\n\x05\x04\0\x02\x03\x05\x12\
|
||||
\x03\x06\x11\x17\n\x0c\n\x05\x04\0\x02\x03\x01\x12\x03\x06\x18\x1f\n\x0c\
|
||||
\n\x05\x04\0\x02\x03\x03\x12\x03\x06\"#\n\x0b\n\x04\x04\0\x02\x04\x12\
|
||||
\x03\x07\x08#\n\x0c\n\x05\x04\0\x02\x04\x04\x12\x03\x07\x08\x10\n\x0c\n\
|
||||
\x05\x04\0\x02\x04\x05\x12\x03\x07\x11\x17\n\x0c\n\x05\x04\0\x02\x04\x01\
|
||||
\x12\x03\x07\x18\x1e\n\x0c\n\x05\x04\0\x02\x04\x03\x12\x03\x07!\"\n\n\n\
|
||||
\x02\x04\x01\x12\x04\n\0\r\x01\n\n\n\x03\x04\x01\x01\x12\x03\n\x08\x10\n\
|
||||
\x0b\n\x04\x04\x01\x02\0\x12\x03\x0b\x08#\n\x0c\n\x05\x04\x01\x02\0\x04\
|
||||
\x12\x03\x0b\x08\x10\n\x0c\n\x05\x04\x01\x02\0\x05\x12\x03\x0b\x11\x16\n\
|
||||
\x0c\n\x05\x04\x01\x02\0\x01\x12\x03\x0b\x17\x1e\n\x0c\n\x05\x04\x01\x02\
|
||||
\0\x03\x12\x03\x0b!\"\n\x0b\n\x04\x04\x01\x02\x01\x12\x03\x0c\x08%\n\x0c\
|
||||
\n\x05\x04\x01\x02\x01\x04\x12\x03\x0c\x08\x10\n\x0c\n\x05\x04\x01\x02\
|
||||
\x01\x05\x12\x03\x0c\x11\x16\n\x0c\n\x05\x04\x01\x02\x01\x01\x12\x03\x0c\
|
||||
\x17\x20\n\x0c\n\x05\x04\x01\x02\x01\x03\x12\x03\x0c#$\
|
||||
";
|
||||
|
||||
static mut file_descriptor_proto_lazy: ::protobuf::lazy::Lazy<::protobuf::descriptor::FileDescriptorProto> = ::protobuf::lazy::Lazy {
|
||||
lock: ::protobuf::lazy::ONCE_INIT,
|
||||
ptr: 0 as *const ::protobuf::descriptor::FileDescriptorProto,
|
||||
};
|
||||
|
||||
fn parse_descriptor_proto() -> ::protobuf::descriptor::FileDescriptorProto {
|
||||
::protobuf::parse_from_bytes(file_descriptor_proto_data).unwrap()
|
||||
}
|
||||
|
||||
pub fn file_descriptor_proto() -> &'static ::protobuf::descriptor::FileDescriptorProto {
|
||||
unsafe {
|
||||
file_descriptor_proto_lazy.get(|| {
|
||||
parse_descriptor_proto()
|
||||
})
|
||||
}
|
||||
}
|
14
protocols/secio/structs.proto
Normal file
14
protocols/secio/structs.proto
Normal file
@ -0,0 +1,14 @@
|
||||
package spipe.pb;
|
||||
|
||||
message Propose {
|
||||
optional bytes rand = 1;
|
||||
optional bytes pubkey = 2;
|
||||
optional string exchanges = 3;
|
||||
optional string ciphers = 4;
|
||||
optional string hashes = 5;
|
||||
}
|
||||
|
||||
message Exchange {
|
||||
optional bytes epubkey = 1;
|
||||
optional bytes signature = 2;
|
||||
}
|
BIN
protocols/secio/tests/test-rsa-private-key-2.pk8
Normal file
BIN
protocols/secio/tests/test-rsa-private-key-2.pk8
Normal file
Binary file not shown.
BIN
protocols/secio/tests/test-rsa-private-key.pk8
Normal file
BIN
protocols/secio/tests/test-rsa-private-key.pk8
Normal file
Binary file not shown.
BIN
protocols/secio/tests/test-rsa-public-key-2.der
Normal file
BIN
protocols/secio/tests/test-rsa-public-key-2.der
Normal file
Binary file not shown.
BIN
protocols/secio/tests/test-rsa-public-key.der
Normal file
BIN
protocols/secio/tests/test-rsa-public-key.der
Normal file
Binary file not shown.
BIN
protocols/secio/tests/test-secp256k1-private-key-2.der
Normal file
BIN
protocols/secio/tests/test-secp256k1-private-key-2.der
Normal file
Binary file not shown.
BIN
protocols/secio/tests/test-secp256k1-private-key.der
Normal file
BIN
protocols/secio/tests/test-secp256k1-private-key.der
Normal file
Binary file not shown.
Reference in New Issue
Block a user