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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// 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.

//! Pending I2CP client session.
//!
//! IC2P client session is considered pending until the client has sent a `CreateSession` message
//! and a tunnel pool with at least one inbound tunnel has been built for the session.
//!
//! After these two conditions have been met, the pending session is destroyed and a new active
//! session is created from the pending context.

use crate::{
    crypto::StaticPrivateKey,
    i2cp::{
        message::{
            BandwidthLimits, Message, RequestVariableLeaseSet, SessionId, SessionStatus,
            SessionStatusKind, SetDate,
        },
        socket::I2cpSocket,
    },
    primitives::{Date, DestinationId, Lease, Mapping, Str, TunnelId},
    profile::ProfileStorage,
    runtime::{AddressBook, Runtime},
    tunnel::{TunnelManagerHandle, TunnelPoolConfig, TunnelPoolEvent, TunnelPoolHandle},
};

use bytes::Bytes;
use futures::{future::BoxFuture, FutureExt, StreamExt};
use hashbrown::{HashMap, HashSet};

use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec};
use core::{
    fmt,
    future::Future,
    mem,
    pin::Pin,
    str::FromStr,
    task::{Context, Poll},
};

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

/// I2CP client session context.
pub struct I2cpSessionContext<R: Runtime> {
    /// Address book.
    pub address_book: Option<Arc<dyn AddressBook>>,

    /// Destination ID.
    pub destination_id: DestinationId,

    /// Active inbound tunnels and their leases.
    pub inbound: HashMap<TunnelId, Lease>,

    /// Serialized [`LeaseSet2`].
    pub leaseset: Bytes,

    /// Session options.
    pub options: Mapping,

    /// Active outbound tunnels.
    pub outbound: HashSet<TunnelId>,

    /// Private keys of the destination.
    pub private_keys: Vec<StaticPrivateKey>,

    /// Profile storage.
    pub profile_storage: ProfileStorage<R>,

    /// Session ID.
    pub session_id: u16,

    /// I2CP socket.
    pub socket: I2cpSocket<R>,

    /// Tunnel pool handle.
    pub tunnel_pool_handle: TunnelPoolHandle,
}

/// State of the pending I2CP client session.
enum PendingSessionState<R: Runtime> {
    /// I2CP session doesn't have an active tunnel pool
    /// and is waiting for the client to send `CreateSession` message.
    Inactive {
        /// Session ID.
        session_id: u16,

        /// I2CP socket.
        socket: I2cpSocket<R>,
    },

    /// The tunnel pool itself is being built.
    BuildingPool {
        /// Session ID.
        session_id: u16,

        /// I2CP socket.
        socket: I2cpSocket<R>,

        /// Session options.
        options: Mapping,

        /// Tunnel pool build future.
        ///
        /// Resolves to a `TunnelPoolHandle` once the pool has been built.
        tunnel_pool_future: BoxFuture<'static, TunnelPoolHandle>,
    },

    /// Tunnels of the tunnel pool are being built.
    ///
    /// As soon as one inbound and one outbound tunnel has been built, the client is sent
    /// `RequestVariableLeaseSet` message which instructs it to create a [`LeaseSet2`] for the
    /// inbound tunnel(s) and send private key(s) of the `Destination`.
    BuildingTunnels {
        /// Session ID.
        session_id: u16,

        /// I2CP socket.
        socket: I2cpSocket<R>,

        /// Session options.
        options: Mapping,

        /// Handle to the built tunnel pool.
        handle: TunnelPoolHandle,

        /// Active inbound tunnels and their leases.
        inbound: HashMap<TunnelId, Lease>,

        /// Active outbound tunnels.
        outbound: HashSet<TunnelId>,
    },

    /// Awaiting to receive a lease set for client destination and an associated private key(s).
    ///
    /// After these are received, the pending I2CP session is destroyed and the `I2cpServer`
    /// is returned [`I2cpSessionContext`] which allows it to create an active I2CP session.
    AwaitingLeaseSet {
        /// Session ID.
        session_id: u16,

        /// I2CP socket.
        socket: I2cpSocket<R>,

        /// Session options.
        options: Mapping,

        /// Handle to the built tunnel pool.
        handle: TunnelPoolHandle,

        /// Active inbound tunnels and their leases.
        inbound: HashMap<TunnelId, Lease>,

        /// Active outbound tunnels.
        outbound: HashSet<TunnelId>,
    },

    /// Tunnel pool state is poisoned.
    Poisoned,
}

