Skip to main content

tor_linkspec/
verbatim.rs

1//! Wrappers for using a CircTarget with a verbatim list of
2//! [link specifiers](EncodedLinkSpec).
3
4use crate::{ChanTarget, CircTarget, EncodedLinkSpec, HasAddrs, HasChanMethod, HasRelayIds};
5
6/// A wrapper around an underlying [`CircTarget`] that provides a user-specified
7/// list of [link specifiers](EncodedLinkSpec).
8///
9/// Onion services and their clients use this type of target when telling a
10/// relay to extend a circuit to a target relay (an introduction point or
11/// rendezvous point) chosen by some other party.
12#[derive(Clone, Debug)]
13pub struct VerbatimLinkSpecCircTarget<T> {
14    /// The underlying CircTarget
15    target: T,
16    /// The link specifiers to provide.
17    linkspecs: Vec<EncodedLinkSpec>,
18}
19
20impl<T> VerbatimLinkSpecCircTarget<T> {
21    /// Construct a new `VerbatimLinkSpecCircTarget` to wrap an underlying
22    /// `CircTarget` object, and provide it with a new set of encoded link
23    /// specifiers that will be used when telling a relay to extend to this
24    /// node.
25    ///
26    /// Note that nothing here will check that `linkspecs` is sufficient to
27    /// actually connect to the chosen target, or to any target at all. It is
28    /// the caller's responsibility to choose a valid set of link specifiers.
29    pub fn new(target: T, linkspecs: Vec<EncodedLinkSpec>) -> Self {
30        Self { target, linkspecs }
31    }
32}
33
34// Now, the delegation functions.  All of these are simple delegations to
35// self.target, except for `CircTarget::linkspecs` with returns self.linkspecs.
36
37impl<T: HasRelayIds> HasRelayIds for VerbatimLinkSpecCircTarget<T> {
38    fn identity(&self, key_type: crate::RelayIdType) -> Option<crate::RelayIdRef<'_>> {
39        self.target.identity(key_type)
40    }
41}
42impl<T: HasAddrs> HasAddrs for VerbatimLinkSpecCircTarget<T> {
43    fn addrs(&self) -> impl Iterator<Item = std::net::SocketAddr> {
44        self.target.addrs()
45    }
46}
47impl<T: HasChanMethod> HasChanMethod for VerbatimLinkSpecCircTarget<T> {
48    fn chan_method(&self) -> crate::ChannelMethod {
49        self.target.chan_method()
50    }
51}
52impl<T: ChanTarget> ChanTarget for VerbatimLinkSpecCircTarget<T> {}
53impl<T: CircTarget> CircTarget for VerbatimLinkSpecCircTarget<T> {
54    fn linkspecs(&self) -> tor_bytes::EncodeResult<Vec<EncodedLinkSpec>> {
55        Ok(self.linkspecs.clone())
56    }
57
58    fn ntor_onion_key(&self) -> &tor_llcrypto::pk::curve25519::PublicKey {
59        self.target.ntor_onion_key()
60    }
61
62    fn protovers(&self) -> &tor_protover::Protocols {
63        self.target.protovers()
64    }
65}
66
67#[cfg(test)]
68mod test {
69    // @@ begin test lint list maintained by maint/add_warning @@
70    #![allow(clippy::bool_assert_comparison)]
71    #![allow(clippy::clone_on_copy)]
72    #![allow(clippy::dbg_macro)]
73    #![allow(clippy::mixed_attributes_style)]
74    #![allow(clippy::print_stderr)]
75    #![allow(clippy::print_stdout)]
76    #![allow(clippy::single_char_pattern)]
77    #![allow(clippy::unwrap_used)]
78    #![allow(clippy::unchecked_time_subtraction)]
79    #![allow(clippy::useless_vec)]
80    #![allow(clippy::needless_pass_by_value)]
81    #![allow(clippy::string_slice)] // See arti#2571
82    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
83
84    use super::*;
85    use crate::OwnedCircTarget;
86    use itertools::Itertools;
87
88    #[test]
89    fn verbatim_linkspecs() {
90        let mut builder = OwnedCircTarget::builder();
91        builder
92            .chan_target()
93            .addrs(vec!["127.0.0.1:11".parse().unwrap()])
94            .ed_identity([42; 32].into())
95            .rsa_identity([45; 20].into());
96        let inner = builder
97            .ntor_onion_key([99; 32].into())
98            .protocols("FlowCtrl=7".parse().unwrap())
99            .build()
100            .unwrap();
101        let weird_linkspecs = vec![EncodedLinkSpec::new(
102            77.into(),
103            b"mysterious whisper".to_vec(),
104        )];
105        let wrapped = VerbatimLinkSpecCircTarget::new(inner.clone(), weird_linkspecs.clone());
106
107        assert_eq!(wrapped.addrs().collect_vec(), inner.addrs().collect_vec());
108        assert!(wrapped.same_relay_ids(&inner));
109        assert_eq!(wrapped.ntor_onion_key(), inner.ntor_onion_key());
110        assert_eq!(wrapped.protovers(), inner.protovers());
111
112        assert_ne!(inner.linkspecs().unwrap(), weird_linkspecs);
113        assert_eq!(wrapped.linkspecs().unwrap(), weird_linkspecs);
114    }
115}