From ba80cbfc997a91b3a1c9a4e903530d5def3910b6 Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 27 Mar 2020 22:32:52 +0300 Subject: [PATCH] Tunnel methods --- src/crypto.rs | 4 +--- src/ironforce.rs | 5 +++-- src/message.rs | 2 ++ src/transport.rs | 6 +++++- src/tunnel.rs | 24 +++++++++++++++++++++--- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index 0901a78..9527e39 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -1,5 +1,4 @@ extern crate rand; - extern crate rand_os; extern crate ed25519_dalek; use ed25519_dalek::{PublicKey as PK, Keypair, Signature}; @@ -19,13 +18,12 @@ pub struct PublicKey { pub struct Keys { pub pair: Keypair, - csprng: OsRng, } impl Keys { pub fn gen() -> Keys { let mut osrng = OsRng {}; - Keys { pair: Keypair::generate(&mut osrng), csprng: osrng } + Keys { pair: Keypair::generate(&mut osrng) } } } diff --git a/src/ironforce.rs b/src/ironforce.rs index b246bbf..237fa7c 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -10,6 +10,7 @@ pub struct IronForce { key_pack: Keys, } +#[allow(dead_code)] impl IronForce { pub fn new() -> IronForce { IronForce { transport: Transport::new(), key_pack: Keys::gen() } @@ -43,10 +44,10 @@ impl IronForce { self.service_msg(&msg.content.body) }, MsgType::MultiCast => { - + // todo }, MsgType::UniCast(target) => { - + // todo } } } diff --git a/src/message.rs b/src/message.rs index 01b65c8..5349519 100644 --- a/src/message.rs +++ b/src/message.rs @@ -52,6 +52,7 @@ impl MessageContent { to_vec(&self).expect("Message content serialization failed") } + #[allow(dead_code)] pub fn deserialize(serialized: Vec) -> Self { from_bytes(&serialized).expect("Message content deserialization failed") } @@ -101,6 +102,7 @@ impl Message { to_vec(&self).expect("Message serialization failed") } + #[allow(dead_code)] pub fn deserialize(serialized: Vec) -> Self { from_bytes(&serialized).expect("Message deserialization failed") } diff --git a/src/transport.rs b/src/transport.rs index f2efb1b..ea06f0a 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -10,6 +10,7 @@ use std::thread; use std::sync::{Arc, Mutex}; use alloc::vec::Vec; +#[cfg(feature = "std")] use alloc::boxed::Box; #[cfg(not(feature = "std"))] @@ -27,8 +28,11 @@ impl Transport { Self {} } - pub fn send(&self, msg: &Message) {} + pub fn send(&self, msg: &Message) { + // todo + } + #[allow(dead_code)] pub fn receive(&self) -> (Message, PublicKey) { (Message::new(MsgType::Service, Vec::::new(), diff --git a/src/tunnel.rs b/src/tunnel.rs index 2b9f247..38f750e 100644 --- a/src/tunnel.rs +++ b/src/tunnel.rs @@ -1,7 +1,6 @@ use crate::crypto::PublicKey; use alloc::vec::Vec; -use alloc::boxed::Box; use serde::{Serialize, Deserialize}; use core::option::Option; @@ -15,6 +14,13 @@ impl Tunnel { Self { nodes } } + pub fn is_in_tunnel(&self, node: &PublicKey) -> bool { + self.nodes + .iter() + .enumerate() + .find(|¤t_node| current_node.1 == node).is_some() + } + pub fn tunnel_to(&self, me: &PublicKey, to: &PublicKey) -> Option { let my_index = self.nodes .iter() @@ -36,7 +42,19 @@ impl Tunnel { } } - pub fn next(&self, to: &PublicKey) -> PublicKey { - PublicKey { key: [0u8; 32] } + #[allow(dead_code)] + pub fn next(&self, me: &PublicKey, to: &PublicKey) -> Option { + let my_index = self.nodes + .iter() + .enumerate() + .find(|&node| node.1 == me).unwrap().0; + let to_index = self.nodes + .iter() + .enumerate() + .find(|&node| node.1 == to).unwrap().0; + if to_index <= my_index { + return None; + } + Option::from(self.nodes[my_index + 1].clone()) } }