Browse Source

Tunnel methods

master
ennucore 5 years ago
parent
commit
ba80cbfc99
  1. 4
      src/crypto.rs
  2. 5
      src/ironforce.rs
  3. 2
      src/message.rs
  4. 6
      src/transport.rs
  5. 24
      src/tunnel.rs

4
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) }
}
}

5
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
}
}
}

2
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<u8>) -> 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<u8>) -> Self {
from_bytes(&serialized).expect("Message deserialization failed")
}

6
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::<u8>::new(),

24
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(|&current_node| current_node.1 == node).is_some()
}
pub fn tunnel_to(&self, me: &PublicKey, to: &PublicKey) -> Option<Tunnel> {
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<PublicKey> {
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())
}
}

Loading…
Cancel
Save