From 099013bc6d6d69b56a8527b283b537ae778db2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 21 Mar 2023 17:19:17 +0000 Subject: [PATCH] propagating packet size information to message preparer --- .../real_messages_control/message_handler.rs | 23 +++++++++++----- .../src/client/real_messages_control/mod.rs | 3 ++- common/nymsphinx/src/preparer/mod.rs | 26 +++++++++++++------ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/common/client-core/src/client/real_messages_control/message_handler.rs b/common/client-core/src/client/real_messages_control/message_handler.rs index cad6974b25..11d69ffc2a 100644 --- a/common/client-core/src/client/real_messages_control/message_handler.rs +++ b/common/client-core/src/client/real_messages_control/message_handler.rs @@ -101,8 +101,11 @@ pub(crate) struct Config { /// Note that it does not include gateway hops. num_mix_hops: u8, - /// Predefined packet size used for the encapsulated messages. - packet_size: PacketSize, + /// Primary predefined packet size used for the encapsulated messages. + primary_packet_size: PacketSize, + + /// Optional secondary predefined packet size used for the encapsulated messages. + secondary_packet_size: Option, } impl Config { @@ -118,7 +121,8 @@ impl Config { average_packet_delay, average_ack_delay, num_mix_hops: DEFAULT_NUM_MIX_HOPS, - packet_size: PacketSize::default(), + primary_packet_size: PacketSize::default(), + secondary_packet_size: None, } } @@ -130,8 +134,14 @@ impl Config { } /// Allows setting non-default size of the sphinx packets sent out. - pub fn with_custom_packet_size(mut self, packet_size: PacketSize) -> Self { - self.packet_size = packet_size; + pub fn with_custom_primary_packet_size(mut self, packet_size: PacketSize) -> Self { + self.primary_packet_size = packet_size; + self + } + + /// Allows setting non-default size of the sphinx packets sent out. + pub fn with_custom_secondary_packet_size(mut self, packet_size: Option) -> Self { + self.secondary_packet_size = packet_size; self } } @@ -170,7 +180,8 @@ where config.average_packet_delay, config.average_ack_delay, ) - .with_custom_real_message_packet_size(config.packet_size) + .with_custom_primary_packet_size(config.primary_packet_size) + .with_custom_secondary_packet_size(config.secondary_packet_size) .with_mix_hops(config.num_mix_hops); MessageHandler { diff --git a/common/client-core/src/client/real_messages_control/mod.rs b/common/client-core/src/client/real_messages_control/mod.rs index 140599bb0a..9771b17908 100644 --- a/common/client-core/src/client/real_messages_control/mod.rs +++ b/common/client-core/src/client/real_messages_control/mod.rs @@ -102,7 +102,8 @@ impl<'a> From<&'a Config> for message_handler::Config { cfg.traffic.average_packet_delay, cfg.acks.average_ack_delay, ) - .with_custom_packet_size(cfg.traffic.primary_packet_size) + .with_custom_primary_packet_size(cfg.traffic.primary_packet_size) + .with_custom_secondary_packet_size(cfg.traffic.secondary_packet_size) } } diff --git a/common/nymsphinx/src/preparer/mod.rs b/common/nymsphinx/src/preparer/mod.rs index d7445275ca..0442dfb259 100644 --- a/common/nymsphinx/src/preparer/mod.rs +++ b/common/nymsphinx/src/preparer/mod.rs @@ -46,7 +46,10 @@ pub struct MessagePreparer { rng: R, /// Size of the target [`SphinxPacket`] into which the underlying is going to get split. - packet_size: PacketSize, + primary_packet_size: PacketSize, + + /// Alternative size of the target [`SphinxPacket`] into which the underlying is going to get split. + secondary_packet_size: Option, /// Address of this client which also represent an address to which all acknowledgements /// and surb-based are going to be sent. @@ -75,11 +78,12 @@ where ) -> Self { MessagePreparer { rng, - packet_size: Default::default(), sender_address, average_packet_delay, average_ack_delay, num_mix_hops: DEFAULT_NUM_MIX_HOPS, + primary_packet_size: PacketSize::RegularPacket, + secondary_packet_size: None, } } @@ -90,8 +94,14 @@ where } /// Allows setting non-default size of the sphinx packets sent out. - pub fn with_custom_real_message_packet_size(mut self, packet_size: PacketSize) -> Self { - self.packet_size = packet_size; + pub fn with_custom_primary_packet_size(mut self, packet_size: PacketSize) -> Self { + self.primary_packet_size = packet_size; + self + } + + /// Allows setting non-default size of the sphinx packets sent out. + pub fn with_custom_secondary_packet_size(mut self, packet_size: Option) -> Self { + self.secondary_packet_size = packet_size; self } @@ -150,10 +160,10 @@ where let packet_payload = NymsphinxPayloadBuilder::new(fragment, surb_ack) .build_reply(reply_surb.encryption_key()); - // the unwrap here is fine as the failures can only originate from attempting to use invalid payload lenghts + // the unwrap here is fine as the failures can only originate from attempting to use invalid payload lengths // and we just very carefully constructed a (presumably) valid one let (sphinx_packet, first_hop_address) = reply_surb - .apply_surb(packet_payload, Some(self.packet_size)) + .apply_surb(packet_payload, Some(self.primary_packet_size)) .unwrap(); Ok(PreparedFragment { @@ -213,7 +223,7 @@ where // create the actual sphinx packet here. With valid route and correct payload size, // there's absolutely no reason for this call to fail. let sphinx_packet = SphinxPacketBuilder::new() - .with_payload_size(self.packet_size.payload_size()) + .with_payload_size(self.primary_packet_size.payload_size()) .build_packet(packet_payload, &route, &destination, &delays) .unwrap(); @@ -249,7 +259,7 @@ where } pub fn pad_and_split_message(&mut self, message: NymMessage) -> Vec { - let plaintext_per_packet = message.available_plaintext_per_packet(self.packet_size); + let plaintext_per_packet = message.available_plaintext_per_packet(self.primary_packet_size); message .pad_to_full_packet_lengths(plaintext_per_packet)