From af5d27dc973b576da4b26467d40feb16ffe51b9f Mon Sep 17 00:00:00 2001 From: ennucore Date: Fri, 10 Dec 2021 23:15:32 +0300 Subject: [PATCH] Sending messages --- src/interface.rs | 2 -- src/ironforce.rs | 47 ++++++++++++++++++++++++++++++++++++++++++----- src/message.rs | 4 ++-- 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/src/interface.rs b/src/interface.rs index 405ecdc..6dd7572 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -53,7 +53,6 @@ pub mod test_interface { use alloc::vec; use alloc::string::ToString; use alloc::sync::Arc; - use std::println; use spin::Mutex; #[derive(Default)] @@ -113,7 +112,6 @@ pub mod test_interface { .storage .lock() .push((Vec::from(message), target.unwrap_or_default())); - println!("Sending in testinterface. Storage: {:?}", self.storage.lock()); Ok(()) } diff --git a/src/ironforce.rs b/src/ironforce.rs index d2042fd..a836497 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -73,11 +73,24 @@ impl IronForce { /// Send a message through tunnel fn send_through_tunnel( &mut self, - _tunnel_id: u64, - _message: Message, - _direction: Option, + tunnel_id: u64, + mut message: Message, + direction: Option, ) -> IFResult<()> { - todo!() + let tunnel: Tunnel = if let Some(tun) = self.tunnels.iter().cloned().find(|t| t.id == Some(tunnel_id)) { tun } else { return Err(IFError::TunnelNotFound) }; + message.tunnel_id = tunnel_id; + let peer_ids = match (direction, tunnel.peer_ids) { + (_, (x, 0)) => vec![x], + (_, (0, x)) => vec![x], + (None, (x1, x2)) => vec![x1, x2], + (Some(true), (x1, _x2)) => vec![x1], + (Some(false), (_x1, x2)) => vec![x2], + }; + let msg_bytes = serde_cbor::to_vec(&message)?; + for peer in peer_ids { + self.transport.send_message(msg_bytes.clone(), Some(peer))?; + } + Ok(()) } /// Send a message to another node, @@ -209,6 +222,7 @@ mod if_testing { use alloc::boxed::Box; use alloc::vec; use alloc::vec::Vec; + use crate::message::{Message, MessageType}; fn create_test_network() -> Vec { let interfaces = create_test_interfaces(5); @@ -238,7 +252,30 @@ mod if_testing { network[1].main_loop_iteration()?; network[0].main_loop_iteration()?; assert!(!network[0].tunnels.is_empty()); - // println!("T: {:?}", network[0].tunnels); + Ok(()) + } + + #[test] + fn test_sending_message() -> IFResult<()> { + let mut network = create_test_network(); + let key_1 = network[1].keys.get_public(); + network[0].initialize_tunnel_creation(key_1.clone())?; + network[0].main_loop_iteration()?; + network[1].main_loop_iteration()?; + network[0].main_loop_iteration()?; + let zero_keys = network[0].keys.clone(); + network[0].send_message( + Message::build() + .message_type(MessageType::SingleCast) + .sign(&zero_keys) + .recipient(key_1.clone()) + .content(b"hello".to_vec()) + .build()?, + key_1)?; + network[1].main_loop_iteration()?; + let msg = network[1].read_message(); + assert!(msg.is_some()); + assert_eq!(msg.unwrap().get_decrypted(&network[1].keys).unwrap().as_slice(), b"hello"); Ok(()) } } diff --git a/src/message.rs b/src/message.rs index 312c73f..eced2e9 100644 --- a/src/message.rs +++ b/src/message.rs @@ -138,7 +138,7 @@ pub struct Message { /// Optional: hash of the message encrypted for the recipient, so that the recipient can know that this message is for them, but nobody else recipient_verification: Option>, /// ID of the tunnel that is used - tunnel_id: u64, + pub tunnel_id: u64, /// Network info network_info: NetworkInfo, } @@ -185,7 +185,7 @@ impl Message { } /// Get decrypted content of the message - pub fn get_decrypted(&self, keys: Keys) -> IFResult> { + pub fn get_decrypted(&self, keys: &Keys) -> IFResult> { Ok(match &self.content { MessageContent::Plain(c) => c.clone(), MessageContent::Encrypted(encrypted_content) => {