emissary-core 0.4.0

Rust implementation of the I2P protocol stack
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::{
    crypto::SigningPrivateKey,
    error::StreamingError,
    primitives::{Destination, DestinationId},
    runtime::Runtime,
    sam::protocol::streaming::packet::{Packet, PacketBuilder},
};

use rand::Rng;

use alloc::{collections::VecDeque, vec::Vec};

/// Logging target for the file.
const LOG_TARGET: &str = "emissary::streaming::pending";

/// Initial window size
const INITIAL_WINDOW_SIZE: usize = 6usize;

/// Result type returned from [`PendingStream::on_packet()`].
pub enum PendingStreamResult {
    /// Inbound packet doesn't require an action because it was, e.g., a duplicate ACK.
    DoNothing,

    /// Send packet to remote peer.
    Send {
        /// Packet.
        packet: Vec<u8>,
    },

    /// Send packet to remote peer and destroy the pending stream.
    SendAndDestroy {
        /// Packet.
        packet: Vec<u8>,
    },

    /// Destroy the pending stream because, e.g., `RESET` was received.
    Destroy,
}

/// Pending stream.
///
/// Inbound stream which has been accepted by [`StreamManager`] but which hasn't been converted into
/// an active stream because there are no active listeners who could accept the stream.
///
/// Pending streams are periodically pruned ([`PENDING_STREAM_PRUNE_THRESHOLD`]) if they haven't
/// been accepted within that time window by the client. The stream is pruned before
/// [`PENDING_STREAM_PRUNE_THRESHOLD`] if a full window has been received without client accepting
/// the stream, either by register `STREAM ACCEPT` or `STREAM FORWARD`.
pub struct PendingStream<R: Runtime> {
    /// Destination ID of the remote peer.
    pub destination_id: DestinationId,

    /// Destination of remote peer.
    pub remote_destination: Destination,

    /// When was the stream established.
    pub established: R::Instant,

    /// Pending packets.
    ///
    /// Packets that have been received and ACKed while the stream was pending.
    pub packets: VecDeque<Vec<u8>>,

    /// Receive stream ID.
    pub recv_stream_id: u32,

    /// Send stream ID.
    pub send_stream_id: u32,

    /// Current sequnce number of the remote peer.
    pub seq_nro: u32,
}

impl<R: Runtime> PendingStream<R> {
    /// Create new [`PendingStream`].
    ///
    /// `syn_payload` is the payload contained within the `SYN` message and may be empty.
    pub fn new(
        destination: &Destination,
        remote_destination: Destination,
        recv_stream_id: u32,
        syn_payload: Vec<u8>,
        signing_key: &SigningPrivateKey,
    ) -> (Self, Vec<u8>) {
        let send_stream_id = R::rng().next_u32();
        let packet = PacketBuilder::new(send_stream_id)
            .with_send_stream_id(recv_stream_id)
            .with_seq_nro(0)
            .with_from_included(destination)
            .with_synchronize()
            .with_signature()
            .build_and_sign(signing_key)
            .to_vec();

        (
            Self {
                destination_id: remote_destination.id(),
                remote_destination,
                established: R::now(),
                packets: match syn_payload.is_empty() {
                    true => VecDeque::new(),
                    false => VecDeque::from_iter([syn_payload]),
                },
                recv_stream_id,
                send_stream_id,
                seq_nro: 0u32,
            },
            packet,
        )
    }

    /// Handle `packet`.
    ///
    /// If the packet is valid and it requires an ACK, the function returns a serialized [`Packet`]
    /// with an ACK which must be sent to the remote peer. Duplicate ACKs do not require a response.
    ///
    /// If the packet has `CLOSE`/`RESET` flags set, inform caller that the stream has been
    /// destroyed by the remote and that it can be removed.
    ///
    /// If [`INITIAL_WINDOW_SIZE`] many packets have been received without the session owner
    /// registering a listener, reject the inbound stream by sending a packet with `RESET` flag set.
    /// [`StreamManager`] is also instructed to destroy the session after the packet has been sent.
    fn on_packet_inner(&mut self, packet: Vec<u8>) -> Result<Option<Vec<u8>>, StreamingError> {
        let Packet {
            send_stream_id,
            recv_stream_id,
            seq_nro,
            flags,
            payload,
            ..
        } = Packet::parse::<R>(&packet)?;

        tracing::trace!(
            target: LOG_TARGET,
            remote = %self.destination_id,
            ?send_stream_id,
            ?recv_stream_id,
            payload_len = ?payload.len(),
            "inbound message",
        );

        // destroy stream if remote wants to close it
        if flags.reset() || flags.close() {
            return Err(StreamingError::Closed);
        }

        // ignore empty and duplicate packets
        if payload.is_empty() || seq_nro <= self.seq_nro {
            return Ok(None);
        }

        // reset connection because a full window of data was received
        // but the connection was not accepted by the session owner
        if self.packets.len() == INITIAL_WINDOW_SIZE {
            return Err(StreamingError::ReceiveWindowFull);
        }

        // TODO: keep track of dropped packets

        self.packets.push_back(payload.to_vec());
        self.seq_nro = seq_nro;

        Ok(Some(
            PacketBuilder::new(self.send_stream_id)
                .with_send_stream_id(self.recv_stream_id)
                .with_ack_through(seq_nro)
                .build()
                .to_vec(),
        ))
    }

