blob: 138df263c7ba676be92ebd8ae7d7b65369957a86 (
plain)
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
|
//! Tracking for the path of a client circuit.
use std::sync::Mutex;
use tor_linkspec::OwnedChanTarget;
use crate::crypto::cell::HopNum;
/// A descriptor of a single hop in a circuit path.
//
// TODO HS: I think this will want to be a public type once we change the
// return type of Circuit::path().
#[derive(Debug, Clone)]
#[non_exhaustive]
pub(super) enum PathEntry {
/// A hop built through a known relay or a set of externally provided
/// linkspecs.
///
/// TODO hs: distinguish the two cases here?
Relay(OwnedChanTarget),
/// A hop built using
/// [`extend_virtual`](crate::circuit::ClientCirc::extend_virtual).
///
/// TODO hs: remember anything about what the virtual hop represents?
#[cfg(feature = "hs-common")]
Virtual,
}
/// Helper struct that shares information
#[derive(Debug, Default)]
pub(super) struct Path {
/// Information about the relays on this circuit.
///
/// We only store ChanTarget information here, because it doesn't matter
/// which ntor key we actually used with each hop.
hops: Mutex<Vec<PathEntry>>,
}
impl Path {
/// Return the number of hops in this path
pub(super) fn n_hops(&self) -> usize {
self.hops.lock().expect("poisoned lock").len()
}
/// Add a hop to this this path.
pub(super) fn push_hop(&self, target: PathEntry) {
self.hops.lock().expect("poisoned lock").push(target);
}
/// Return an OwnedChanTarget representing the first hop of this path.
pub(super) fn first_hop(&self) -> Option<PathEntry> {
self.hops
.lock()
.expect("poisoned lock")
.get(0)
.map(Clone::clone)
}
/// Return a copy of all the hops in this path.
pub(super) fn all_hops(&self) -> Vec<PathEntry> {
self.hops.lock().expect("poisoned lock").clone()
}
/// Return the index of the last hop on this path, or `None` if the path is
/// empty (or impossibly long).
pub(super) fn last_hop_num(&self) -> Option<HopNum> {
let n = self.n_hops();
let idx: u8 = n.checked_sub(1)?.try_into().ok()?;
Some(idx.into())
}
}
|