|
|
@ -60,7 +60,7 @@ impl IronForce { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Create a new tunnel to another node
|
|
|
|
/// Create a new tunnel to another node
|
|
|
|
fn initialize_tunnel_creation(&mut self, destination: PublicKey) -> IFResult<()> { |
|
|
|
fn initialize_tunnel_creation(&mut self, destination: &PublicKey) -> IFResult<()> { |
|
|
|
let tunnel = TunnelPublic::new_singlecast(); |
|
|
|
let tunnel = TunnelPublic::new_singlecast(); |
|
|
|
#[cfg(std)] |
|
|
|
#[cfg(std)] |
|
|
|
println!( |
|
|
|
println!( |
|
|
@ -107,7 +107,7 @@ impl IronForce { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return Err(IFError::TunnelNotFound); |
|
|
|
return Err(IFError::TunnelNotFound); |
|
|
|
}; |
|
|
|
}; |
|
|
|
message.tunnel_id = tunnel_id; |
|
|
|
message.tunnel_id = (tunnel_id, tunnel.peer_ids.0 != 0); |
|
|
|
let peer_ids = match (direction, tunnel.peer_ids) { |
|
|
|
let peer_ids = match (direction, tunnel.peer_ids) { |
|
|
|
(_, (x, 0)) => vec![x], |
|
|
|
(_, (x, 0)) => vec![x], |
|
|
|
(_, (0, x)) => vec![x], |
|
|
|
(_, (0, x)) => vec![x], |
|
|
@ -124,15 +124,15 @@ impl IronForce { |
|
|
|
|
|
|
|
|
|
|
|
/// Send a message to another node,
|
|
|
|
/// Send a message to another node,
|
|
|
|
/// creating a new tunnel if needed
|
|
|
|
/// creating a new tunnel if needed
|
|
|
|
pub fn send_message(&mut self, message: Message, destination: PublicKey) -> IFResult<()> { |
|
|
|
pub fn send_message(&mut self, message: Message, destination: &PublicKey) -> IFResult<()> { |
|
|
|
if let Some(Some(tunnel_id)) = self |
|
|
|
if let Some(Some(tunnel_id)) = self |
|
|
|
.tunnels |
|
|
|
.tunnels |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.find(|t| { |
|
|
|
.find(|t| { |
|
|
|
t.target_node.as_ref() == Some(&destination) |
|
|
|
t.target_node.as_ref() == Some(destination) |
|
|
|
|| t.nodes_in_tunnel |
|
|
|
|| t.nodes_in_tunnel |
|
|
|
.as_ref() |
|
|
|
.as_ref() |
|
|
|
.map(|nodes| nodes.contains(&destination)) |
|
|
|
.map(|nodes| nodes.contains(destination)) |
|
|
|
== Some(true) |
|
|
|
== Some(true) |
|
|
|
}) |
|
|
|
}) |
|
|
|
.map(|tunnel| tunnel.id) |
|
|
|
.map(|tunnel| tunnel.id) |
|
|
@ -171,18 +171,12 @@ impl IronForce { |
|
|
|
let tunnel = Tunnel { |
|
|
|
let tunnel = Tunnel { |
|
|
|
id: tunnel_pub.id, |
|
|
|
id: tunnel_pub.id, |
|
|
|
local_ids: tunnel_pub.local_ids.clone(), |
|
|
|
local_ids: tunnel_pub.local_ids.clone(), |
|
|
|
peer_ids: (inc_peer, 0), |
|
|
|
peer_ids: (0, inc_peer), |
|
|
|
ttd: 0, |
|
|
|
ttd: 0, |
|
|
|
nodes_in_tunnel: None, |
|
|
|
nodes_in_tunnel: None, |
|
|
|
is_multicast: false, |
|
|
|
is_multicast: false, |
|
|
|
target_node: Some(sender), |
|
|
|
target_node: Some(sender), |
|
|
|
}; |
|
|
|
}; |
|
|
|
#[cfg(feature = "std")] |
|
|
|
|
|
|
|
println!( |
|
|
|
|
|
|
|
"[{}] Got an incoming tunnel for me! {:?}", |
|
|
|
|
|
|
|
self.short_id(), |
|
|
|
|
|
|
|
tunnel_pub |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
self.tunnels.push(tunnel); |
|
|
|
self.tunnels.push(tunnel); |
|
|
|
self.transport.send_message( |
|
|
|
self.transport.send_message( |
|
|
|
serde_cbor::to_vec( |
|
|
|
serde_cbor::to_vec( |
|
|
@ -192,7 +186,7 @@ impl IronForce { |
|
|
|
tunnel_pub.clone(), |
|
|
|
tunnel_pub.clone(), |
|
|
|
), |
|
|
|
), |
|
|
|
)) |
|
|
|
)) |
|
|
|
.tunnel(tunnel_pub.id.unwrap()) |
|
|
|
.tunnel((tunnel_pub.id.unwrap(), false)) |
|
|
|
.sign(&self.keys) |
|
|
|
.sign(&self.keys) |
|
|
|
.build()?, |
|
|
|
.build()?, |
|
|
|
)?, |
|
|
|
)?, |
|
|
@ -255,7 +249,17 @@ impl IronForce { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
MessageType::SingleCast | MessageType::Broadcast => self.messages.push(message), |
|
|
|
MessageType::SingleCast if message.check_recipient(&self.keys) => self.messages.push(message.clone()), |
|
|
|
|
|
|
|
MessageType::SingleCast => { |
|
|
|
|
|
|
|
if let Some(tunnel) = self.tunnels.iter().find(|tun| tun.id == Some(message.tunnel_id.0)) { |
|
|
|
|
|
|
|
let peer_id = if message.tunnel_id.1 { tunnel.peer_ids.0 } else { tunnel.peer_ids.1 }; |
|
|
|
|
|
|
|
self.transport.send_message(serde_cbor::to_vec(&message)?, Some(peer_id))?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
MessageType::Broadcast => { |
|
|
|
|
|
|
|
self.messages.push(message.clone()); |
|
|
|
|
|
|
|
self.send_to_all(message)?; |
|
|
|
|
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
@ -323,7 +327,7 @@ mod if_testing { |
|
|
|
fn test_creating_a_tunnel() -> IFResult<()> { |
|
|
|
fn test_creating_a_tunnel() -> IFResult<()> { |
|
|
|
let mut network = create_test_network(); |
|
|
|
let mut network = create_test_network(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
network[0].initialize_tunnel_creation(key_1)?; |
|
|
|
network[0].initialize_tunnel_creation(&key_1)?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
@ -335,7 +339,7 @@ mod if_testing { |
|
|
|
fn test_sending_message() -> IFResult<()> { |
|
|
|
fn test_sending_message() -> IFResult<()> { |
|
|
|
let mut network = create_test_network(); |
|
|
|
let mut network = create_test_network(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
network[0].initialize_tunnel_creation(key_1.clone())?; |
|
|
|
network[0].initialize_tunnel_creation(&key_1)?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
|
network[0].main_loop_iteration()?; |
|
|
@ -344,10 +348,10 @@ mod if_testing { |
|
|
|
Message::build() |
|
|
|
Message::build() |
|
|
|
.message_type(MessageType::SingleCast) |
|
|
|
.message_type(MessageType::SingleCast) |
|
|
|
.sign(&zero_keys) |
|
|
|
.sign(&zero_keys) |
|
|
|
.recipient(key_1.clone()) |
|
|
|
.recipient(&key_1) |
|
|
|
.content(b"hello".to_vec()) |
|
|
|
.content(b"hello".to_vec()) |
|
|
|
.build()?, |
|
|
|
.build()?, |
|
|
|
key_1, |
|
|
|
&key_1, |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
network[1].main_loop_iteration()?; |
|
|
|
let msg = network[1].read_message(); |
|
|
|
let msg = network[1].read_message(); |
|
|
@ -367,29 +371,29 @@ mod if_testing { |
|
|
|
#[cfg(feature = "std")] |
|
|
|
#[cfg(feature = "std")] |
|
|
|
mod test_with_ip { |
|
|
|
mod test_with_ip { |
|
|
|
use crate::crypto::Keys; |
|
|
|
use crate::crypto::Keys; |
|
|
|
use crate::interfaces::ip::IPInterface; |
|
|
|
|
|
|
|
use crate::ironforce::IronForce; |
|
|
|
use crate::ironforce::IronForce; |
|
|
|
use crate::res::IFResult; |
|
|
|
use crate::res::IFResult; |
|
|
|
use crate::transport::Transport; |
|
|
|
use crate::transport::Transport; |
|
|
|
use alloc::boxed::Box; |
|
|
|
use alloc::boxed::Box; |
|
|
|
use alloc::vec; |
|
|
|
use alloc::vec; |
|
|
|
use alloc::vec::Vec; |
|
|
|
use alloc::vec::Vec; |
|
|
|
use core::str::FromStr; |
|
|
|
|
|
|
|
use std::println; |
|
|
|
use std::println; |
|
|
|
|
|
|
|
use crate::interfaces::ip::create_test_interfaces; |
|
|
|
|
|
|
|
use crate::message::{Message, MessageType}; |
|
|
|
|
|
|
|
|
|
|
|
fn create_test_interfaces(n: usize) -> impl Iterator<Item = IPInterface> { |
|
|
|
// fn create_test_interfaces(n: usize) -> impl Iterator<Item = IPInterface> {
|
|
|
|
let ip_addr = std::net::IpAddr::from_str("127.0.0.1").unwrap(); |
|
|
|
// let ip_addr = std::net::IpAddr::from_str("127.0.0.1").unwrap();
|
|
|
|
(0..n).map(move |i| { |
|
|
|
// (0..n).map(move |i| {
|
|
|
|
IPInterface::new( |
|
|
|
// IPInterface::new(
|
|
|
|
(5000 + 5 * i) as u16, |
|
|
|
// (5000 + 5 * i) as u16,
|
|
|
|
(0..n) |
|
|
|
// (0..n)
|
|
|
|
.filter(|j| *j != i) |
|
|
|
// .filter(|j| *j != i)
|
|
|
|
.map(|j| (ip_addr, (5000 + 5 * j) as u16)) |
|
|
|
// .map(|j| (ip_addr, (5000 + 5 * j) as u16))
|
|
|
|
.collect(), |
|
|
|
// .collect(),
|
|
|
|
) |
|
|
|
// )
|
|
|
|
.unwrap() |
|
|
|
// .unwrap()
|
|
|
|
}) |
|
|
|
// })
|
|
|
|
} |
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
fn create_test_network() -> Vec<IronForce> { |
|
|
|
fn create_test_network() -> Vec<IronForce> { |
|
|
|
let interfaces = create_test_interfaces(4); |
|
|
|
let interfaces = create_test_interfaces(4); |
|
|
@ -411,43 +415,47 @@ mod test_with_ip { |
|
|
|
.collect() |
|
|
|
.collect() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MAIN TEST RIGHT HERE
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_creating_a_tunnel() -> IFResult<()> { |
|
|
|
fn test_creating_a_tunnel_and_sending_message() -> IFResult<()> { |
|
|
|
let mut network = create_test_network(); |
|
|
|
let mut network = create_test_network(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
let key_1 = network[1].keys.get_public(); |
|
|
|
let (mut node0, mut node1) = (network.remove(0), network.remove(0)); |
|
|
|
let (mut node0, mut node1) = (network.remove(0), network.remove(0)); |
|
|
|
|
|
|
|
let node0_keys = node0.keys.clone(); |
|
|
|
println!("node0 id: {}", node0.short_id()); |
|
|
|
println!("node0 id: {}", node0.short_id()); |
|
|
|
println!("node1 id: {}", node1.short_id()); |
|
|
|
println!("node1 id: {}", node1.short_id()); |
|
|
|
let (mut node2, mut node3) = (network.remove(0), network.remove(0)); |
|
|
|
let (mut node2, mut node3) = (network.remove(0), network.remove(0)); |
|
|
|
let t1 = std::thread::spawn(move || { |
|
|
|
let t1 = std::thread::spawn(move || { |
|
|
|
for _i in 0..5 { |
|
|
|
for _i in 0..170 { |
|
|
|
// println!("Iteration {} (1)", i);
|
|
|
|
// println!("Iteration {} (1)", i);
|
|
|
|
node0.main_loop_iteration().unwrap(); |
|
|
|
node0.main_loop_iteration().unwrap(); |
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10)); |
|
|
|
} |
|
|
|
} |
|
|
|
node0 |
|
|
|
node0 |
|
|
|
}); |
|
|
|
}); |
|
|
|
let t2 = std::thread::spawn(move || { |
|
|
|
let t2 = std::thread::spawn(move || { |
|
|
|
for _i in 0..15 { |
|
|
|
for _i in 0..250 { |
|
|
|
// println!("Iteration {} (2)", i);
|
|
|
|
// println!("Iteration {} (2)", i);
|
|
|
|
node1.main_loop_iteration().unwrap(); |
|
|
|
node1.main_loop_iteration().unwrap(); |
|
|
|
|
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10)); |
|
|
|
} |
|
|
|
} |
|
|
|
node1 |
|
|
|
node1 |
|
|
|
}); |
|
|
|
}); |
|
|
|
std::thread::spawn(move || loop { |
|
|
|
std::thread::spawn(move || loop { |
|
|
|
node2.main_loop_iteration().unwrap(); |
|
|
|
node2.main_loop_iteration().unwrap(); |
|
|
|
std::thread::sleep(std::time::Duration::from_secs(5)); |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10)); |
|
|
|
}); |
|
|
|
}); |
|
|
|
std::thread::spawn(move || loop { |
|
|
|
std::thread::spawn(move || loop { |
|
|
|
std::thread::sleep(std::time::Duration::from_secs(5)); |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(10)); |
|
|
|
node3.main_loop_iteration().unwrap(); |
|
|
|
node3.main_loop_iteration().unwrap(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
let mut node0 = t1.join().unwrap(); |
|
|
|
let mut node0 = t1.join().unwrap(); |
|
|
|
node0.initialize_tunnel_creation(key_1)?; |
|
|
|
node0.initialize_tunnel_creation(&key_1)?; |
|
|
|
let mut node1 = t2.join().unwrap(); |
|
|
|
let mut node1 = t2.join().unwrap(); |
|
|
|
let t1 = std::thread::spawn(move || { |
|
|
|
let t1 = std::thread::spawn(move || { |
|
|
|
for _ in 0..18 { |
|
|
|
for _ in 0..18 { |
|
|
|
node0.main_loop_iteration().unwrap(); |
|
|
|
node0.main_loop_iteration().unwrap(); |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(150)); |
|
|
|
std::thread::sleep(std::time::Duration::from_millis(50)); |
|
|
|
} |
|
|
|
} |
|
|
|
node0 |
|
|
|
node0 |
|
|
|
}); |
|
|
|
}); |
|
|
@ -457,9 +465,27 @@ mod test_with_ip { |
|
|
|
} |
|
|
|
} |
|
|
|
node1 |
|
|
|
node1 |
|
|
|
}); |
|
|
|
}); |
|
|
|
let node0 = t1.join().unwrap(); |
|
|
|
let mut node0 = t1.join().unwrap(); |
|
|
|
t2.join().unwrap(); |
|
|
|
let mut node1 = t2.join().unwrap(); |
|
|
|
assert!(!node0.tunnels.is_empty()); |
|
|
|
assert!(!node0.tunnels.is_empty()); |
|
|
|
|
|
|
|
node0.send_message( |
|
|
|
|
|
|
|
Message::build() |
|
|
|
|
|
|
|
.message_type(MessageType::SingleCast) |
|
|
|
|
|
|
|
.content(b"Hello!".to_vec()) |
|
|
|
|
|
|
|
.recipient(&key_1) |
|
|
|
|
|
|
|
.sign(&node0_keys) |
|
|
|
|
|
|
|
.build()?, |
|
|
|
|
|
|
|
&key_1)?; |
|
|
|
|
|
|
|
let t2 = std::thread::spawn(move || { |
|
|
|
|
|
|
|
for _ in 0..18 { |
|
|
|
|
|
|
|
node1.main_loop_iteration().unwrap(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
node1 |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
let mut node1 = t2.join().unwrap(); |
|
|
|
|
|
|
|
let msg = node1.read_message(); |
|
|
|
|
|
|
|
assert!(msg.is_some()); |
|
|
|
|
|
|
|
assert_eq!(msg.unwrap().get_decrypted(&node1.keys)?, b"Hello!".to_vec()); |
|
|
|
Ok(()) |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|