diff options
| author | Will Dillon <william@housedillon.com> | 2025-11-08 06:54:10 +0000 |
|---|---|---|
| committer | Will Dillon <william@housedillon.com> | 2025-11-08 06:54:10 +0000 |
| commit | 6ce2b201ae021f055df181fac19ea53b41d77895 (patch) | |
| tree | 7cc778bef6969270ee571d7afd633412bcfddec1 /src/packet.rs | |
| parent | Back over to the laptop (diff) | |
| download | meshcore-rs-6ce2b201ae021f055df181fac19ea53b41d77895.tar.gz meshcore-rs-6ce2b201ae021f055df181fac19ea53b41d77895.zip | |
Getting pretty close to finished
Diffstat (limited to 'src/packet.rs')
| -rw-r--r-- | src/packet.rs | 78 |
1 files changed, 67 insertions, 11 deletions
diff --git a/src/packet.rs b/src/packet.rs index c4e3809..ba98ac9 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,9 +1,9 @@ -use std::str::FromStr; +use std::{fmt::{Debug, Display}, str::FromStr}; use hex::decode; use tokio_util::bytes::{Buf, Bytes}; use structdiff::{Difference, StructDiff}; -use crate::{crypto::SharedSecret, packet_content::PacketContent}; +use crate::{crypto::SharedSecret, identity::Keystore, packet_content::PacketContent}; #[derive(PartialEq, Debug, Clone, Difference)] #[difference(expose)] @@ -132,28 +132,60 @@ impl Default for Packet { } } +impl Display for Packet { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.route_type, f)?; + f.write_str(" | ")?; + std::fmt::Display::fmt(&self.version, f)?; + + if self.route_type == RouteType::TransportDirect || self.route_type == RouteType::TransportFlood { + f.write_fmt(format_args!(" | {:4x?}, {:4x?} | ", self.transport[0], self.transport[1]))? + } else { + f.write_fmt(format_args!(" | |"))? + } + + let len = self.path.len(); + match self.path.len() { + 0 => f.write_fmt(format_args!(" [] | ")), + 1 => f.write_fmt(format_args!(" [{:2x?}] | ", self.path[0])), + 2 => f.write_fmt(format_args!(" [{:2x?}, {:2x?}] | ", self.path[0], self.path[1])), + 3 => f.write_fmt(format_args!(" [{:2x?}, {:2x?}, {:2x?}] | ", self.path[0], self.path[1], self.path[2])), + _ => f.write_fmt(format_args!(" [{:2x?}, {:2x?}, ... {:2x?}, {:2x?}] | ", self.path[0], self.path[1], self.path[len - 2], self.path[len -1])), + }?; + + if self.incomplete { + f.write_str("x | ") + } else { + f.write_str(" | ") + }?; + + std::fmt::Display::fmt(&self.content, f) + } +} + impl Packet { - pub fn try_decrypt(&mut self, key: SharedSecret) -> bool { - + pub fn try_decrypt(&mut self, keystore: &Keystore) -> bool { match self.content { // Encrypted packet types PacketContent::Path(ref mut path) => { - path.cipher.try_decrypt(key) + path.cipher.try_decrypt(keystore) }, PacketContent::Request(ref mut request) => { - request.cipher.try_decrypt(key) + request.cipher.try_decrypt(keystore) }, PacketContent::Response(ref mut response) => { - response.cipher.try_decrypt(key) + response.cipher.try_decrypt(keystore) }, PacketContent::Text(ref mut text) => { - text.cipher.try_decrypt(key) + text.cipher.try_decrypt(keystore) }, PacketContent::AnonReq(ref mut anon_req) => { - let decrypt_reault = key.mac_then_decrypt( + let decrypt_reault = keystore.decrypt_anon( + anon_req.dest, + &anon_req.public_key, anon_req.mac, - anon_req.ciphertext.clone() + &anon_req.ciphertext ); if let Some(cleartext) = decrypt_reault { @@ -164,7 +196,7 @@ impl Packet { } PacketContent::GroupText(ref mut group_text) => { - group_text.try_decrypt(key) + group_text.try_decrypt(keystore) } // None of the other packets implement any encryption @@ -182,6 +214,18 @@ pub enum RouteType { Invalid, } +impl Display for RouteType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + RouteType::TransportFlood => f.write_str("T-Flood "), + RouteType::Flood => f.write_str(" Flood "), + RouteType::Direct => f.write_str(" Direct"), + RouteType::TransportDirect => f.write_str("T-Direct"), + RouteType::Invalid => f.write_str("INVALID "), + } + } +} + impl From<u8> for RouteType { fn from(value: u8) -> Self { match value & 0x03 { @@ -218,6 +262,18 @@ impl From<u8> for PayloadVersion { } } +impl Display for PayloadVersion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + PayloadVersion::VersionOne => f.write_str("v1"), + PayloadVersion::VersionTwo => f.write_str("v2"), + PayloadVersion::VersionThree => f.write_str("v3"), + PayloadVersion::VersionFour => f.write_str("v4"), + PayloadVersion::Invalid => f.write_str("xx"), + } + } +} + #[allow(dead_code)] pub(crate) fn print_compare(lhs: Packet, rhs: Packet) -> String { let mut output_string = format!("Left hand side: \n{:#?}\nRight hand side: \n{:#?}\nDifferences: \n", lhs, rhs); |