impl<R: Runtime> fmt::Debug for PendingSessionState<R> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Inactive { session_id, .. } => f
                .debug_struct("PendingSessionState::Inactive")
                .field("session_id", &session_id)
                .finish_non_exhaustive(),
            Self::BuildingPool { session_id, .. } => f
                .debug_struct("PendingSessionState::BuildingPool")
                .field("session_id", &session_id)
                .finish_non_exhaustive(),
            Self::BuildingTunnels { session_id, .. } => f
                .debug_struct("PendingSessionState::BuildingTunnels")
                .field("session_id", &session_id)
                .finish_non_exhaustive(),
            Self::AwaitingLeaseSet { session_id, .. } => f
                .debug_struct("PendingSessionState::AwaitingLeaseSet")
                .field("session_id", &session_id)
                .finish_non_exhaustive(),
            Self::Poisoned =>
                f.debug_struct("PendingSessionState::Poisoned").finish_non_exhaustive(),
        }
    }
}

impl<R: Runtime> PendingSessionState<R> {
    /// Get mutable reference to session's `I2cpSocket`.
    ///
    /// Panics if called after the session has been poisoned.
    fn socket(&mut self) -> &mut I2cpSocket<R> {
        match self {
            Self::Inactive { socket, .. } => socket,
            Self::BuildingPool { socket, .. } => socket,
            Self::BuildingTunnels { socket, .. } => socket,
            Self::AwaitingLeaseSet { socket, .. } => socket,
            Self::Poisoned => unreachable!(),
        }
    }

    /// Get session ID of the pending session.
    ///
    /// Panics if called after the session has been poisoned.
    fn session_id(&self) -> u16 {
        match self {
            Self::Inactive { session_id, .. } => *session_id,
            Self::BuildingPool { session_id, .. } => *session_id,
            Self::BuildingTunnels { session_id, .. } => *session_id,
            Self::AwaitingLeaseSet { session_id, .. } => *session_id,
            Self::Poisoned => unreachable!(),
        }
    }
}

/// Pending I2CP client session.
pub struct PendingI2cpSession<R: Runtime> {
    /// Address book.
    address_book: Option<Arc<dyn AddressBook>>,

    /// Profile storage.
    profile_storage: ProfileStorage<R>,

    /// State of the pending session.
    state: PendingSessionState<R>,

    /// Handle to `TunnelManager`.
    tunnel_manager_handle: TunnelManagerHandle,
}

impl<R: Runtime> PendingI2cpSession<R> {
    /// Create new [`I2cpSession`] from `stream`.
    pub fn new(
        session_id: u16,
        socket: I2cpSocket<R>,
        tunnel_manager_handle: TunnelManagerHandle,
        address_book: Option<Arc<dyn AddressBook>>,
        profile_storage: ProfileStorage<R>,
    ) -> Self {
        Self {
            address_book,
            profile_storage,
            state: PendingSessionState::Inactive { session_id, socket },
            tunnel_manager_handle,
        }
    }

