b5c8b69547
* Experiment with serde * Framed encoding serde POC * Outfox framing * Outfox rest compat (#3333) * Outfox forwarding compat * Tidy up interface * PacketSize compat * Address PR comments * Rebase on develop commit342883fcbeAuthor: durch <durch@users.noreply.github.com> Date: Thu Apr 27 09:17:18 2023 +0200 Put back PacketType 1 commit61a0ee5a19Author: Tommy Verrall <tommyvez@protonmail.com> Date: Wed Apr 26 16:37:29 2023 +0100 change output for cpu-cycle management logs commit3956109c7eAuthor: Tommy Verrall <tommy@nymtech.net> Date: Wed Apr 26 12:13:22 2023 +0100 change the workflow file to build with cpucycles commit8d725b13c5Author: durch <durch@users.noreply.github.com> Date: Mon Apr 24 13:14:58 2023 +0200 Outfox client compat commit4d166c389bAuthor: durch <durch@users.noreply.github.com> Date: Fri Apr 21 00:30:46 2023 +0200 Address PR comments commit145c3c1223Author: durch <durch@users.noreply.github.com> Date: Fri Apr 21 00:12:35 2023 +0200 Rename PacketMode commitcbd654d6fdAuthor: Drazen Urch <drazen@urch.eu> Date: Thu Apr 20 23:59:40 2023 +0200 Outfox rest compat (#3333) * Outfox forwarding compat * Tidy up interface * PacketSize compat commite7be91a94cAuthor: durch <durch@users.noreply.github.com> Date: Wed Apr 19 16:36:48 2023 +0200 Remove serde cruft commit582e7d566aAuthor: durch <durch@users.noreply.github.com> Date: Wed Apr 19 16:24:09 2023 +0200 Outfox framing commit6464da5f01Author: durch <durch@users.noreply.github.com> Date: Tue Apr 18 22:23:02 2023 +0200 Framing compat commitd5e77e499bAuthor: durch <durch@users.noreply.github.com> Date: Tue Apr 18 18:18:54 2023 +0200 Framed encoding serde POC commitf086f9c35aAuthor: durch <durch@users.noreply.github.com> Date: Tue Apr 18 16:54:21 2023 +0200 Experiment with serde * Client tweaks * Speed up from_plaintext * SurbAcks * More work on the reciever end, and outfox format * Cleanup and fmt * Wrap up rebase * Happy clippy * Fix lock files * Final cleanup
116 lines
3.3 KiB
Rust
116 lines
3.3 KiB
Rust
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
use nym_sphinx_addressing::nodes::{NymNodeRoutingAddress, NymNodeRoutingAddressError};
|
|
use nym_sphinx_params::{PacketSize, PacketType};
|
|
use nym_sphinx_types::{NymPacket, NymPacketError};
|
|
use std::convert::TryFrom;
|
|
use std::fmt::{self, Debug, Formatter};
|
|
use thiserror::Error;
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum MixPacketFormattingError {
|
|
#[error("too few bytes provided to recover from bytes")]
|
|
TooFewBytesProvided,
|
|
#[error("provided packet mode is invalid")]
|
|
InvalidPacketType,
|
|
#[error("received request had invalid size - received {0}")]
|
|
InvalidPacketSize(usize),
|
|
#[error("address field was incorrectly encoded")]
|
|
InvalidAddress,
|
|
#[error("received sphinx packet was malformed")]
|
|
MalformedSphinxPacket,
|
|
#[error("Packet: {0}")]
|
|
Packet(#[from] NymPacketError),
|
|
}
|
|
|
|
impl From<NymNodeRoutingAddressError> for MixPacketFormattingError {
|
|
fn from(_: NymNodeRoutingAddressError) -> Self {
|
|
MixPacketFormattingError::InvalidAddress
|
|
}
|
|
}
|
|
|
|
pub struct MixPacket {
|
|
next_hop: NymNodeRoutingAddress,
|
|
packet: NymPacket,
|
|
packet_type: PacketType,
|
|
}
|
|
|
|
impl Debug for MixPacket {
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
write!(
|
|
f,
|
|
"MixPacket to {:?} with packet_type {:?}. Packet {:?}",
|
|
self.next_hop, self.packet_type, self.packet
|
|
)
|
|
}
|
|
}
|
|
|
|
impl MixPacket {
|
|
pub fn new(
|
|
next_hop: NymNodeRoutingAddress,
|
|
packet: NymPacket,
|
|
packet_type: PacketType,
|
|
) -> Self {
|
|
MixPacket {
|
|
next_hop,
|
|
packet,
|
|
packet_type,
|
|
}
|
|
}
|
|
|
|
pub fn next_hop(&self) -> NymNodeRoutingAddress {
|
|
self.next_hop
|
|
}
|
|
|
|
pub fn packet(&self) -> &NymPacket {
|
|
&self.packet
|
|
}
|
|
|
|
pub fn into_packet(self) -> NymPacket {
|
|
self.packet
|
|
}
|
|
|
|
pub fn packet_type(&self) -> PacketType {
|
|
self.packet_type
|
|
}
|
|
|
|
// the message is formatted as follows:
|
|
// packet_type || FIRST_HOP || packet
|
|
pub fn try_from_bytes(b: &[u8]) -> Result<Self, MixPacketFormattingError> {
|
|
let packet_type = match PacketType::try_from(b[0]) {
|
|
Ok(mode) => mode,
|
|
Err(_) => return Err(MixPacketFormattingError::InvalidPacketType),
|
|
};
|
|
|
|
let next_hop = NymNodeRoutingAddress::try_from_bytes(&b[1..])?;
|
|
let addr_offset = next_hop.bytes_min_len();
|
|
|
|
let packet_data = &b[addr_offset + 1..];
|
|
let packet_size = packet_data.len();
|
|
if PacketSize::get_type(packet_size).is_err() {
|
|
Err(MixPacketFormattingError::InvalidPacketSize(packet_size))
|
|
} else {
|
|
let packet = match packet_type {
|
|
PacketType::Outfox => NymPacket::outfox_from_bytes(packet_data)?,
|
|
_ => NymPacket::sphinx_from_bytes(packet_data)?,
|
|
};
|
|
|
|
Ok(MixPacket {
|
|
next_hop,
|
|
packet,
|
|
packet_type,
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn into_bytes(self) -> Result<Vec<u8>, MixPacketFormattingError> {
|
|
Ok(std::iter::once(self.packet_type as u8)
|
|
.chain(self.next_hop.as_bytes().into_iter())
|
|
.chain(self.packet.to_bytes()?.into_iter())
|
|
.collect())
|
|
}
|
|
}
|
|
|
|
// TODO: test for serialization and errors!
|