    /// Handle inbound `packet` for a pending stream.
    pub fn on_packet(&mut self, packet: Vec<u8>) -> PendingStreamResult {
        match self.on_packet_inner(packet) {
            Ok(None) => PendingStreamResult::DoNothing,
            Ok(Some(packet)) => PendingStreamResult::Send { packet },
            Err(StreamingError::ReceiveWindowFull) => PendingStreamResult::SendAndDestroy {
                packet: PacketBuilder::new(self.send_stream_id)
                    .with_send_stream_id(self.recv_stream_id)
                    .with_reset()
                    .build()
                    .to_vec(),
            },
            Err(StreamingError::Closed) => PendingStreamResult::Destroy,
            Err(StreamingError::Malformed(_)) => PendingStreamResult::DoNothing,
            Err(_) => unreachable!(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::{mock::MockRuntime, noop::NoopRuntime};

    #[test]
    fn ignore_duplicate_ack() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        let packet = PacketBuilder::new(stream.send_stream_id)
            .with_send_stream_id(stream.recv_stream_id)
            .with_seq_nro(0u32)
            .build()
            .to_vec();

        match stream.on_packet(packet) {
            PendingStreamResult::DoNothing => {}
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn destroy_stream_on_close() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        let packet = PacketBuilder::new(stream.send_stream_id)
            .with_send_stream_id(stream.recv_stream_id)
            .with_seq_nro(0u32)
            .with_close()
            .build()
            .to_vec();

        match stream.on_packet(packet) {
            PendingStreamResult::Destroy => {}
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn destroy_stream_on_reset() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        let packet = PacketBuilder::new(stream.send_stream_id)
            .with_send_stream_id(stream.recv_stream_id)
            .with_seq_nro(0u32)
            .with_reset()
            .build()
            .to_vec();

        match stream.on_packet(packet) {
            PendingStreamResult::Destroy => {}
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn buffer_data_correctly() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        for i in 1..=3 {
            let packet = PacketBuilder::new(stream.send_stream_id)
                .with_send_stream_id(stream.recv_stream_id)
                .with_seq_nro(i as u32)
                .with_payload(b"hello, world")
                .build()
                .to_vec();

            match stream.on_packet(packet) {
                PendingStreamResult::Send { packet } => {
                    let Packet { ack_through, .. } = Packet::parse::<MockRuntime>(&packet).unwrap();
                    assert_eq!(ack_through, i as u32);
                }
                _ => panic!("invalid result"),
            }
        }
        assert_eq!(stream.packets.len(), 3);

        for packet in &stream.packets {
            assert_eq!(packet, b"hello, world");
        }

        // send duplicate ack and verify it's ignored
        let packet = PacketBuilder::new(stream.send_stream_id)
            .with_send_stream_id(stream.recv_stream_id)
            .with_seq_nro(3u32)
            .build()
            .to_vec();

        match stream.on_packet(packet) {
            PendingStreamResult::DoNothing => {}
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn ignore_invalid_packets() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        match stream.on_packet(vec![1, 2, 3, 4]) {
            PendingStreamResult::DoNothing => {}
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn receive_window_full() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        for i in 1..=INITIAL_WINDOW_SIZE {
            let packet = PacketBuilder::new(stream.send_stream_id)
                .with_send_stream_id(stream.recv_stream_id)
                .with_seq_nro(i as u32)
                .with_payload(b"hello, world")
                .build()
                .to_vec();

            match stream.on_packet(packet) {
                PendingStreamResult::Send { packet } => {
                    let Packet { ack_through, .. } = Packet::parse::<MockRuntime>(&packet).unwrap();
                    assert_eq!(ack_through, i as u32);
                }
                _ => panic!("invalid result"),
            }
        }
        assert_eq!(stream.packets.len(), INITIAL_WINDOW_SIZE);

        for packet in &stream.packets {
            assert_eq!(packet, b"hello, world");
        }

        let packet = PacketBuilder::new(stream.send_stream_id)
            .with_send_stream_id(stream.recv_stream_id)
            .with_seq_nro(7 as u32)
            .with_payload(b"hello, world")
            .build()
            .to_vec();

        match stream.on_packet(packet) {
            PendingStreamResult::SendAndDestroy { packet } => {
                assert!(Packet::parse::<MockRuntime>(&packet).unwrap().flags.reset());
            }
            _ => panic!("invalid result"),
        }
    }

    #[test]
    fn syn_payload_not_empty() {
        let signing_key = SigningPrivateKey::from_bytes(&[0u8; 32]).unwrap();
        let (mut stream, _) = PendingStream::<NoopRuntime>::new(
            &Destination::new::<NoopRuntime>(signing_key.public()),
            Destination::random().0,
            1337u32,
            vec![1, 2, 3, 4],
            &SigningPrivateKey::random(NoopRuntime::rng()),
        );

        match stream.packets.pop_front() {
            Some(payload) => {
                assert_eq!(payload, vec![1, 2, 3, 4]);
            }
            _ => panic!("expected payload"),
        }
    }
}