    /// Handle I2CP message received from the client.
    fn on_message(&mut self, message: Message) -> Option<I2cpSessionContext<R>> {
        match message {
            Message::GetDate { version, options } => {
                tracing::trace!(
                    target: LOG_TARGET,
                    session_id = ?self.state.session_id(),
                    %version,
                    ?options,
                    "get date, send set date",
                );

                self.state.socket().send_message(SetDate::new(
                    Date::new(R::time_since_epoch().as_millis() as u64),
                    Str::from_str("0.9.63").expect("to succeed"),
                ));
            }
            Message::GetBandwidthLimits => {
                tracing::trace!(
                    target: LOG_TARGET,
                    session_id = ?self.state.session_id(),
                    "handle bandwidth limit request",
                );

                self.state.socket().send_message(BandwidthLimits::new());
            }
            Message::DestroySession { session_id } => {
                tracing::trace!(
                    target: LOG_TARGET,
                    session_id = ?self.state.session_id(),
                    destroyed_session_id = ?session_id,
                    "destroy session",
                );

                self.state
                    .socket()
                    .send_message(SessionStatus::new(session_id, SessionStatusKind::Destroyed));
            }
            Message::CreateSession {
                destination,
                date,
                mut options,
            } => match mem::replace(&mut self.state, PendingSessionState::Poisoned) {
                PendingSessionState::Inactive {
                    session_id,
                    mut socket,
                } => {
                    tracing::info!(
                        target: LOG_TARGET,
                        ?session_id,
                        destination = %destination.id(),
                        ?date,
                        num_options = ?options.len(),
                        "create session",
                    );

                    // create tunnel pool config
                    //
                    // emissary uses `inbound.nick` to name the tunnel pool config and in case it's
                    // not set check if `outbound.nick` is set and if so, use that for the tunnel
                    // pool's name
                    //
                    // if neither is set, use the destination short hash as the tunnel pool's name
                    let tunnel_pool_config = {
                        match options.get(&Str::from("inbound.nickname")) {
                            Some(_) => TunnelPoolConfig::from(&options),
                            None => {
                                let name =
                                    options.get(&Str::from("outbound.nickname")).map_or_else(
                                        || Str::from(destination.id().to_string()),
                                        |name| name.clone(),
                                    );
                                options.insert(Str::from("inbound.nickname"), name);

                                TunnelPoolConfig::from(&options)
                            }
                        }
                    };

                    // attempt to create tunnel pool for the session
                    //
                    // if channel towards `TunnelManager` is clogged, don't respond to the client,
                    // allowing them to retry `CreateSession` later when the channel has available
                    // capacity
                    //
                    // response from `TunnelManagerHandle::create_tunnel_pool()` is a future which
                    // the session must poll until a `TunnelPoolHandle` is received. Reception of
                    // the handle doesn't mean the tunnel is ready for use and the handle must be
                    // polled further until an inbound tunnel is built
                    match self.tunnel_manager_handle.create_tunnel_pool(tunnel_pool_config) {
                        Ok(future) => {
                            tracing::trace!(
                                target: LOG_TARGET,
                                ?session_id,
                                "tunnel pool build started",
                            );

                            socket.send_message(SessionStatus::new(
                                SessionId::Session(session_id),
                                SessionStatusKind::Created,
                            ));
                            self.state = PendingSessionState::BuildingPool {
                                session_id,
                                socket,
                                options,
                                tunnel_pool_future: Box::pin(future),
                            };
                        }
                        Err(error) => {
                            tracing::warn!(
                                target: LOG_TARGET,
                                ?session_id,
                                ?error,
                                "failed to build tunnel pool",
                            );

                            self.state = PendingSessionState::Inactive { session_id, socket };
                        }
                    }
                }
                state => {
                    tracing::warn!(
                        target: LOG_TARGET,
                        ?state,
                        "`CreateSession` received but tunnel pool is already pending",
                    );

                    self.state = state;
                }
            },
            Message::CreateLeaseSet2 {
                key,
                leaseset,
                private_keys,
                ..
            } => match mem::replace(&mut self.state, PendingSessionState::Poisoned) {
                PendingSessionState::AwaitingLeaseSet {
                    session_id,
                    socket,
                    options,
                    handle,
                    inbound,
                    outbound,
                } => {
                    // the lease set is returned to the active session constructor which publishes
                    // it to netdb
                    return Some(I2cpSessionContext {
                        address_book: self.address_book.clone(),
                        destination_id: DestinationId::from(key),
                        inbound,
                        leaseset,
                        options,
                        outbound,
                        private_keys,
                        profile_storage: self.profile_storage.clone(),
                        session_id,
                        socket,
                        tunnel_pool_handle: handle,
                    });
                }
                state => {
                    tracing::warn!(
                        target: LOG_TARGET,
                        ?state,
                        "`CreateLeaseSet2` received but not awaiting lease set",
                    );
                    debug_assert!(false);

                    self.state = state;
                }
            },
            _ => {}
        }

        None
    }
}

impl<R: Runtime> Future for PendingI2cpSession<R> {
    type Output = Option<I2cpSessionContext<R>>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        loop {
            match self.state.socket().poll_next_unpin(cx) {
                Poll::Pending => break,
                Poll::Ready(None) => return Poll::Ready(None),
                Poll::Ready(Some(message)) =>
                    if let Some(context) = self.on_message(message) {
                        return Poll::Ready(Some(context));
                    },
            }
        }

