nym_client_core/client/
inbound_messages.rs

1// Copyright 2020-2023 - Nym Technologies SA <contact@nymtech.net>
2// SPDX-License-Identifier: Apache-2.0
3
4use nym_sphinx::addressing::clients::Recipient;
5use nym_sphinx::anonymous_replies::requests::AnonymousSenderTag;
6use nym_sphinx::forwarding::packet::MixPacket;
7use nym_sphinx::params::PacketType;
8use nym_task::connections::TransmissionLane;
9
10pub type InputMessageSender = tokio::sync::mpsc::Sender<InputMessage>;
11pub type InputMessageReceiver = tokio::sync::mpsc::Receiver<InputMessage>;
12
13#[derive(Debug)]
14pub enum InputMessage {
15    /// Fire an already prepared mix packets into the network.
16    /// No guarantees are made about it. For example no retransmssion
17    /// will be attempted if it gets dropped.
18    Premade {
19        msgs: Vec<MixPacket>,
20        lane: TransmissionLane,
21    },
22
23    /// The simplest message variant where no additional information is attached.
24    /// You're simply sending your `data` to specified `recipient` without any tagging.
25    ///
26    /// Ends up with `NymMessage::Plain` variant
27    Regular {
28        recipient: Recipient,
29        data: Vec<u8>,
30        lane: TransmissionLane,
31        max_retransmissions: Option<u32>,
32    },
33
34    /// Creates a message used for a duplex anonymous communication where the recipient
35    /// will never learn of our true identity. This is achieved by carefully sending `reply_surbs`.
36    ///
37    /// Note that if reply_surbs is set to zero then
38    /// this variant requires the client having sent some reply_surbs in the past
39    /// (and thus the recipient also knowing our sender tag).
40    ///
41    /// Ends up with `NymMessage::Repliable` variant
42    Anonymous {
43        recipient: Recipient,
44        data: Vec<u8>,
45        reply_surbs: u32,
46        lane: TransmissionLane,
47        max_retransmissions: Option<u32>,
48    },
49
50    /// Attempt to use our internally received and stored `ReplySurb` to send the message back
51    /// to specified recipient whilst not knowing its full identity (or even gateway).
52    ///
53    /// Ends up with `NymMessage::Reply` variant
54    Reply {
55        recipient_tag: AnonymousSenderTag,
56        data: Vec<u8>,
57        lane: TransmissionLane,
58        max_retransmissions: Option<u32>,
59    },
60
61    MessageWrapper {
62        message: Box<InputMessage>,
63        packet_type: PacketType,
64    },
65}
66
67impl InputMessage {
68    pub fn new_premade(
69        msgs: Vec<MixPacket>,
70        lane: TransmissionLane,
71        packet_type: PacketType,
72    ) -> Self {
73        let message = InputMessage::Premade { msgs, lane };
74        if packet_type == PacketType::Mix {
75            message
76        } else {
77            InputMessage::new_wrapper(message, packet_type)
78        }
79    }
80
81    pub fn new_wrapper(message: InputMessage, packet_type: PacketType) -> Self {
82        InputMessage::MessageWrapper {
83            message: Box::new(message),
84            packet_type,
85        }
86    }
87
88    pub fn new_regular(
89        recipient: Recipient,
90        data: Vec<u8>,
91        lane: TransmissionLane,
92        packet_type: Option<PacketType>,
93    ) -> Self {
94        let message = InputMessage::Regular {
95            recipient,
96            data,
97            lane,
98            max_retransmissions: None,
99        };
100        if let Some(packet_type) = packet_type {
101            InputMessage::new_wrapper(message, packet_type)
102        } else {
103            message
104        }
105    }
106
107    pub fn new_anonymous(
108        recipient: Recipient,
109        data: Vec<u8>,
110        reply_surbs: u32,
111        lane: TransmissionLane,
112        packet_type: Option<PacketType>,
113    ) -> Self {
114        let message = InputMessage::Anonymous {
115            recipient,
116            data,
117            reply_surbs,
118            lane,
119            max_retransmissions: None,
120        };
121        if let Some(packet_type) = packet_type {
122            InputMessage::new_wrapper(message, packet_type)
123        } else {
124            message
125        }
126    }
127
128    pub fn new_reply(
129        recipient_tag: AnonymousSenderTag,
130        data: Vec<u8>,
131        lane: TransmissionLane,
132        packet_type: Option<PacketType>,
133    ) -> Self {
134        let message = InputMessage::Reply {
135            recipient_tag,
136            data,
137            lane,
138            // \/ set it to SOME sane default so that if we run out of surbs and constantly
139            // fail to request more, we wouldn't be stuck in limbo
140            max_retransmissions: Some(10),
141        };
142        if let Some(packet_type) = packet_type {
143            InputMessage::new_wrapper(message, packet_type)
144        } else {
145            message
146        }
147    }
148
149    pub fn lane(&self) -> &TransmissionLane {
150        match self {
151            InputMessage::Regular { lane, .. }
152            | InputMessage::Anonymous { lane, .. }
153            | InputMessage::Reply { lane, .. }
154            | InputMessage::Premade { lane, .. } => lane,
155            InputMessage::MessageWrapper { message, .. } => message.lane(),
156        }
157    }
158
159    pub fn set_max_retransmissions(&mut self, max_retransmissions: u32) -> &mut Self {
160        match self {
161            InputMessage::Regular {
162                max_retransmissions: m,
163                ..
164            }
165            | InputMessage::Anonymous {
166                max_retransmissions: m,
167                ..
168            }
169            | InputMessage::Reply {
170                max_retransmissions: m,
171                ..
172            } => {
173                *m = Some(max_retransmissions);
174            }
175            InputMessage::Premade { .. } => {}
176            InputMessage::MessageWrapper { message, .. } => {
177                message.set_max_retransmissions(max_retransmissions);
178            }
179        }
180
181        self
182    }
183
184    pub fn with_max_retransmissions(mut self, max_retransmissions: u32) -> Self {
185        self.set_max_retransmissions(max_retransmissions);
186        self
187    }
188}