1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48

            
49
// TODO #1645 (either remove this, or decide to have it everywhere)
50
#![cfg_attr(not(all(feature = "full", feature = "experimental")), allow(unused))]
51

            
52
// Glossary:
53
//     Primary guard
54
//     Sample
55
//     confirmed
56
//     filtered
57

            
58
use derive_deftly::Deftly;
59
use futures::channel::mpsc;
60
use itertools::Either;
61
use serde::{Deserialize, Serialize};
62
use std::collections::HashMap;
63
use std::net::SocketAddr;
64
use std::sync::{Arc, Mutex, Weak};
65
use std::time::{Duration, Instant, SystemTime};
66
#[cfg(feature = "bridge-client")]
67
use tor_error::internal;
68
use tor_linkspec::{OwnedChanTarget, OwnedCircTarget, RelayId, RelayIdSet};
69
use tor_netdir::NetDirProvider;
70
use tor_proto::ClockSkew;
71
use tor_rtcompat::SpawnExt;
72
use tor_units::BoundedInt32;
73
use tracing::{debug, info, instrument, trace, warn};
74

            
75
use tor_config::derive::prelude::*;
76
use tor_config::{ExplicitOrAuto, impl_standard_builder};
77
use tor_config::{ReconfigureError, impl_not_auto_value};
78
use tor_config::{define_list_builder_accessors, define_list_builder_helper};
79
use tor_netdir::{NetDir, Relay, params::NetParameters};
80
use tor_persist::{DynStorageHandle, StateMgr};
81
use tor_rtcompat::Runtime;
82

            
83
#[cfg(feature = "bridge-client")]
84
pub mod bridge;
85
mod config;
86
mod daemon;
87
mod dirstatus;
88
mod err;
89
mod events;
90
pub mod fallback;
91
mod filter;
92
mod guard;
93
mod ids;
94
mod pending;
95
mod sample;
96
mod skew;
97
mod util;
98
#[cfg(feature = "vanguards")]
99
pub mod vanguards;
100

            
101
#[cfg(not(feature = "bridge-client"))]
102
#[path = "bridge_disabled.rs"]
103
pub mod bridge;
104

            
105
#[cfg(any(test, feature = "testing"))]
106
pub use config::testing::TestConfig;
107

            
108
#[cfg(test)]
109
use oneshot_fused_workaround as oneshot;
110

            
111
pub use config::GuardMgrConfig;
112
pub use err::{GuardMgrConfigError, GuardMgrError, PickGuardError};
113
pub use events::ClockSkewEvents;
114
pub use filter::GuardFilter;
115
pub use ids::FirstHopId;
116
pub use pending::{GuardMonitor, GuardStatus, GuardUsable};
117
pub use skew::SkewEstimate;
118

            
119
#[cfg(feature = "vanguards")]
120
pub use vanguards::VanguardMgrError;
121

            
122
use pending::{PendingRequest, RequestId};
123
use sample::{GuardSet, Universe, UniverseRef};
124

            
125
use crate::ids::{FirstHopIdInner, GuardId};
126

            
127
/// A "guard manager" that selects and remembers a persistent set of
128
/// guard nodes.
129
///
130
/// This is a "handle"; clones of it share state.
131
#[derive(Clone)]
132
pub struct GuardMgr<R: Runtime> {
133
    /// An asynchronous runtime object.
134
    ///
135
    /// GuardMgr uses this runtime for timing, timeouts, and spawning
136
    /// tasks.
137
    runtime: R,
138

            
139
    /// Internal state for the guard manager.
140
    inner: Arc<Mutex<GuardMgrInner>>,
141
}
142

            
143
/// Helper type that holds the data used by a [`GuardMgr`].
144
///
145
/// This would just be a [`GuardMgr`], except that it needs to sit inside
146
/// a `Mutex` and get accessed by daemon tasks.
147
struct GuardMgrInner {
148
    /// Last time when marked all of our primary guards as retriable.
149
    ///
150
    /// We keep track of this time so that we can rate-limit
151
    /// these attempts.
152
    last_primary_retry_time: Instant,
153

            
154
    /// Persistent guard manager state.
155
    ///
156
    /// This object remembers one or more persistent set of guards that we can
157
    /// use, along with their relative priorities and statuses.
158
    guards: GuardSets,
159

            
160
    /// The current filter that we're using to decide which guards are
161
    /// supported.
162
    //
163
    // TODO: This field is duplicated in the current active [`GuardSet`]; we
164
    // should fix that.
165
    filter: GuardFilter,
166

            
167
    /// Configuration values derived from the consensus parameters.
168
    ///
169
    /// This is updated whenever the consensus parameters change.
170
    params: GuardParams,
171

            
172
    /// A mpsc channel, used to tell the task running in
173
    /// [`daemon::report_status_events`] about a new event to monitor.
174
    ///
175
    /// This uses an `UnboundedSender` so that we don't have to await
176
    /// while sending the message, which in turn allows the GuardMgr
177
    /// API to be simpler.  The risk, however, is that there's no
178
    /// backpressure in the event that the task running
179
    /// [`daemon::report_status_events`] fails to read from this
180
    /// channel.
181
    ctrl: mpsc::UnboundedSender<daemon::Msg>,
182

            
183
    /// Information about guards that we've given out, but where we have
184
    /// not yet heard whether the guard was successful.
185
    ///
186
    /// Upon leaning whether the guard was successful, the pending
187
    /// requests in this map may be either moved to `waiting`, or
188
    /// discarded.
189
    ///
190
    /// There can be multiple pending requests corresponding to the
191
    /// same guard.
192
    pending: HashMap<RequestId, PendingRequest>,
193

            
194
    /// A list of pending requests for which we have heard that the
195
    /// guard was successful, but we have not yet decided whether the
196
    /// circuit may be used.
197
    ///
198
    /// There can be multiple waiting requests corresponding to the
199
    /// same guard.
200
    waiting: Vec<PendingRequest>,
201

            
202
    /// A list of fallback directories used to access the directory system
203
    /// when no other directory information is yet known.
204
    fallbacks: fallback::FallbackState,
205

            
206
    /// Location in which to store persistent state.
207
    storage: DynStorageHandle<GuardSets>,
208

            
209
    /// A sender object to publish changes in our estimated clock skew.
210
    send_skew: postage::watch::Sender<Option<SkewEstimate>>,
211

            
212
    /// A receiver object to hand out to observers who want to know about
213
    /// changes in our estimated clock skew.
214
    recv_skew: events::ClockSkewEvents,
215

            
216
    /// A netdir provider that we can use for adding new guards when
217
    /// insufficient guards are available.
218
    ///
219
    /// This has to be an Option so it can be initialized from None: at the
220
    /// time a GuardMgr is created, there is no NetDirProvider for it to use.
221
    netdir_provider: Option<Weak<dyn NetDirProvider>>,
222

            
223
    /// A netdir provider that we can use for discovering bridge descriptors.
224
    ///
225
    /// This has to be an Option so it can be initialized from None: at the time
226
    /// a GuardMgr is created, there is no BridgeDescProvider for it to use.
227
    #[cfg(feature = "bridge-client")]
228
    bridge_desc_provider: Option<Weak<dyn bridge::BridgeDescProvider>>,
229

            
230
    /// A list of the bridges that we are configured to use, or "None" if we are
231
    /// not configured to use bridges.
232
    #[cfg(feature = "bridge-client")]
233
    configured_bridges: Option<Arc<[bridge::BridgeConfig]>>,
234
}
235

            
236
/// A selector that tells us which [`GuardSet`] of several is currently in use.
237
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd, strum::EnumIter)]
238
enum GuardSetSelector {
239
    /// The default guard set is currently in use: that's the one that we use
240
    /// when we have no filter installed, or the filter permits most of the
241
    /// guards on the network.
242
    #[default]
243
    Default,
244
    /// A "restrictive" guard set is currently in use: that's the one that we
245
    /// use when we have a filter that excludes a large fraction of the guards
246
    /// on the network.
247
    Restricted,
248
    /// The "bridges" guard set is currently in use: we are selecting our guards
249
    /// from among the universe of configured bridges.
250
    #[cfg(feature = "bridge-client")]
251
    Bridges,
252
}
253

            
254
/// Describes the [`Universe`] that a guard sample should take its guards from.
255
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
256
enum UniverseType {
257
    /// Take information from the network directory.
258
    NetDir,
259
    /// Take information from the configured bridges.
260
    #[cfg(feature = "bridge-client")]
261
    BridgeSet,
262
}
263

            
264
impl GuardSetSelector {
265
    /// Return a description of which [`Universe`] this guard sample should take
266
    /// its guards from.
267
1165364
    fn universe_type(&self) -> UniverseType {
268
1165364
        match self {
269
1165364
            GuardSetSelector::Default | GuardSetSelector::Restricted => UniverseType::NetDir,
270
            #[cfg(feature = "bridge-client")]
271
            GuardSetSelector::Bridges => UniverseType::BridgeSet,
272
        }
273
1165364
    }
274
}
275

            
276
/// Persistent state for a guard manager, as serialized to disk.
277
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
278
struct GuardSets {
279
    /// Which set of guards is currently in use?
280
    #[serde(skip)]
281
    active_set: GuardSetSelector,
282

            
283
    /// The default set of guards to use.
284
    ///
285
    /// We use this one when there is no filter, or the filter permits most of the
286
    /// guards on the network.
287
    default: GuardSet,
288

            
289
    /// A guard set to use when we have a restrictive filter.
290
    #[serde(default)]
291
    restricted: GuardSet,
292

            
293
    /// A guard set sampled from our configured bridges.
294
    #[serde(default)]
295
    #[cfg(feature = "bridge-client")]
296
    bridges: GuardSet,
297

            
298
    /// Unrecognized fields, including (possibly) other guard sets.
299
    #[serde(flatten)]
300
    remaining: HashMap<String, tor_persist::JsonValue>,
301
}
302

            
303
/// The key (filename) we use for storing our persistent guard state in the
304
/// `StateMgr`.
305
///
306
/// We used to store this in a different format in a filename called
307
/// "default_guards" (before Arti 0.1.0).
308
const STORAGE_KEY: &str = "guards";
309

            
310
/// A description of which circuits to retire because of a configuration change.
311
///
312
/// TODO(nickm): Eventually we will want to add a "Some" here, to support
313
/// removing only those circuits that correspond to no-longer-usable guards.
314
#[derive(Clone, Debug, Eq, PartialEq)]
315
#[must_use]
316
#[non_exhaustive]
317
pub enum RetireCircuits {
318
    /// There's no need to retire any circuits.
319
    None,
320
    /// All circuits should be retired.
321
    All,
322
}
323

            
324
impl<R: Runtime> GuardMgr<R> {
325
    /// Create a new "empty" guard manager and launch its background tasks.
326
    ///
327
    /// It won't be able to hand out any guards until a [`NetDirProvider`] has
328
    /// been installed.
329
    #[instrument(skip_all, level = "trace")]
330
702
    pub fn new<S>(
331
702
        runtime: R,
332
702
        state_mgr: S,
333
702
        config: &impl GuardMgrConfig,
334
702
    ) -> Result<Self, GuardMgrError>
335
702
    where
336
702
        S: StateMgr + Send + Sync + 'static,
337
    {
338
702
        let (ctrl, rcv) = mpsc::unbounded();
339
702
        let storage: DynStorageHandle<GuardSets> = state_mgr.create_handle(STORAGE_KEY);
340
        // TODO(nickm): We should do something about the old state in
341
        // `default_guards`.  Probably it would be best to delete it.  We could
342
        // try to migrate it instead, but that's beyond the stability guarantee
343
        // that we're getting at this stage of our (pre-0.1) development.
344
702
        let state = storage.load()?.unwrap_or_default();
345

            
346
702
        let (send_skew, recv_skew) = postage::watch::channel();
347
702
        let recv_skew = ClockSkewEvents { inner: recv_skew };
348

            
349
702
        let inner = Arc::new(Mutex::new(GuardMgrInner {
350
702
            guards: state,
351
702
            filter: GuardFilter::unfiltered(),
352
702
            last_primary_retry_time: runtime.now(),
353
702
            params: GuardParams::default(),
354
702
            ctrl,
355
702
            pending: HashMap::new(),
356
702
            waiting: Vec::new(),
357
702
            fallbacks: config.fallbacks().into(),
358
702
            storage,
359
702
            send_skew,
360
702
            recv_skew,
361
702
            netdir_provider: None,
362
702
            #[cfg(feature = "bridge-client")]
363
702
            bridge_desc_provider: None,
364
702
            #[cfg(feature = "bridge-client")]
365
702
            configured_bridges: None,
366
702
        }));
367
        #[cfg(feature = "bridge-client")]
368
        {
369
702
            let mut inner = inner.lock().expect("lock poisoned");
370
            // TODO(nickm): This calls `GuardMgrInner::update`. Will we mind doing so before any
371
            // providers are configured? I think not, but we should make sure.
372
702
            let _: RetireCircuits =
373
702
                inner.replace_bridge_config(config, runtime.wallclock(), runtime.now())?;
374
        }
375
        {
376
702
            let weak_inner = Arc::downgrade(&inner);
377
702
            let rt_clone = runtime.clone();
378
702
            runtime
379
702
                .spawn(daemon::report_status_events(rt_clone, weak_inner, rcv))
380
702
                .map_err(|e| GuardMgrError::from_spawn("guard status event reporter", e))?;
381
        }
382
        {
383
702
            let rt_clone = runtime.clone();
384
702
            let weak_inner = Arc::downgrade(&inner);
385
702
            runtime
386
702
                .spawn(daemon::run_periodic(rt_clone, weak_inner))
387
702
                .map_err(|e| GuardMgrError::from_spawn("periodic guard updater", e))?;
388
        }
389
702
        Ok(GuardMgr { runtime, inner })
390
702
    }
391

            
392
    /// Install a [`NetDirProvider`] for use by this guard manager.
393
    ///
394
    /// It will be used to keep the guards up-to-date with changes from the
395
    /// network directory, and to find new guards when no NetDir is provided to
396
    /// select_guard().
397
    ///
398
    /// TODO: we should eventually return some kind of a task handle from this
399
    /// task, even though it is not strictly speaking periodic.
400
    ///
401
    /// The guardmgr retains only a `Weak` reference to `provider`,
402
    /// `install_netdir_provider` downgrades it on entry,
403
    // TODO add ref to document when https://gitlab.torproject.org/tpo/core/arti/-/issues/624
404
    // is fixed.  Also, maybe take an owned `Weak` to start with.
405
    //
406
    /// # Panics
407
    ///
408
    /// Panics if a [`NetDirProvider`] is already installed.
409
630
    pub fn install_netdir_provider(
410
630
        &self,
411
630
        provider: &Arc<dyn NetDirProvider>,
412
630
    ) -> Result<(), GuardMgrError> {
413
630
        let weak_provider = Arc::downgrade(provider);
414
        {
415
630
            let mut inner = self.inner.lock().expect("Poisoned lock");
416
630
            assert!(inner.netdir_provider.is_none());
417
630
            inner.netdir_provider = Some(weak_provider.clone());
418
        }
419
630
        let weak_inner = Arc::downgrade(&self.inner);
420
630
        let rt_clone = self.runtime.clone();
421
630
        self.runtime
422
630
            .spawn(daemon::keep_netdir_updated(
423
630
                rt_clone,
424
630
                weak_inner,
425
630
                weak_provider,
426
            ))
427
630
            .map_err(|e| GuardMgrError::from_spawn("periodic guard netdir updater", e))?;
428
630
        Ok(())
429
630
    }
430

            
431
    /// Configure a new [`bridge::BridgeDescProvider`] for this [`GuardMgr`].
432
    ///
433
    /// It will be used to learn about changes in the set of available bridge
434
    /// descriptors; we'll inform it whenever our desired set of bridge
435
    /// descriptors changes.
436
    ///
437
    /// TODO: Same todo as in `install_netdir_provider` about task handles.
438
    ///
439
    /// # Panics
440
    ///
441
    /// Panics if a [`bridge::BridgeDescProvider`] is already installed.
442
    #[cfg(feature = "bridge-client")]
443
    pub fn install_bridge_desc_provider(
444
        &self,
445
        provider: &Arc<dyn bridge::BridgeDescProvider>,
446
    ) -> Result<(), GuardMgrError> {
447
        let weak_provider = Arc::downgrade(provider);
448
        {
449
            let mut inner = self.inner.lock().expect("Poisoned lock");
450
            assert!(inner.bridge_desc_provider.is_none());
451
            inner.bridge_desc_provider = Some(weak_provider.clone());
452
        }
453

            
454
        let weak_inner = Arc::downgrade(&self.inner);
455
        let rt_clone = self.runtime.clone();
456
        self.runtime
457
            .spawn(daemon::keep_bridge_descs_updated(
458
                rt_clone,
459
                weak_inner,
460
                weak_provider,
461
            ))
462
            .map_err(|e| GuardMgrError::from_spawn("periodic guard netdir updater", e))?;
463

            
464
        Ok(())
465
    }
466

            
467
    /// Flush our current guard state to the state manager, if there
468
    /// is any unsaved state.
469
46
    pub fn store_persistent_state(&self) -> Result<(), GuardMgrError> {
470
46
        let inner = self.inner.lock().expect("Poisoned lock");
471
46
        trace!("Flushing guard state to disk.");
472
46
        inner.storage.store(&inner.guards)?;
473
46
        Ok(())
474
46
    }
475

            
476
    /// Reload state from the state manager.
477
    ///
478
    /// We only call this method if we _don't_ have the lock on the state
479
    /// files.  If we have the lock, we only want to save.
480
    #[instrument(level = "trace", skip_all)]
481
    pub fn reload_persistent_state(&self) -> Result<(), GuardMgrError> {
482
        let mut inner = self.inner.lock().expect("Poisoned lock");
483
        if let Some(new_guards) = inner.storage.load()? {
484
            inner.replace_guards_with(new_guards, self.runtime.wallclock(), self.runtime.now());
485
        }
486
        Ok(())
487
    }
488

            
489
    /// Switch from having an unowned persistent state to having an owned one.
490
    ///
491
    /// Requires that we hold the lock on the state files.
492
    #[instrument(level = "trace", skip_all)]
493
    pub fn upgrade_to_owned_persistent_state(&self) -> Result<(), GuardMgrError> {
494
        let mut inner = self.inner.lock().expect("Poisoned lock");
495
        debug_assert!(inner.storage.can_store());
496
        let new_guards = inner.storage.load()?.unwrap_or_default();
497
        let wallclock = self.runtime.wallclock();
498
        let now = self.runtime.now();
499
        inner.replace_guards_with(new_guards, wallclock, now);
500
        Ok(())
501
    }
502

            
503
    /// Return true if `netdir` has enough information to safely become our new netdir.
504
    pub fn netdir_is_sufficient(&self, netdir: &NetDir) -> bool {
505
        let mut inner = self.inner.lock().expect("Poisoned lock");
506
        if inner.guards.active_set.universe_type() != UniverseType::NetDir {
507
            // If we aren't using the netdir, this isn't something we want to look at.
508
            return true;
509
        }
510
        inner
511
            .guards
512
            .active_guards_mut()
513
            .n_primary_without_id_info_in(netdir)
514
            == 0
515
    }
516

            
517
    /// Mark every guard as potentially retriable, regardless of how recently we
518
    /// failed to connect to it.
519
    pub fn mark_all_guards_retriable(&self) {
520
        let mut inner = self.inner.lock().expect("Poisoned lock");
521
        inner.guards.active_guards_mut().mark_all_guards_retriable();
522
    }
523

            
524
    /// Configure this guardmgr to use a fixed [`NetDir`] instead of a provider.
525
    ///
526
    /// This function is for testing only, and is exclusive with
527
    /// `install_netdir_provider`.
528
    ///
529
    /// # Panics
530
    ///
531
    /// Panics if any [`NetDirProvider`] has already been installed.
532
    #[cfg(any(test, feature = "testing"))]
533
144
    pub fn install_test_netdir(&self, netdir: &NetDir) {
534
        use tor_netdir::testprovider::TestNetDirProvider;
535
144
        let wallclock = self.runtime.wallclock();
536
144
        let now = self.runtime.now();
537
144
        let netdir_provider: Arc<dyn NetDirProvider> =
538
144
            Arc::new(TestNetDirProvider::from(netdir.clone()));
539
144
        self.install_netdir_provider(&netdir_provider)
540
144
            .expect("Couldn't install testing network provider");
541

            
542
144
        let mut inner = self.inner.lock().expect("Poisoned lock");
543
144
        inner.update(wallclock, now);
544
144
    }
545

            
546
    /// Replace the configuration in this `GuardMgr` with `config`.
547
    #[instrument(level = "trace", skip_all)]
548
4
    pub fn reconfigure(
549
4
        &self,
550
4
        config: &impl GuardMgrConfig,
551
4
    ) -> Result<RetireCircuits, ReconfigureError> {
552
4
        let mut inner = self.inner.lock().expect("Poisoned lock");
553
        // Change the set of configured fallbacks.
554
4
        {
555
4
            let mut fallbacks: fallback::FallbackState = config.fallbacks().into();
556
4
            std::mem::swap(&mut inner.fallbacks, &mut fallbacks);
557
4
            inner.fallbacks.take_status_from(fallbacks);
558
4
        }
559
        // If we are built to use bridges, change the bridge configuration.
560
        #[cfg(feature = "bridge-client")]
561
        {
562
4
            let wallclock = self.runtime.wallclock();
563
4
            let now = self.runtime.now();
564
4
            Ok(inner.replace_bridge_config(config, wallclock, now)?)
565
        }
566
        // If we are built to use bridges, change the bridge configuration.
567
        #[cfg(not(feature = "bridge-client"))]
568
        {
569
            Ok(RetireCircuits::None)
570
        }
571
4
    }
572

            
573
    /// Replace the current [`GuardFilter`] used by this `GuardMgr`.
574
    // TODO should this be part of the config?
575
58
    pub fn set_filter(&self, filter: GuardFilter) {
576
58
        let wallclock = self.runtime.wallclock();
577
58
        let now = self.runtime.now();
578
58
        let mut inner = self.inner.lock().expect("Poisoned lock");
579
58
        inner.set_filter(filter, wallclock, now);
580
58
    }
581

            
582
    /// Select a guard for a given [`GuardUsage`].
583
    ///
584
    /// On success, we return a [`FirstHop`] object to identify which
585
    /// guard we have picked, a [`GuardMonitor`] object that the
586
    /// caller can use to report whether its attempt to use the guard
587
    /// succeeded or failed, and a [`GuardUsable`] future that the
588
    /// caller can use to decide whether a circuit built through the
589
    /// guard is actually safe to use.
590
    ///
591
    /// That last point is important: It's okay to build a circuit
592
    /// through the guard returned by this function, but you can't
593
    /// actually use it for traffic unless the [`GuardUsable`] future
594
    /// yields "true".
595
    #[instrument(skip_all, level = "trace")]
596
25396
    pub fn select_guard(
597
25396
        &self,
598
25396
        usage: GuardUsage,
599
25396
    ) -> Result<(FirstHop, GuardMonitor, GuardUsable), PickGuardError> {
600
25396
        let now = self.runtime.now();
601
25396
        let wallclock = self.runtime.wallclock();
602

            
603
25396
        let mut inner = self.inner.lock().expect("Poisoned lock");
604

            
605
        // (I am not 100% sure that we need to consider_all_retries here, but
606
        // it should _probably_ not hurt.)
607
25396
        inner.guards.active_guards_mut().consider_all_retries(now);
608

            
609
25396
        let (origin, guard) = inner.select_guard_with_expand(&usage, now, wallclock)?;
610
25396
        trace!(?guard, ?usage, "Guard selected");
611

            
612
25396
        let (usable, usable_sender) = if origin.usable_immediately() {
613
25372
            (GuardUsable::new_usable_immediately(), None)
614
        } else {
615
24
            let (u, snd) = GuardUsable::new_uncertain();
616
24
            (u, Some(snd))
617
        };
618
25396
        let request_id = pending::RequestId::next();
619
25396
        let ctrl = inner.ctrl.clone();
620
25396
        let monitor = GuardMonitor::new(request_id, ctrl);
621

            
622
        // Note that the network can be down even if all the primary guards
623
        // are not yet marked as unreachable.  But according to guard-spec we
624
        // don't want to acknowledge the net as down before that point, since
625
        // we don't mark all the primary guards as retriable unless
626
        // we've been forced to non-primary guards.
627
25396
        let net_has_been_down =
628
25396
            if let Some(duration) = tor_proto::time_since_last_incoming_traffic() {
629
                inner
630
                    .guards
631
                    .active_guards_mut()
632
                    .all_primary_guards_are_unreachable()
633
                    && duration >= inner.params.internet_down_timeout
634
            } else {
635
                // TODO: Is this the correct behavior in this case?
636
25396
                false
637
            };
638

            
639
25396
        let pending_request = pending::PendingRequest::new(
640
25396
            guard.first_hop_id(),
641
25396
            usage,
642
25396
            usable_sender,
643
25396
            net_has_been_down,
644
        );
645
25396
        inner.pending.insert(request_id, pending_request);
646

            
647
25396
        match &guard.sample {
648
25396
            Some(sample) => {
649
25396
                let guard_id = GuardId::from_relay_ids(&guard);
650
25396
                inner
651
25396
                    .guards
652
25396
                    .guards_mut(sample)
653
25396
                    .record_attempt(&guard_id, now);
654
25396
            }
655
            None => {
656
                // We don't record attempts for fallbacks; we only care when
657
                // they have failed.
658
            }
659
        }
660

            
661
25396
        Ok((guard, monitor, usable))
662
25396
    }
663

            
664
    /// Record that _after_ we built a circuit with a guard, something described
665
    /// in `external_failure` went wrong with it.
666
12
    pub fn note_external_failure<T>(&self, identity: &T, external_failure: ExternalActivity)
667
12
    where
668
12
        T: tor_linkspec::HasRelayIds + ?Sized,
669
    {
670
12
        let now = self.runtime.now();
671
12
        let mut inner = self.inner.lock().expect("Poisoned lock");
672
12
        let ids = inner.lookup_ids(identity);
673
24
        for id in ids {
674
12
            match &id.0 {
675
12
                FirstHopIdInner::Guard(sample, id) => {
676
12
                    inner
677
12
                        .guards
678
12
                        .guards_mut(sample)
679
12
                        .record_failure(id, Some(external_failure), now);
680
12
                }
681
                FirstHopIdInner::Fallback(id) => {
682
                    if external_failure == ExternalActivity::DirCache {
683
                        inner.fallbacks.note_failure(id, now);
684
                    }
685
                }
686
            }
687
        }
688
12
    }
689

            
690
    /// Record that _after_ we built a circuit with a guard, some activity
691
    /// described in `external_activity` was successful with it.
692
12
    pub fn note_external_success<T>(&self, identity: &T, external_activity: ExternalActivity)
693
12
    where
694
12
        T: tor_linkspec::HasRelayIds + ?Sized,
695
    {
696
12
        let mut inner = self.inner.lock().expect("Poisoned lock");
697

            
698
12
        inner.record_external_success(identity, external_activity, self.runtime.wallclock());
699
12
    }
700

            
701
    /// Return a stream of events about our estimated clock skew; these events
702
    /// are `None` when we don't have enough information to make an estimate,
703
    /// and `Some(`[`SkewEstimate`]`)` otherwise.
704
    ///
705
    /// Note that this stream can be lossy: if the estimate changes more than
706
    /// one before you read from the stream, you might only get the most recent
707
    /// update.
708
22
    pub fn skew_events(&self) -> ClockSkewEvents {
709
22
        let inner = self.inner.lock().expect("Poisoned lock");
710
22
        inner.recv_skew.clone()
711
22
    }
712

            
713
    /// Ensure that the message queue is flushed before proceeding to
714
    /// the next step.  Used for testing.
715
    #[cfg(test)]
716
72
    async fn flush_msg_queue(&self) {
717
72
        let (snd, rcv) = oneshot::channel();
718
72
        let pingmsg = daemon::Msg::Ping(snd);
719
72
        {
720
72
            let inner = self.inner.lock().expect("Poisoned lock");
721
72
            inner
722
72
                .ctrl
723
72
                .unbounded_send(pingmsg)
724
72
                .expect("Guard observer task exited prematurely.");
725
72
        }
726
72
        let _ = rcv.await;
727
72
    }
728
}
729

            
730
/// An activity that can succeed or fail, and whose success or failure can be
731
/// attributed to a guard.
732
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
733
#[non_exhaustive]
734
pub enum ExternalActivity {
735
    /// The activity of using the guard as a directory cache.
736
    DirCache,
737
}
738

            
739
impl GuardSets {
740
    /// Return a reference to the currently active set of guards.
741
    ///
742
    /// (That's easy enough for now, since there is never more than one set of
743
    /// guards.  But eventually that will change, as we add support for more
744
    /// complex filter types, and for bridge relays. Those will use separate
745
    /// `GuardSet` instances, and this accessor will choose the right one.)
746
9976
    fn active_guards(&self) -> &GuardSet {
747
9976
        self.guards(&self.active_set)
748
9976
    }
749

            
750
    /// Return the set of guards corresponding to the provided selector.
751
586364
    fn guards(&self, selector: &GuardSetSelector) -> &GuardSet {
752
586364
        match selector {
753
586292
            GuardSetSelector::Default => &self.default,
754
48
            GuardSetSelector::Restricted => &self.restricted,
755
            #[cfg(feature = "bridge-client")]
756
24
            GuardSetSelector::Bridges => &self.bridges,
757
        }
758
586364
    }
759

            
760
    /// Return a mutable reference to the currently active set of guards.
761
940250
    fn active_guards_mut(&mut self) -> &mut GuardSet {
762
940250
        self.guards_mut(&self.active_set.clone())
763
940250
    }
764

            
765
    /// Return a mutable reference to the set of guards corresponding to the
766
    /// provided selector.
767
1859392
    fn guards_mut(&mut self, selector: &GuardSetSelector) -> &mut GuardSet {
768
1859392
        match selector {
769
1859336
            GuardSetSelector::Default => &mut self.default,
770
56
            GuardSetSelector::Restricted => &mut self.restricted,
771
            #[cfg(feature = "bridge-client")]
772
            GuardSetSelector::Bridges => &mut self.bridges,
773
        }
774
1859392
    }
775

            
776
    /// Update all non-persistent state for the guards in this object with the
777
    /// state in `other`.
778
    fn copy_status_from(&mut self, mut other: GuardSets) {
779
        use strum::IntoEnumIterator;
780
        for sample in GuardSetSelector::iter() {
781
            self.guards_mut(&sample)
782
                .copy_ephemeral_status_into_newly_loaded_state(std::mem::take(
783
                    other.guards_mut(&sample),
784
                ));
785
        }
786
        self.active_set = other.active_set;
787
    }
788
}
789

            
790
impl GuardMgrInner {
791
    /// Look up the latest [`NetDir`] (if there is one) from our
792
    /// [`NetDirProvider`] (if we have one).
793
30072
    fn timely_netdir(&self) -> Option<Arc<NetDir>> {
794
30072
        self.netdir_provider
795
30072
            .as_ref()
796
30072
            .and_then(Weak::upgrade)
797
30454
            .and_then(|np| np.timely_netdir().ok())
798
30072
    }
799

            
800
    /// Look up the latest [`BridgeDescList`](bridge::BridgeDescList) (if there
801
    /// is one) from our [`BridgeDescProvider`](bridge::BridgeDescProvider) (if
802
    /// we have one).
803
    #[cfg(feature = "bridge-client")]
804
    fn latest_bridge_desc_list(&self) -> Option<Arc<bridge::BridgeDescList>> {
805
        self.bridge_desc_provider
806
            .as_ref()
807
            .and_then(Weak::upgrade)
808
            .map(|bp| bp.bridges())
809
    }
810

            
811
    /// Run a function that takes `&mut self` and an optional NetDir.
812
    ///
813
    /// We try to use the netdir from our [`NetDirProvider`] (if we have one).
814
    /// Therefore, although its _parameters_ are suitable for every
815
    /// [`GuardSet`], its _contents_ might not be. For those, call
816
    /// [`with_opt_universe`](Self::with_opt_universe) instead.
817
    //
818
    // This function exists to handle the lifetime mess where sometimes the
819
    // resulting NetDir will borrow from `netdir`, and sometimes it will borrow
820
    // from an Arc returned by `self.latest_netdir()`.
821
9976
    fn with_opt_netdir<F, T>(&mut self, func: F) -> T
822
9976
    where
823
9976
        F: FnOnce(&mut Self, Option<&NetDir>) -> T,
824
    {
825
9976
        if let Some(nd) = self.timely_netdir() {
826
1908
            func(self, Some(nd.as_ref()))
827
        } else {
828
8068
            func(self, None)
829
        }
830
9976
    }
831

            
832
    /// Return the latest `BridgeSet` based on our `BridgeDescProvider` and our
833
    /// configured bridges.
834
    ///
835
    /// Returns `None` if we are not configured to use bridges.
836
    #[cfg(feature = "bridge-client")]
837
    fn latest_bridge_set(&self) -> Option<bridge::BridgeSet> {
838
        let bridge_config = self.configured_bridges.as_ref()?.clone();
839
        let bridge_descs = self.latest_bridge_desc_list();
840
        Some(bridge::BridgeSet::new(bridge_config, bridge_descs))
841
    }
842

            
843
    /// Run a function that takes `&mut self` and an optional [`UniverseRef`].
844
    ///
845
    /// We try to get a universe from the appropriate source for the current
846
    /// active guard set.
847
20096
    fn with_opt_universe<F, T>(&mut self, func: F) -> T
848
20096
    where
849
20096
        F: FnOnce(&mut Self, Option<&UniverseRef>) -> T,
850
    {
851
        // TODO: it might be nice to make `func` take an GuardSet and a set of
852
        // parameters, so we can't get the active set wrong. Doing that will
853
        // require a fair amount of refactoring so that the borrow checker is
854
        // happy, however.
855
20096
        match self.guards.active_set.universe_type() {
856
            UniverseType::NetDir => {
857
20096
                if let Some(nd) = self.timely_netdir() {
858
12028
                    func(self, Some(&UniverseRef::NetDir(nd)))
859
                } else {
860
8068
                    func(self, None)
861
                }
862
            }
863
            #[cfg(feature = "bridge-client")]
864
            UniverseType::BridgeSet => func(
865
                self,
866
                self.latest_bridge_set()
867
                    .map(UniverseRef::BridgeSet)
868
                    .as_ref(),
869
            ),
870
        }
871
20096
    }
872

            
873
    /// Update the status of all guards in the active set, based on the passage
874
    /// of time, our configuration, and the relevant Universe for our active
875
    /// set.
876
    #[instrument(skip_all, level = "trace")]
877
9976
    fn update(&mut self, wallclock: SystemTime, now: Instant) {
878
10260
        self.with_opt_netdir(|this, netdir| {
879
            // Here we update our parameters from the latest NetDir, and check
880
            // whether we need to change to a (non)-restrictive GuardSet based
881
            // on those parameters and our configured filter.
882
            //
883
            // This uses a NetDir unconditionally, since we always want to take
884
            // the network parameters our parameters from the consensus even if
885
            // the guards themselves are from a BridgeSet.
886
9976
            this.update_active_set_params_and_filter(netdir);
887
9976
        });
888
10260
        self.with_opt_universe(|this, univ| {
889
            // Now we update the set of guards themselves based on the
890
            // Universe, which is either the latest NetDir, or the latest
891
            // BridgeSet—depending on what the GuardSet wants.
892
9976
            Self::update_guardset_internal(
893
9976
                &this.params,
894
9976
                wallclock,
895
9976
                this.guards.active_set.universe_type(),
896
9976
                this.guards.active_guards_mut(),
897
9976
                univ,
898
            );
899
            #[cfg(feature = "bridge-client")]
900
9976
            this.update_desired_descriptors(now);
901
            #[cfg(not(feature = "bridge-client"))]
902
            let _ = now;
903
9976
        });
904
9976
    }
905

            
906
    /// Replace our bridge configuration with the one from `new_config`.
907
    #[cfg(feature = "bridge-client")]
908
    #[instrument(level = "trace", skip_all)]
909
706
    fn replace_bridge_config(
910
706
        &mut self,
911
706
        new_config: &impl GuardMgrConfig,
912
706
        wallclock: SystemTime,
913
706
        now: Instant,
914
706
    ) -> Result<RetireCircuits, GuardMgrConfigError> {
915
706
        match (&self.configured_bridges, new_config.bridges_enabled()) {
916
            (None, false) => {
917
706
                assert_ne!(
918
706
                    self.guards.active_set.universe_type(),
919
                    UniverseType::BridgeSet
920
                );
921
706
                return Ok(RetireCircuits::None); // nothing to do
922
            }
923
            (_, true) if !self.storage.can_store() => {
924
                // TODO: Ideally we would try to upgrade, obtaining an exclusive lock,
925
                // but `StorageHandle` currently lacks a method for that.
926
                return Err(GuardMgrConfigError::NoLock("bridges configured".into()));
927
            }
928
            (Some(current_bridges), true) if new_config.bridges() == current_bridges.as_ref() => {
929
                assert_eq!(
930
                    self.guards.active_set.universe_type(),
931
                    UniverseType::BridgeSet
932
                );
933
                return Ok(RetireCircuits::None); // nothing to do.
934
            }
935
            (_, true) => {
936
                self.configured_bridges = Some(new_config.bridges().into());
937
                self.guards.active_set = GuardSetSelector::Bridges;
938
            }
939
            (_, false) => {
940
                self.configured_bridges = None;
941
                self.guards.active_set = GuardSetSelector::Default;
942
            }
943
        }
944

            
945
        // If we have gotten here, we have changed the set of bridges, changed
946
        // which set is active, or changed them both.  We need to make sure that
947
        // our `GuardSet` object is up-to-date with our configuration.
948
        self.update(wallclock, now);
949

            
950
        // We also need to tell the caller that its circuits are no good any
951
        // more.
952
        //
953
        // TODO(nickm): Someday we can do this more judiciously by retuning
954
        // "Some" in the case where we're still using bridges but our new bridge
955
        // set contains different elements; see comment on RetireCircuits.
956
        //
957
        // TODO(nickm): We could also safely return RetireCircuits::None if we
958
        // are using bridges, and our new bridge list is a superset of the older
959
        // one.
960
        Ok(RetireCircuits::All)
961
706
    }
962

            
963
    /// Update our parameters, our selection (based on network parameters and
964
    /// configuration), and make sure the active GuardSet has the right
965
    /// configuration itself.
966
    ///
967
    /// We should call this whenever the NetDir's parameters change, or whenever
968
    /// our filter changes.  We do not need to call it for new elements arriving
969
    /// in our Universe, since those do not affect anything here.
970
    ///
971
    /// We should also call this whenever a new GuardSet becomes active for any
972
    /// reason _other_ than just having called this function.
973
    ///
974
    /// (This function is only invoked from `update`, which should be called
975
    /// under the above circumstances.)
976
9976
    fn update_active_set_params_and_filter(&mut self, netdir: Option<&NetDir>) {
977
        // Set the parameters.  These always come from the NetDir, even if this
978
        // is a bridge set.
979
9976
        if let Some(netdir) = netdir {
980
1908
            match GuardParams::try_from(netdir.params()) {
981
1908
                Ok(params) => self.params = params,
982
                Err(e) => warn!("Unusable guard parameters from consensus: {}", e),
983
            }
984

            
985
1908
            self.select_guard_set_based_on_filter(netdir);
986
8068
        }
987

            
988
        // Change the filter, if it doesn't match what the guards have.
989
        //
990
        // TODO(nickm): We could use a "dirty" flag or something to decide
991
        // whether we need to call set_filter, if this comparison starts to show
992
        // up in profiles.
993
9976
        if self.guards.active_guards().filter() != &self.filter {
994
1036
            let restrictive = self.guards.active_set == GuardSetSelector::Restricted;
995
1036
            self.guards
996
1036
                .active_guards_mut()
997
1036
                .set_filter(self.filter.clone(), restrictive);
998
8940
        }
999
9976
    }
    /// Update the status of every guard in `active_guards`, and expand it as
    /// needed.
    ///
    /// This function doesn't take `&self`, to make sure that we are only
    /// affecting a single `GuardSet`, and to avoid confusing the borrow
    /// checker.
    ///
    /// We should call this whenever the contents of the universe have changed.
    ///
    /// We should also call this whenever a new GuardSet becomes active.
20096
    fn update_guardset_internal<U: Universe>(
20096
        params: &GuardParams,
20096
        now: SystemTime,
20096
        universe_type: UniverseType,
20096
        active_guards: &mut GuardSet,
20096
        universe: Option<&U>,
20096
    ) -> ExtendedStatus {
        // Expire guards.  Do that early, in case doing so makes it clear that
        // we need to grab more guards or mark others as primary.
20096
        active_guards.expire_old_guards(params, now);
20096
        let extended = if let Some(universe) = universe {
            // TODO: This check here may be completely unnecessary. I inserted
            // it back in 5ac0fcb7ef603e0d14 because I was originally concerned
            // it might be undesirable to list a primary guard as "missing dir
            // info" (and therefore unusable) if we were expecting to get its
            // microdescriptor "very soon."
            //
            // But due to the other check in `netdir_is_sufficient`, we
            // shouldn't be installing a netdir until it has microdescs for all
            // of the (non-bridge) primary guards that it lists. - nickm
12028
            let n = active_guards.n_primary_without_id_info_in(universe);
12028
            if n > 0 && universe_type == UniverseType::NetDir {
                // We are missing the information from a NetDir needed to see
                // whether our primary guards are listed, so we shouldn't update
                // our guard status.
                //
                // We don't want to do this check if we are using bridges, since
                // a missing bridge descriptor is not guaranteed to temporary
                // problem in the same way that a missing microdescriptor is.
                // (When a bridge desc is missing, the bridge could be down or
                // unreachable, and nobody else can help us. But if a microdesc
                // is missing, we just need to find a cache that has it.)
                trace!(
                    n_primary_without_id_info = n,
                    "Not extending guardset, missing information."
                );
                return ExtendedStatus::No;
12028
            }
12028
            active_guards.update_status_from_dir(universe);
12028
            active_guards.extend_sample_as_needed(now, params, universe)
        } else {
8068
            trace!("Not extending guardset, no universe given.");
8068
            ExtendedStatus::No
        };
20096
        active_guards.select_primary_guards(params);
20096
        extended
20096
    }
    /// If using bridges, tell the BridgeDescProvider which descriptors we want.
    /// We need to check this *after* we select our primary guards.
    #[cfg(feature = "bridge-client")]
9976
    fn update_desired_descriptors(&mut self, now: Instant) {
9976
        if self.guards.active_set.universe_type() != UniverseType::BridgeSet {
9976
            return;
        }
        let provider = self.bridge_desc_provider.as_ref().and_then(Weak::upgrade);
        let bridge_set = self.latest_bridge_set();
        if let (Some(provider), Some(bridge_set)) = (provider, bridge_set) {
            let desired: Vec<_> = self
                .guards
                .active_guards()
                .descriptors_to_request(now, &self.params)
                .into_iter()
                .flat_map(|guard| bridge_set.bridge_by_guard(guard))
                .cloned()
                .collect();
            provider.set_bridges(&desired);
        }
9976
    }
    /// Replace the active guard state with `new_state`, preserving
    /// non-persistent state for any guards that are retained.
    #[instrument(level = "trace", skip_all)]
    fn replace_guards_with(
        &mut self,
        mut new_guards: GuardSets,
        wallclock: SystemTime,
        now: Instant,
    ) {
        std::mem::swap(&mut self.guards, &mut new_guards);
        self.guards.copy_status_from(new_guards);
        self.update(wallclock, now);
    }
    /// Update which guard set is active based on the current filter and the
    /// provided netdir.
    ///
    /// After calling this function, the new guard set's filter may be
    /// out-of-date: be sure to call `set_filter` as appropriate.
1908
    fn select_guard_set_based_on_filter(&mut self, netdir: &NetDir) {
        // In general, we'd like to use the restricted set if we're under the
        // threshold, and the default set if we're over the threshold.  But if
        // we're sitting close to the threshold, we want to avoid flapping back
        // and forth, so we only change when we're more than 5% "off" from
        // whatever our current setting is.
        //
        // (See guard-spec section 2 for more information.)
1908
        let offset = match self.guards.active_set {
1908
            GuardSetSelector::Default => -0.05,
            GuardSetSelector::Restricted => 0.05,
            // If we're using bridges, then we don't switch between the other guard sets based on on the filter at all.
            #[cfg(feature = "bridge-client")]
            GuardSetSelector::Bridges => return,
        };
1908
        let frac_permitted = self.filter.frac_bw_permitted(netdir);
1908
        let threshold = self.params.filter_threshold + offset;
1908
        let new_choice = if frac_permitted < threshold {
12
            GuardSetSelector::Restricted
        } else {
1896
            GuardSetSelector::Default
        };
1908
        if new_choice != self.guards.active_set {
12
            info!(
                "Guard selection changed; we are now using the {:?} guard set",
                &new_choice
            );
12
            self.guards.active_set = new_choice;
12
            if frac_permitted < self.params.extreme_threshold {
                warn!(
                    "The number of guards permitted is smaller than the recommended minimum of {:.0}%.",
                    self.params.extreme_threshold * 100.0,
                );
12
            }
1896
        }
1908
    }
    /// Mark all of our primary guards as retriable, if we haven't done
    /// so since long enough before `now`.
    ///
    /// We want to call this function whenever a guard attempt succeeds,
    /// if the internet seemed to be down when the guard attempt was
    /// first launched.
    fn maybe_retry_primary_guards(&mut self, now: Instant) {
        // We don't actually want to mark our primary guards as
        // retriable more than once per internet_down_timeout: after
        // the first time, we would just be noticing the same "coming
        // back online" event more than once.
        let interval = self.params.internet_down_timeout;
        if self.last_primary_retry_time + interval <= now {
            debug!(
                "Successfully reached a guard after a while off the internet; marking all primary guards retriable."
            );
            self.guards
                .active_guards_mut()
                .mark_primary_guards_retriable();
            self.last_primary_retry_time = now;
        }
    }
    /// Replace the current GuardFilter with `filter`.
    #[instrument(level = "trace", skip_all)]
1024
    fn set_filter(&mut self, filter: GuardFilter, wallclock: SystemTime, now: Instant) {
1024
        self.filter = filter;
1024
        self.update(wallclock, now);
1024
    }
    /// Called when the circuit manager reports (via [`GuardMonitor`]) that
    /// a guard succeeded or failed.
    ///
    /// Changes the guard's status as appropriate, and updates the pending
    /// request as needed.
    #[allow(clippy::cognitive_complexity)]
16594
    pub(crate) fn handle_msg(
16594
        &mut self,
16594
        request_id: RequestId,
16594
        status: GuardStatus,
16594
        skew: Option<ClockSkew>,
16594
        runtime: &impl tor_rtcompat::SleepProvider,
16594
    ) {
16594
        if let Some(mut pending) = self.pending.remove(&request_id) {
            // If there was a pending request matching this RequestId, great!
16594
            let guard_id = pending.guard_id();
16594
            trace!(?guard_id, ?status, "Received report of guard status");
            // First, handle the skew report (if any)
16594
            if let Some(skew) = skew {
                let now = runtime.now();
                let observation = skew::SkewObservation { skew, when: now };
                match &guard_id.0 {
                    FirstHopIdInner::Guard(_, id) => {
                        self.guards.active_guards_mut().record_skew(id, observation);
                    }
                    FirstHopIdInner::Fallback(id) => {
                        self.fallbacks.note_skew(id, observation);
                    }
                }
                // TODO: We call this whenever we receive an observed clock
                // skew. That's not the perfect timing for two reasons.  First
                // off, it might be too frequent: it does an O(n) calculation,
                // which isn't ideal.  Second, it might be too infrequent: after
                // an hour has passed, a given observation won't be up-to-date
                // any more, and we might want to recalculate the skew
                // accordingly.
                self.update_skew(now);
16594
            }
16594
            match (status, &guard_id.0) {
                (GuardStatus::Failure, FirstHopIdInner::Fallback(id)) => {
                    // We used a fallback, and we weren't able to build a circuit through it.
                    self.fallbacks.note_failure(id, runtime.now());
                }
                (_, FirstHopIdInner::Fallback(_)) => {
                    // We don't record any other kind of circuit activity if we
                    // took the entry from the fallback list.
                }
512
                (GuardStatus::Success, FirstHopIdInner::Guard(sample, id)) => {
                    // If we had gone too long without any net activity when we
                    // gave out this guard, and now we're seeing a circuit
                    // succeed, tell the primary guards that they might be
                    // retriable.
512
                    if pending.net_has_been_down() {
                        self.maybe_retry_primary_guards(runtime.now());
512
                    }
                    // The guard succeeded.  Tell the GuardSet.
512
                    self.guards.guards_mut(sample).record_success(
512
                        id,
512
                        &self.params,
512
                        None,
512
                        runtime.wallclock(),
                    );
                    // Either tell the request whether the guard is
                    // usable, or schedule it as a "waiting" request.
512
                    if let Some(usable) = self.guard_usability_status(&pending, runtime.now()) {
512
                        trace!(?guard_id, usable, "Known usability status");
512
                        pending.reply(usable);
                    } else {
                        // This is the one case where we can't use the
                        // guard yet.
                        trace!(?guard_id, "Not able to answer right now");
                        pending.mark_waiting(runtime.now());
                        self.waiting.push(pending);
                    }
                }
36
                (GuardStatus::Failure, FirstHopIdInner::Guard(sample, id)) => {
36
                    self.guards
36
                        .guards_mut(sample)
36
                        .record_failure(id, None, runtime.now());
36
                    pending.reply(false);
36
                }
16046
                (GuardStatus::AttemptAbandoned, FirstHopIdInner::Guard(sample, id)) => {
16046
                    self.guards.guards_mut(sample).record_attempt_abandoned(id);
16046
                    pending.reply(false);
16046
                }
                (GuardStatus::Indeterminate, FirstHopIdInner::Guard(sample, id)) => {
                    self.guards
                        .guards_mut(sample)
                        .record_indeterminate_result(id);
                    pending.reply(false);
                }
            };
        } else {
            warn!(
                "Got a status {:?} for a request {:?} that wasn't pending",
                status, request_id
            );
        }
        // We might need to update the primary guards based on changes in the
        // status of guards above.
16594
        self.guards
16594
            .active_guards_mut()
16594
            .select_primary_guards(&self.params);
        // Some waiting request may just have become ready (usable or
        // not); we need to give them the information they're waiting
        // for.
16594
        self.expire_and_answer_pending_requests(runtime.now());
16594
    }
    /// Helper to implement `GuardMgr::note_external_success()`.
    ///
    /// (This has to be a separate function so that we can borrow params while
    /// we have `mut self` borrowed.)
12
    fn record_external_success<T>(
12
        &mut self,
12
        identity: &T,
12
        external_activity: ExternalActivity,
12
        now: SystemTime,
12
    ) where
12
        T: tor_linkspec::HasRelayIds + ?Sized,
    {
12
        for id in self.lookup_ids(identity) {
12
            match &id.0 {
12
                FirstHopIdInner::Guard(sample, id) => {
12
                    self.guards.guards_mut(sample).record_success(
12
                        id,
12
                        &self.params,
12
                        Some(external_activity),
12
                        now,
12
                    );
12
                }
                FirstHopIdInner::Fallback(id) => {
                    if external_activity == ExternalActivity::DirCache {
                        self.fallbacks.note_success(id);
                    }
                }
            }
        }
12
    }
    /// Return an iterator over all of the clock skew observations we've made
    /// for guards or fallbacks.
    fn skew_observations(&self) -> impl Iterator<Item = &skew::SkewObservation> {
        self.fallbacks
            .skew_observations()
            .chain(self.guards.active_guards().skew_observations())
    }
    /// Recalculate our estimated clock skew, and publish it to anybody who
    /// cares.
    fn update_skew(&mut self, now: Instant) {
        let estimate = skew::SkewEstimate::estimate_skew(self.skew_observations(), now);
        // TODO: we might want to do this only conditionally, when the skew
        // estimate changes.
        *self.send_skew.borrow_mut() = estimate;
    }
    /// If the circuit built because of a given [`PendingRequest`] may
    /// now be used (or discarded), return `Some(true)` or
    /// `Some(false)` respectively.
    ///
    /// Return None if we can't yet give an answer about whether such
    /// a circuit is usable.
10256
    fn guard_usability_status(&self, pending: &PendingRequest, now: Instant) -> Option<bool> {
10256
        match &pending.guard_id().0 {
10256
            FirstHopIdInner::Guard(sample, id) => self.guards.guards(sample).circ_usability_status(
10256
                id,
10256
                pending.usage(),
10256
                &self.params,
10256
                now,
            ),
            // Fallback circuits are usable immediately, since we don't have to wait to
            // see whether any _other_ circuit succeeds or fails.
            FirstHopIdInner::Fallback(_) => Some(true),
        }
10256
    }
    /// For requests that have been "waiting" for an answer for too long,
    /// expire them and tell the circuit manager that their circuits
    /// are unusable.
370222
    fn expire_and_answer_pending_requests(&mut self, now: Instant) {
        // A bit ugly: we use a separate Vec here to avoid borrowing issues,
        // and put it back when we're done.
370222
        let mut waiting = Vec::new();
370222
        std::mem::swap(&mut waiting, &mut self.waiting);
370222
        waiting.retain_mut(|pending| {
            let expired = pending
                .waiting_since()
                .and_then(|w| now.checked_duration_since(w))
                .map(|d| d >= self.params.np_idle_timeout)
                == Some(true);
            if expired {
                trace!(?pending, "Pending request expired");
                pending.reply(false);
                return false;
            }
            // TODO-SPEC: guard_usability_status isn't what the spec says.  It
            // says instead that we should look at _circuit_ status, saying:
            //  "   Definition: In the algorithm above, C2 "blocks" C1 if:
            // * C2 obeys all the restrictions that C1 had to obey, AND
            // * C2 has higher priority than C1, AND
            // * Either C2 is <complete>, or C2 is <waiting_for_better_guard>,
            // or C2 has been <usable_if_no_better_guard> for no more than
            // {NONPRIMARY_GUARD_CONNECT_TIMEOUT} seconds."
            //
            // See comments in sample::GuardSet::circ_usability_status.
            if let Some(answer) = self.guard_usability_status(pending, now) {
                trace!(?pending, answer, "Pending request now ready");
                pending.reply(answer);
                return false;
            }
            true
        });
        // Put the waiting list back.
370222
        std::mem::swap(&mut waiting, &mut self.waiting);
370222
    }
    /// Return every currently extant FirstHopId for a guard or fallback
    /// directory matching (or possibly matching) the provided keys.
    ///
    /// An identity is _possibly matching_ if it contains some of the IDs in the
    /// provided identity, and it has no _contradictory_ identities, but it does
    /// not necessarily contain _all_ of those identities.
    ///
    /// # TODO
    ///
    /// This function should probably not exist; it's only used so that dirmgr
    /// can report successes or failures, since by the time it observes them it
    /// doesn't know whether its circuit came from a guard or a fallback.  To
    /// solve that, we'll need CircMgr to record and report which one it was
    /// using, which will take some more plumbing.
    ///
    /// TODO relay: we will have to make the change above when we implement
    /// relays; otherwise, it would be possible for an attacker to exploit it to
    /// mislead us about our guard status.
24
    fn lookup_ids<T>(&self, identity: &T) -> Vec<FirstHopId>
24
    where
24
        T: tor_linkspec::HasRelayIds + ?Sized,
    {
        use strum::IntoEnumIterator;
24
        let mut vec = Vec::with_capacity(2);
24
        let id = ids::GuardId::from_relay_ids(identity);
72
        for sample in GuardSetSelector::iter() {
72
            let guard_id = match self.guards.guards(&sample).contains(&id) {
24
                Ok(true) => &id,
                Err(other) => other,
48
                Ok(false) => continue,
            };
24
            vec.push(FirstHopId(FirstHopIdInner::Guard(sample, guard_id.clone())));
        }
24
        let id = ids::FallbackId::from_relay_ids(identity);
24
        if self.fallbacks.contains(&id) {
            vec.push(id.into());
24
        }
24
        vec
24
    }
    /// Run any periodic events that update guard status, and return a
    /// duration after which periodic events should next be run.
    #[instrument(skip_all, level = "trace")]
7044
    pub(crate) fn run_periodic_events(&mut self, wallclock: SystemTime, now: Instant) -> Duration {
7044
        self.update(wallclock, now);
7044
        self.expire_and_answer_pending_requests(now);
7044
        Duration::from_secs(1) // TODO: Too aggressive.
7044
    }
    /// Try to select a guard, expanding the sample if the first attempt fails.
    #[instrument(skip_all, level = "trace")]
555940
    fn select_guard_with_expand(
555940
        &mut self,
555940
        usage: &GuardUsage,
555940
        now: Instant,
555940
        wallclock: SystemTime,
555940
    ) -> Result<(sample::ListKind, FirstHop), PickGuardError> {
        // Try to find a guard.
555940
        let first_error = match self.select_guard_once(usage, now) {
545820
            Ok(res1) => return Ok(res1),
10120
            Err(e) => {
10120
                trace!("Couldn't select guard on first attempt: {}", e);
10120
                e
            }
        };
        // That didn't work. If we have a netdir, expand the sample and try again.
10350
        let res = self.with_opt_universe(|this, univ| {
10120
            let univ = univ?;
10120
            trace!("No guards available, trying to extend the sample.");
            // Make sure that the status on all of our guards are accurate, and
            // expand the sample if we can.
            //
            // Our parameters and configuration did not change, so we do not
            // need to call update() or update_active_set_and_filter(). This
            // call is sufficient to  extend the sample and recompute primary
            // guards.
10120
            let extended = Self::update_guardset_internal(
10120
                &this.params,
10120
                wallclock,
10120
                this.guards.active_set.universe_type(),
10120
                this.guards.active_guards_mut(),
10120
                Some(univ),
            );
10120
            if extended == ExtendedStatus::Yes {
10120
                match this.select_guard_once(usage, now) {
10120
                    Ok(res) => return Some(res),
                    Err(e) => {
                        trace!("Couldn't select guard after update: {}", e);
                    }
                }
            }
            None
10120
        });
10120
        if let Some(res) = res {
10120
            return Ok(res);
        }
        // Okay, that didn't work either.  If we were asked for a directory
        // guard, and we aren't using bridges, then we may be able to use a
        // fallback.
        if usage.kind == GuardUsageKind::OneHopDirectory
            && self.guards.active_set.universe_type() == UniverseType::NetDir
        {
            return self.select_fallback(now);
        }
        // Couldn't extend the sample or use a fallback; return the original error.
        Err(first_error)
555940
    }
    /// Helper: try to pick a single guard, without retrying on failure.
566060
    fn select_guard_once(
566060
        &self,
566060
        usage: &GuardUsage,
566060
        now: Instant,
566060
    ) -> Result<(sample::ListKind, FirstHop), PickGuardError> {
566060
        let active_set = &self.guards.active_set;
        #[cfg_attr(not(feature = "bridge-client"), allow(unused_mut))]
555940
        let (list_kind, mut first_hop) =
566060
            self.guards
566060
                .guards(active_set)
566060
                .pick_guard(active_set, usage, &self.params, now)?;
        #[cfg(feature = "bridge-client")]
555940
        if self.guards.active_set.universe_type() == UniverseType::BridgeSet {
            // See if we can promote first_hop to a viable CircTarget.
            let bridges = self.latest_bridge_set().ok_or_else(|| {
                PickGuardError::Internal(internal!(
                    "No bridge set available, even though this is the Bridges sample"
                ))
            })?;
            first_hop.lookup_bridge_circ_target(&bridges);
            if usage.kind == GuardUsageKind::Data && !first_hop.contains_circ_target() {
                return Err(PickGuardError::Internal(internal!(
                    "Tried to return a non-circtarget guard with Data usage!"
                )));
            }
555940
        }
555940
        Ok((list_kind, first_hop))
566060
    }
    /// Helper: Select a fallback directory.
    ///
    /// Called when we have no guard information to use. Return values are as
    /// for [`GuardMgr::select_guard()`]
    fn select_fallback(
        &self,
        now: Instant,
    ) -> Result<(sample::ListKind, FirstHop), PickGuardError> {
        let filt = self.guards.active_guards().filter();
        let fallback = crate::FirstHop {
            sample: None,
            inner: crate::FirstHopInner::Chan(OwnedChanTarget::from_chan_target(
                self.fallbacks.choose(&mut rand::rng(), now, filt)?,
            )),
        };
        let fallback = filt.modify_hop(fallback)?;
        Ok((sample::ListKind::Fallback, fallback))
    }
}
/// A possible outcome of trying to extend a guard sample.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum ExtendedStatus {
    /// The guard sample was extended. (At least one guard was added to it.)
    Yes,
    /// The guard sample was not extended.
    No,
}
/// A set of parameters, derived from the consensus document, controlling
/// the behavior of a guard manager.
#[derive(Debug, Clone)]
#[cfg_attr(test, derive(PartialEq))]
struct GuardParams {
    /// How long should a sampled, un-confirmed guard be kept in the sample before it expires?
    lifetime_unconfirmed: Duration,
    /// How long should a confirmed guard be kept in the sample before
    /// it expires?
    lifetime_confirmed: Duration,
    /// How long may  a guard be unlisted before we remove it from the sample?
    lifetime_unlisted: Duration,
    /// Largest number of guards we're willing to add to the sample.
    max_sample_size: usize,
    /// Largest fraction of the network's guard bandwidth that we're
    /// willing to add to the sample.
    max_sample_bw_fraction: f64,
    /// Smallest number of guards that we're willing to have in the
    /// sample, after applying a [`GuardFilter`].
    min_filtered_sample_size: usize,
    /// How many guards are considered "Primary"?
    n_primary: usize,
    /// When making a regular circuit, how many primary guards should we
    /// be willing to try?
    data_parallelism: usize,
    /// When making a one-hop directory circuit, how many primary
    /// guards should we be willing to try?
    dir_parallelism: usize,
    /// For how long does a pending attempt to connect to a guard
    /// block an attempt to use a less-favored non-primary guard?
    np_connect_timeout: Duration,
    /// How long do we allow a circuit to a successful but unfavored
    /// non-primary guard to sit around before deciding not to use it?
    np_idle_timeout: Duration,
    /// After how much time without successful activity does a
    /// successful circuit indicate that we should retry our primary
    /// guards?
    internet_down_timeout: Duration,
    /// What fraction of the guards can be can be filtered out before we
    /// decide that our filter is "very restrictive"?
    filter_threshold: f64,
    /// What fraction of the guards determine that our filter is "very
    /// restrictive"?
    extreme_threshold: f64,
}
impl Default for GuardParams {
14216
    fn default() -> Self {
14216
        let one_day = Duration::from_secs(86400);
14216
        GuardParams {
14216
            lifetime_unconfirmed: one_day * 120,
14216
            lifetime_confirmed: one_day * 60,
14216
            lifetime_unlisted: one_day * 20,
14216
            max_sample_size: 60,
14216
            max_sample_bw_fraction: 0.2,
14216
            min_filtered_sample_size: 20,
14216
            n_primary: 3,
14216
            data_parallelism: 1,
14216
            dir_parallelism: 3,
14216
            np_connect_timeout: Duration::from_secs(15),
14216
            np_idle_timeout: Duration::from_secs(600),
14216
            internet_down_timeout: Duration::from_secs(600),
14216
            filter_threshold: 0.2,
14216
            extreme_threshold: 0.01,
14216
        }
14216
    }
}
impl TryFrom<&NetParameters> for GuardParams {
    type Error = tor_units::Error;
1910
    fn try_from(p: &NetParameters) -> Result<GuardParams, Self::Error> {
        Ok(GuardParams {
1910
            lifetime_unconfirmed: p.guard_lifetime_unconfirmed.try_into()?,
1910
            lifetime_confirmed: p.guard_lifetime_confirmed.try_into()?,
1910
            lifetime_unlisted: p.guard_remove_unlisted_after.try_into()?,
1910
            max_sample_size: p.guard_max_sample_size.try_into()?,
1910
            max_sample_bw_fraction: p.guard_max_sample_threshold.as_fraction(),
1910
            min_filtered_sample_size: p.guard_filtered_min_sample_size.try_into()?,
1910
            n_primary: p.guard_n_primary.try_into()?,
1910
            data_parallelism: p.guard_use_parallelism.try_into()?,
1910
            dir_parallelism: p.guard_dir_use_parallelism.try_into()?,
1910
            np_connect_timeout: p.guard_nonprimary_connect_timeout.try_into()?,
1910
            np_idle_timeout: p.guard_nonprimary_idle_timeout.try_into()?,
1910
            internet_down_timeout: p.guard_internet_likely_down.try_into()?,
1910
            filter_threshold: p.guard_meaningful_restriction.as_fraction(),
1910
            extreme_threshold: p.guard_extreme_restriction.as_fraction(),
        })
1910
    }
}
/// Representation of a guard or fallback, as returned by [`GuardMgr::select_guard()`].
#[derive(Debug, Clone)]
pub struct FirstHop {
    /// The sample from which this guard was taken, or `None` if this is a fallback.
    sample: Option<GuardSetSelector>,
    /// Information about connecting to (or through) this guard.
    inner: FirstHopInner,
}
/// The enumeration inside a FirstHop that holds information about how to
/// connect to (and possibly through) a guard or fallback.
#[derive(Debug, Clone)]
enum FirstHopInner {
    /// We have enough information to connect to a guard.
    Chan(OwnedChanTarget),
    /// We have enough information to connect to a guards _and_ to build
    /// multihop circuits through it.
    #[cfg_attr(not(feature = "bridge-client"), allow(dead_code))]
    Circ(OwnedCircTarget),
}
impl FirstHop {
    /// Return a new [`FirstHopId`] for this `FirstHop`.
555940
    fn first_hop_id(&self) -> FirstHopId {
555940
        match &self.sample {
555940
            Some(sample) => {
555940
                let guard_id = GuardId::from_relay_ids(self);
555940
                FirstHopId::in_sample(sample.clone(), guard_id)
            }
            None => {
                let fallback_id = crate::ids::FallbackId::from_relay_ids(self);
                FirstHopId::from(fallback_id)
            }
        }
555940
    }
    /// Look up this guard in `netdir`.
544984
    pub fn get_relay<'a>(&self, netdir: &'a NetDir) -> Option<Relay<'a>> {
