Browse Source

Sending messages

master
Lev 3 years ago
parent
commit
53252f72b8
  1. 2
      src/interface.rs
  2. 47
      src/ironforce.rs
  3. 4
      src/message.rs

2
src/interface.rs

@ -53,7 +53,6 @@ pub mod test_interface {
use alloc::vec; use alloc::vec;
use alloc::string::ToString; use alloc::string::ToString;
use alloc::sync::Arc; use alloc::sync::Arc;
use std::println;
use spin::Mutex; use spin::Mutex;
#[derive(Default)] #[derive(Default)]
@ -113,7 +112,6 @@ pub mod test_interface {
.storage .storage
.lock() .lock()
.push((Vec::from(message), target.unwrap_or_default())); .push((Vec::from(message), target.unwrap_or_default()));
println!("Sending in testinterface. Storage: {:?}", self.storage.lock());
Ok(()) Ok(())
} }

47
src/ironforce.rs

@ -73,11 +73,24 @@ impl IronForce {
/// Send a message through tunnel /// Send a message through tunnel
fn send_through_tunnel( fn send_through_tunnel(
&mut self, &mut self,
_tunnel_id: u64, tunnel_id: u64,
_message: Message, mut message: Message,
_direction: Option<bool>, direction: Option<bool>,
) -> IFResult<()> { ) -> 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, /// Send a message to another node,
@ -209,6 +222,7 @@ mod if_testing {
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use crate::message::{Message, MessageType};
fn create_test_network() -> Vec<IronForce> { fn create_test_network() -> Vec<IronForce> {
let interfaces = create_test_interfaces(5); let interfaces = create_test_interfaces(5);
@ -238,7 +252,30 @@ mod if_testing {
network[1].main_loop_iteration()?; network[1].main_loop_iteration()?;
network[0].main_loop_iteration()?; network[0].main_loop_iteration()?;
assert!(!network[0].tunnels.is_empty()); 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(()) Ok(())
} }
} }

4
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 /// 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<Vec<u8>>, recipient_verification: Option<Vec<u8>>,
/// ID of the tunnel that is used /// ID of the tunnel that is used
tunnel_id: u64, pub tunnel_id: u64,
/// Network info /// Network info
network_info: NetworkInfo, network_info: NetworkInfo,
} }
@ -185,7 +185,7 @@ impl Message {
} }
/// Get decrypted content of the message /// Get decrypted content of the message
pub fn get_decrypted(&self, keys: Keys) -> IFResult<Vec<u8>> { pub fn get_decrypted(&self, keys: &Keys) -> IFResult<Vec<u8>> {
Ok(match &self.content { Ok(match &self.content {
MessageContent::Plain(c) => c.clone(), MessageContent::Plain(c) => c.clone(),
MessageContent::Encrypted(encrypted_content) => { MessageContent::Encrypted(encrypted_content) => {

Loading…
Cancel
Save