        loop {
            match mem::replace(&mut self.state, PendingSessionState::Poisoned) {
                state @ PendingSessionState::Inactive { .. } => {
                    self.state = state;
                    break;
                }
                PendingSessionState::BuildingPool {
                    session_id,
                    socket,
                    options,
                    mut tunnel_pool_future,
                } => match tunnel_pool_future.poll_unpin(cx) {
                    Poll::Ready(handle) => {
                        tracing::trace!(
                            target: LOG_TARGET,
                            ?session_id,
                            "tunnel pool for the session has been built",
                        );

                        self.state = PendingSessionState::BuildingTunnels {
                            session_id,
                            socket,
                            options,
                            handle,
                            inbound: HashMap::new(),
                            outbound: HashSet::new(),
                        };
                    }
                    Poll::Pending => {
                        self.state = PendingSessionState::BuildingPool {
                            session_id,
                            socket,
                            options,
                            tunnel_pool_future,
                        };
                        break;
                    }
                },
                PendingSessionState::BuildingTunnels {
                    session_id,
                    mut socket,
                    options,
                    mut handle,
                    mut inbound,
                    mut outbound,
                } => match handle.poll_next_unpin(cx) {
                    Poll::Pending => {
                        self.state = PendingSessionState::BuildingTunnels {
                            session_id,
                            socket,
                            options,
                            handle,
                            inbound,
                            outbound,
                        };
                        break;
                    }
                    Poll::Ready(None) => return Poll::Ready(None),
                    Poll::Ready(Some(TunnelPoolEvent::InboundTunnelBuilt { tunnel_id, lease })) => {
                        tracing::trace!(
                            target: LOG_TARGET,
                            ?session_id,
                            %tunnel_id,
                            "inbound tunnel built for pending session",
                        );
                        inbound.insert(tunnel_id, lease);

                        // wait until all tunnels have been built
                        if inbound.len() != handle.config().num_inbound
                            || outbound.len() != handle.config().num_outbound
                        {
                            self.state = PendingSessionState::BuildingTunnels {
                                session_id,
                                socket,
                                options,
                                handle,
                                inbound,
                                outbound,
                            };
                            continue;
                        }

                        tracing::trace!(
                            target: LOG_TARGET,
                            ?session_id,
                            "send leaseset request to client",
                        );

                        socket.send_message(RequestVariableLeaseSet::new(
                            session_id,
                            inbound.values().cloned().collect::<Vec<_>>(),
                        ));

                        self.state = PendingSessionState::AwaitingLeaseSet {
                            inbound,
                            options,
                            outbound,
                            session_id,
                            socket,
                            handle,
                        };
                    }
                    Poll::Ready(Some(TunnelPoolEvent::OutboundTunnelBuilt { tunnel_id })) => {
                        tracing::trace!(
                            target: LOG_TARGET,
                            ?session_id,
                            %tunnel_id,
                            "outbound tunnel built for pending session",
                        );
                        outbound.insert(tunnel_id);

                        // wait until all tunnels have been built
                        if inbound.len() != handle.config().num_inbound
                            || outbound.len() != handle.config().num_outbound
                        {
                            self.state = PendingSessionState::BuildingTunnels {
                                session_id,
                                socket,
                                options,
                                handle,
                                inbound,
                                outbound,
                            };
                            continue;
                        }

                        tracing::trace!(
                            target: LOG_TARGET,
                            ?session_id,
                            "send leaseset request to client",
                        );

                        socket.send_message(RequestVariableLeaseSet::new(
                            session_id,
                            inbound.values().cloned().collect::<Vec<_>>(),
                        ));

                        self.state = PendingSessionState::AwaitingLeaseSet {
                            inbound,
                            options,
                            outbound,
                            session_id,
                            socket,
                            handle,
                        };
                    }
                    Poll::Ready(Some(TunnelPoolEvent::InboundTunnelExpired { tunnel_id })) => {
                        tracing::warn!(
                            target: LOG_TARGET,
                            ?session_id,
                            %tunnel_id,
                            "inbound tunnel expired for pending session",
                        );
                        inbound.remove(&tunnel_id);

                        self.state = PendingSessionState::BuildingTunnels {
                            session_id,
                            socket,
                            options,
                            handle,
                            inbound,
                            outbound,
                        };
                    }
                    Poll::Ready(Some(TunnelPoolEvent::OutboundTunnelExpired { tunnel_id })) => {
                        tracing::warn!(
                            target: LOG_TARGET,
                            ?session_id,
                            %tunnel_id,
                            "outbound tunnel expired for pending session",
                        );
                        outbound.remove(&tunnel_id);

                        self.state = PendingSessionState::BuildingTunnels {
                            session_id,
                            socket,
                            options,
                            handle,
                            inbound,
                            outbound,
                        };
                    }
                    Poll::Ready(Some(event)) => {
                        tracing::warn!(
                            target: LOG_TARGET,
                            ?session_id,
                            ?event,
                            "unexpected event",
                        );

                        self.state = PendingSessionState::BuildingTunnels {
                            session_id,
                            socket,
                            options,
                            handle,
                            inbound,
                            outbound,
                        };
                    }
                },
                state @ PendingSessionState::AwaitingLeaseSet { .. } => {
                    self.state = state;
                    break;
                }
                PendingSessionState::Poisoned => {
                    tracing::warn!(
                        target: LOG_TARGET,
                        "pending i2cp session tunnel pool state is poisoned",
                    );
                    debug_assert!(false);

                    return Poll::Ready(None);
                }
            }
        }

        Poll::Pending
    }
}