544984
        match &self.sample {
            #[cfg(feature = "bridge-client")]
            // Always return "None" for anything that isn't in the netdir.
544984
            Some(s) if s.universe_type() == UniverseType::BridgeSet => None,
            // Otherwise ask the netdir.
544984
            _ => netdir.by_ids(self),
        }
544984
    }
    /// Return true if this guard is a bridge.
    pub fn is_bridge(&self) -> bool {
        match &self.sample {
            #[cfg(feature = "bridge-client")]
            Some(s) if s.universe_type() == UniverseType::BridgeSet => true,
            _ => false,
        }
    }
    /// If possible, return a view of this object that can be used to build a circuit.
544984
    pub fn as_circ_target(&self) -> Option<&OwnedCircTarget> {
544984
        match &self.inner {
544984
            FirstHopInner::Chan(_) => None,
            FirstHopInner::Circ(ct) => Some(ct),
        }
544984
    }
    /// Return a view of this as an OwnedChanTarget.
12
    fn chan_target_mut(&mut self) -> &mut OwnedChanTarget {
12
        match &mut self.inner {
12
            FirstHopInner::Chan(ct) => ct,
            FirstHopInner::Circ(ct) => ct.chan_target_mut(),
        }
12
    }
    /// If possible and appropriate, find a circuit target in `bridges` for this
    /// `FirstHop`, and make this `FirstHop` a viable circuit target.
    ///
    /// (By default, any `FirstHop` that a `GuardSet` returns will have enough
    /// information to be a `ChanTarget`, but it will be lacking the additional
    /// network information in `CircTarget`[^1] necessary for us to build a
    /// multi-hop circuit through it.  If this FirstHop is a regular non-bridge
    /// `Relay`, then the `CircMgr` will later look up that circuit information
    /// itself from the network directory. But if this `FirstHop` *is* a bridge,
    /// then we need to find that information in the `BridgeSet`, since the
    /// CircMgr does not keep track of the `BridgeSet`.)
    ///
    /// [^1]: For example, supported protocol versions and ntor keys.
    #[cfg(feature = "bridge-client")]
    fn lookup_bridge_circ_target(&mut self, bridges: &bridge::BridgeSet) {
        use crate::sample::CandidateStatus::Present;
        if self.sample.as_ref().map(|s| s.universe_type()) == Some(UniverseType::BridgeSet)
            && matches!(self.inner, FirstHopInner::Chan(_))
        {
            if let Present(bridge_relay) = bridges.bridge_relay_by_guard(self) {
                if let Some(circ_target) = bridge_relay.as_relay_with_desc() {
                    self.inner =
                        FirstHopInner::Circ(OwnedCircTarget::from_circ_target(&circ_target));
                }
            }
        }
    }
    /// Return true if this `FirstHop` contains circuit target information.
    ///
    /// This is true if `lookup_bridge_circ_target()` has been called, and it
    /// successfully found the circuit target information.
    #[cfg(feature = "bridge-client")]
    fn contains_circ_target(&self) -> bool {
        matches!(self.inner, FirstHopInner::Circ(_))
    }
}
// This is somewhat redundant with the implementations in crate::guard::Guard.
impl tor_linkspec::HasAddrs for FirstHop {
10836
    fn addrs(&self) -> impl Iterator<Item = SocketAddr> {
10836
        match &self.inner {
10836
            FirstHopInner::Chan(ct) => Either::Left(ct.addrs()),
            FirstHopInner::Circ(ct) => Either::Right(ct.addrs()),
        }
10836
    }
}
impl tor_linkspec::HasRelayIds for FirstHop {
3335640
    fn identity(
3335640
        &self,
3335640
        key_type: tor_linkspec::RelayIdType,
3335640
    ) -> Option<tor_linkspec::RelayIdRef<'_>> {
3335640
        match &self.inner {
3335640
            FirstHopInner::Chan(ct) => ct.identity(key_type),
            FirstHopInner::Circ(ct) => ct.identity(key_type),
        }
3335640
    }
}
impl tor_linkspec::HasChanMethod for FirstHop {
10824
    fn chan_method(&self) -> tor_linkspec::ChannelMethod {
10824
        match &self.inner {
10824
            FirstHopInner::Chan(ct) => ct.chan_method(),
            FirstHopInner::Circ(ct) => ct.chan_method(),
        }
10824
    }
}
impl tor_linkspec::ChanTarget for FirstHop {}
/// The purpose for which we plan to use a guard.
///
/// This can affect the guard selection algorithm.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum GuardUsageKind {
    /// We want to use this guard for a data circuit.
    ///
    /// (This encompasses everything except the `OneHopDirectory` case.)
    #[default]
    Data,
    /// We want to use this guard for a one-hop, non-anonymous
    /// directory request.
    ///
    /// (Our algorithm allows more parallelism for the guards that we use
    /// for these circuits.)
    OneHopDirectory,
}
/// A set of parameters describing how a single guard should be selected.
///
/// Used as an argument to [`GuardMgr::select_guard`].
#[derive(Clone, Debug, derive_builder::Builder)]
#[builder(build_fn(error = "tor_config::ConfigBuildError"))]
pub struct GuardUsage {
    /// The purpose for which this guard will be used.
    #[builder(default)]
    kind: GuardUsageKind,
    /// A list of restrictions on which guard may be used.
    ///
    /// The default is the empty list.
    #[builder(sub_builder, setter(custom))]
    restrictions: GuardRestrictionList,
}
impl_standard_builder! { GuardUsage: !Deserialize }
/// List of socket restrictions, as configured
pub type GuardRestrictionList = Vec<GuardRestriction>;
define_list_builder_helper! {
    pub struct GuardRestrictionListBuilder {
        restrictions: [GuardRestriction],
    }
    built: GuardRestrictionList = restrictions;
    default = vec![];
4456
    item_build: |restriction| Ok(restriction.clone());
}
define_list_builder_accessors! {
    struct GuardUsageBuilder {
        pub restrictions: [GuardRestriction],
    }
}
impl GuardUsageBuilder {
    /// Create a new empty [`GuardUsageBuilder`].
28
    pub fn new() -> Self {
28
        Self::default()
28
    }
}
/// A restriction that applies to a single request for a guard.
///
/// Restrictions differ from filters (see [`GuardFilter`]) in that
/// they apply to single requests, not to our entire set of guards.
/// They're suitable for things like making sure that we don't start
/// and end a circuit at the same relay, or requiring a specific
/// subprotocol version for certain kinds of requests.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[non_exhaustive]
pub enum GuardRestriction {
    /// Don't pick a guard with the provided identity.
    AvoidId(RelayId),
    /// Don't pick a guard with any of the provided Ed25519 identities.
    AvoidAllIds(RelayIdSet),
}
/// The kind of vanguards to use.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] //
#[derive(Serialize, Deserialize)] //
#[derive(derive_more::Display)] //
#[serde(rename_all = "lowercase")]
#[cfg(feature = "vanguards")]
#[non_exhaustive]
pub enum VanguardMode {
    /// "Lite" vanguards.
    #[default]
    #[display("lite")]
    Lite = 1,
    /// "Full" vanguards.
    #[display("full")]
    Full = 2,
    /// Vanguards are disabled.
    #[display("disabled")]
    Disabled = 0,
}
#[cfg(feature = "vanguards")]
impl VanguardMode {
    /// Build a `VanguardMode` from a [`NetParameters`] parameter.
    ///
    /// Used for converting [`vanguards_enabled`](NetParameters::vanguards_enabled)
    /// or [`vanguards_hs_service`](NetParameters::vanguards_hs_service)
    /// to the corresponding `VanguardMode`.
5232
    pub(crate) fn from_net_parameter(val: BoundedInt32<0, 2>) -> Self {
5232
        match val.get() {
            0 => VanguardMode::Disabled,
2648
            1 => VanguardMode::Lite,
2584
            2 => VanguardMode::Full,
            _ => unreachable!("BoundedInt32 was not bounded?!"),
        }
5232
    }
}
impl_not_auto_value!(VanguardMode);
/// Vanguards configuration.
#[derive(Deftly, Clone, Debug, PartialEq, Eq)]
#[derive_deftly(TorConfig)]
pub struct VanguardConfig {
    /// The kind of vanguards to use.
    #[deftly(tor_config(default))]
    mode: ExplicitOrAuto<VanguardMode>,
}
impl VanguardConfig {
    /// Return the configured [`VanguardMode`].
    ///
    /// Returns the [`Default`] `VanguardMode`
    /// if the mode is [`Auto`](ExplicitOrAuto) or unspecified.
3964
    pub fn mode(&self) -> VanguardMode {
3964
        match self.mode {
2420
            ExplicitOrAuto::Auto => Default::default(),
1544
            ExplicitOrAuto::Explicit(mode) => mode,
        }
3964
    }
}
/// The kind of vanguards to use.
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] //
#[derive(Serialize, Deserialize)] //
#[derive(derive_more::Display)] //
#[serde(rename_all = "lowercase")]
#[cfg(not(feature = "vanguards"))]
#[non_exhaustive]
pub enum VanguardMode {
    /// Vanguards are disabled.
    #[default]
    #[display("disabled")]
    Disabled = 0,
}
#[cfg(test)]
mod test {
    // @@ begin test lint list maintained by maint/add_warning @@
    #![allow(clippy::bool_assert_comparison)]
    #![allow(clippy::clone_on_copy)]
    #![allow(clippy::dbg_macro)]
    #![allow(clippy::mixed_attributes_style)]
    #![allow(clippy::print_stderr)]
    #![allow(clippy::print_stdout)]
    #![allow(clippy::single_char_pattern)]
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::unchecked_time_subtraction)]
    #![allow(clippy::useless_vec)]
    #![allow(clippy::needless_pass_by_value)]
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
    use super::*;
    use tor_linkspec::{HasAddrs, HasRelayIds};
    use tor_persist::TestingStateMgr;
    use tor_rtcompat::test_with_all_runtimes;
    #[test]
    fn guard_param_defaults() {
        let p1 = GuardParams::default();
        let p2: GuardParams = (&NetParameters::default()).try_into().unwrap();
        assert_eq!(p1, p2);
    }
    fn init<R: Runtime>(rt: R) -> (GuardMgr<R>, TestingStateMgr, NetDir) {
        use tor_netdir::{MdReceiver, PartialNetDir, testnet};
        let statemgr = TestingStateMgr::new();
        let have_lock = statemgr.try_lock().unwrap();
        assert!(have_lock.held());
        let guardmgr = GuardMgr::new(rt, statemgr.clone(), &TestConfig::default()).unwrap();
        let (con, mds) = testnet::construct_network().unwrap();
        let param_overrides = vec![
            // We make the sample size smaller than usual to compensate for the
            // small testing network.  (Otherwise, we'd sample the whole network,
            // and not be able to observe guards in the tests.)
            "guard-min-filtered-sample-size=5",
            // We choose only two primary guards, to make the tests easier to write.
            "guard-n-primary-guards=2",
            // We define any restriction that allows 75% or fewer of relays as "meaningful",
            // so that we can test the "restrictive" guard sample behavior, and to avoid
            "guard-meaningful-restriction-percent=75",
        ];
        let param_overrides: String =
            itertools::Itertools::intersperse(param_overrides.into_iter(), " ").collect();
        let override_p = param_overrides.parse().unwrap();
        let mut netdir = PartialNetDir::new(con, Some(&override_p));
        for md in mds {
            netdir.add_microdesc(md);
        }
        let netdir = netdir.unwrap_if_sufficient().unwrap();
        (guardmgr, statemgr, netdir)
    }
    #[test]
    #[allow(clippy::clone_on_copy)]
    fn simple_case() {
        test_with_all_runtimes!(|rt| async move {
            let (guardmgr, statemgr, netdir) = init(rt.clone());
            let usage = GuardUsage::default();
            guardmgr.install_test_netdir(&netdir);
            let (id, mon, usable) = guardmgr.select_guard(usage).unwrap();
            // Report that the circuit succeeded.
            mon.succeeded();
            // May we use the circuit?
            let usable = usable.await.unwrap();
            assert!(usable);
            // Save the state...
            guardmgr.flush_msg_queue().await;
            guardmgr.store_persistent_state().unwrap();
            drop(guardmgr);
            // Try reloading from the state...
            let guardmgr2 =
                GuardMgr::new(rt.clone(), statemgr.clone(), &TestConfig::default()).unwrap();
            guardmgr2.install_test_netdir(&netdir);
            // Since the guard was confirmed, we should get the same one this time!
            let usage = GuardUsage::default();
            let (id2, _mon, _usable) = guardmgr2.select_guard(usage).unwrap();
            assert!(id2.same_relay_ids(&id));
        });
    }
    #[test]
    fn simple_waiting() {
        // TODO(nickm): This test fails in rare cases; I suspect a
        // race condition somewhere.
        //
        // I've doubled up on the queue flushing in order to try to make the
        // race less likely, but we should investigate.
        test_with_all_runtimes!(|rt| async move {
            let (guardmgr, _statemgr, netdir) = init(rt);
            let u = GuardUsage::default();
            guardmgr.install_test_netdir(&netdir);
            // We'll have the first two guard fail, which should make us
            // try a non-primary guard.
            let (id1, mon, _usable) = guardmgr.select_guard(u.clone()).unwrap();
            mon.failed();
            guardmgr.flush_msg_queue().await; // avoid race
            guardmgr.flush_msg_queue().await; // avoid race
            let (id2, mon, _usable) = guardmgr.select_guard(u.clone()).unwrap();
            mon.failed();
            guardmgr.flush_msg_queue().await; // avoid race
            guardmgr.flush_msg_queue().await; // avoid race
            assert!(!id1.same_relay_ids(&id2));
            // Now we should get two sampled guards. They should be different.
            let (id3, mon3, usable3) = guardmgr.select_guard(u.clone()).unwrap();
            let (id4, mon4, usable4) = guardmgr.select_guard(u.clone()).unwrap();
            assert!(!id3.same_relay_ids(&id4));
            let (u3, u4) = futures::join!(
                async {
                    mon3.failed();
                    guardmgr.flush_msg_queue().await; // avoid race
                    usable3.await.unwrap()
                },
                async {
                    mon4.succeeded();
                    usable4.await.unwrap()
                }
            );
            assert_eq!((u3, u4), (false, true));
        });
    }
    #[test]
    fn filtering_basics() {
        test_with_all_runtimes!(|rt| async move {
            let (guardmgr, _statemgr, netdir) = init(rt);
            let u = GuardUsage::default();
            let filter = {
                let mut f = GuardFilter::default();
                // All the addresses in the test network are {0,1,2,3,4}.0.0.3:9001.
                // Limit to only 2.0.0.0/8
                f.push_reachable_addresses(vec!["2.0.0.0/8:9001".parse().unwrap()]);
                f
            };
            guardmgr.set_filter(filter);
            guardmgr.install_test_netdir(&netdir);
            let (guard, _mon, _usable) = guardmgr.select_guard(u).unwrap();
            // Make sure that the filter worked.
            let addr = guard.addrs().next().unwrap();
            assert_eq!(addr, "2.0.0.3:9001".parse().unwrap());
        });
    }
    #[test]
    fn external_status() {
        test_with_all_runtimes!(|rt| async move {
            let (guardmgr, _statemgr, netdir) = init(rt);
            let data_usage = GuardUsage::default();
            let dir_usage = GuardUsageBuilder::new()
                .kind(GuardUsageKind::OneHopDirectory)
                .build()
                .unwrap();
            guardmgr.install_test_netdir(&netdir);
            {
                // Override this parameter, so that we can get deterministic results below.
                let mut inner = guardmgr.inner.lock().unwrap();
                inner.params.dir_parallelism = 1;
            }
            let (guard, mon, _usable) = guardmgr.select_guard(data_usage.clone()).unwrap();
            mon.succeeded();
            // Record that this guard gave us a bad directory object.
            guardmgr.note_external_failure(&guard, ExternalActivity::DirCache);
            // We ask for another guard, for data usage.  We should get the same
            // one as last time, since the director failure doesn't mean this
            // guard is useless as a primary guard.
            let (g2, mon, _usable) = guardmgr.select_guard(data_usage).unwrap();
            assert_eq!(g2.ed_identity(), guard.ed_identity());
            mon.succeeded();
            // But if we ask for a guard for directory usage, we should get a
            // different one, since the last guard we gave out failed.
            let (g3, mon, _usable) = guardmgr.select_guard(dir_usage.clone()).unwrap();
            assert_ne!(g3.ed_identity(), guard.ed_identity());
            mon.succeeded();
            // Now record a success for for directory usage.
            guardmgr.note_external_success(&guard, ExternalActivity::DirCache);
            // Now that the guard is working as a cache, asking for it should get us the same guard.
            let (g4, _mon, _usable) = guardmgr.select_guard(dir_usage).unwrap();
            assert_eq!(g4.ed_identity(), guard.ed_identity());
        });
    }
    #[cfg(feature = "vanguards")]
    #[test]
    fn vanguard_mode_ord() {
        assert!(VanguardMode::Disabled < VanguardMode::Lite);
        assert!(VanguardMode::Disabled < VanguardMode::Full);
        assert!(VanguardMode::Lite < VanguardMode::Full);
    }
}