|
|
|
@ -73,11 +73,24 @@ impl IronForce {
|
|
|
|
|
/// Send a message through tunnel
|
|
|
|
|
fn send_through_tunnel( |
|
|
|
|
&mut self, |
|
|
|
|
_tunnel_id: u64, |
|
|
|
|
_message: Message, |
|
|
|
|
_direction: Option<bool>, |
|
|
|
|
tunnel_id: u64, |
|
|
|
|
mut message: Message, |
|
|
|
|
direction: Option<bool>, |
|
|
|
|
) -> 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<IronForce> { |
|
|
|
|
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(()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|