Struct tor_netdir::NetDir

source ·
pub struct NetDir { /* private fields */ }
Expand description

A view of the Tor directory, suitable for use in building circuits.

Abstractly, a NetDir is a set of usable public Relays, each of which has its own properties, identity, and correct weighted probability for use under different circumstances.

A NetDir is constructed by making a PartialNetDir from a consensus document, and then adding enough microdescriptors to that PartialNetDir so that it can be used to build paths. (Thus, if you have a NetDir, it is definitely adequate to build paths.)

§“Usable” relays

Many methods on NetDir are defined in terms of “Usable” relays. Unless otherwise stated, a relay is “usable” if it is listed in the consensus, if we have full directory information for that relay (including a microdescriptor), and if that relay does not have any flags indicating that we should never use it. (Currently, NoEdConsensus is the only such flag.)

§Limitations

The current NetDir implementation assumes fairly strongly that every relay has an Ed25519 identity and an RSA identity, that the consensus is indexed by RSA identities, and that the Ed25519 identities are stored in microdescriptors.

If these assumptions someday change, then we’ll have to revise the implementation.

Implementations§

source§

impl NetDir

source

pub fn lifetime(&self) -> &Lifetime

Return the declared lifetime of this NetDir.

source

pub fn replace_overridden_parameters( &mut self, new_replacement: &NetParams<i32> )

Replace the overridden parameters in this netdir with new_replacement.

After this function is done, the netdir’s parameters will be those in the consensus, overridden by settings from new_replacement. Any settings in the old replacement parameters will be discarded.

source

pub fn all_relays(&self) -> impl Iterator<Item = UncheckedRelay<'_>>

Return an iterator over all Relay objects, including invalid ones that we can’t use.

source

pub fn relays(&self) -> impl Iterator<Item = Relay<'_>>

Return an iterator over all usable Relays.

source

pub fn by_id<'a, T>(&self, id: T) -> Option<Relay<'_>>
where T: Into<RelayIdRef<'a>> + ?Sized,

Return a relay matching a given identity, if we have a usable relay with that key.

(Does not return unusable relays.)

Note that a None answer is not always permanent: if a microdescriptor is subsequently added for a relay with this ID, the ID may become usable even if it was not usable before.

source

pub fn by_ids<T>(&self, target: &T) -> Option<Relay<'_>>
where T: HasRelayIds + ?Sized,

Return a relay with the same identities as those in target, if one exists.

Does not return unusable relays.

§Limitations

This will be very slow if target does not have an Ed25519 or RSA identity.

source

pub fn by_ids_detailed<T>( &self, target: &T ) -> Result<Option<Relay<'_>>, RelayLookupError>
where T: HasRelayIds + ?Sized,

Available on crate feature hs-common only.

Check whether there is a relay that has at least one identity from target, and which could have every identity from target. If so, return such a relay.

Return Ok(None) if we did not find a relay with any identity from target.

Return RelayLookupError::Impossible if we found a relay with at least one identity from target, but that relay’s other identities contradict what we learned from target.

Does not return unusable relays.

(This function is only useful if you need to distinguish the “impossible” case from the “no such relay known” case.)

§Limitations

This will be very slow if target does not have an Ed25519 or RSA identity.

source

pub fn ids_listed<T>(&self, target: &T) -> Option<bool>
where T: HasRelayIds + ?Sized,

As id_pair_listed, but check whether a relay exists (or may exist) with the same identities as those in target.

§Limitations

This can be inefficient if the target does not have both an ed25519 and an rsa identity key.

source

pub fn by_rsa_id_unchecked( &self, rsa_id: &RsaIdentity ) -> Option<UncheckedRelay<'_>>

Available on crate feature experimental-api only.

Return a (possibly unusable) relay with a given RSA identity.

This API can be used to find information about a relay that is listed in the current consensus, even if we don’t yet have enough information (like a microdescriptor) about the relay to use it.

source

pub fn rsa_id_is_listed(&self, rsa_id: &RsaIdentity) -> bool

Available on crate feature experimental-api only.

Return true if rsa_id is listed in this directory, even if it isn’t currently usable.

(An “unusable” relay in this context is one for which we don’t have full directory information.)

source

pub fn params(&self) -> &NetParameters

Return the parameters from the consensus, clamped to the correct ranges, with defaults filled in.

NOTE: that unsupported parameters aren’t returned here; only those values configured in the params module are available.

source

pub fn relay_protocol_status(&self) -> &ProtoStatus

Available on crate feature hs-common only.

Return a ProtoStatus that lists the network’s current requirements and recommendations for the list of protocols that every relay must implement.

source

pub fn pick_relay<'a, R, P>( &'a self, rng: &mut R, role: WeightRole, usable: P ) -> Option<Relay<'a>>
where R: Rng, P: FnMut(&Relay<'a>) -> bool,

Choose a relay at random.

Each relay is chosen with probability proportional to its weight in the role role, and is only selected if the predicate usable returns true for it.

This function returns None if (and only if) there are no relays with nonzero weight where usable returned true.

source

pub fn pick_n_relays<'a, R, P>( &'a self, rng: &mut R, n: usize, role: WeightRole, usable: P ) -> Vec<Relay<'a>>
where R: Rng, P: FnMut(&Relay<'a>) -> bool,

Choose n relay at random.

Each relay is chosen with probability proportional to its weight in the role role, and is only selected if the predicate usable returns true for it.

Relays are chosen without replacement: no relay will be returned twice. Therefore, the resulting vector may be smaller than n if we happen to have fewer than n appropriate relays.

This function returns an empty vector if (and only if) there are no relays with nonzero weight where usable returned true.

source

pub fn relay_weight<'a>( &'a self, relay: &Relay<'a>, role: WeightRole ) -> RelayWeight

Compute the weight with which relay will be selected for a given role.

source

pub fn total_weight<P>(&self, role: WeightRole, usable: P) -> RelayWeight
where P: Fn(&UncheckedRelay<'_>) -> bool,

Compute the total weight with which any relay matching usable will be selected for a given role.

Note: because this function is used to assess the total properties of the consensus, the usable predicate takes a RouterStatus rather than a Relay.

source

pub fn weight_by_rsa_id( &self, rsa_id: &RsaIdentity, role: WeightRole ) -> Option<RelayWeight>

Compute the weight with which a relay with ID rsa_id would be selected for a given role.

Note that weight returned by this function assumes that the relay with that ID is actually usable; if it isn’t usable, then other weight-related functions will call its weight zero.

source

pub fn known_family_members<'a>( &'a self, relay: &'a Relay<'a> ) -> impl Iterator<Item = Relay<'a>>

Return all relays in this NetDir known to be in the same family as relay.

This list of members will not necessarily include relay itself.

§Limitations

Two relays only belong to the same family if each relay claims to share a family with the other. But if we are missing a microdescriptor for one of the relays listed by this relay, we cannot know whether it acknowledges family membership with this relay or not. Therefore, this function can omit family members for which there is not (as yet) any Relay object.

source

pub fn hs_time_period(&self) -> TimePeriod

Available on crate feature hs-common only.

Return the current hidden service directory “time period”.

Specifically, this returns the time period that contains the beginning of the validity period of this NetDir’s consensus. That time period is the one we use when acting as an hidden service client.

source

pub fn hs_all_time_periods(&self) -> Vec<HsDirParams>

Available on crate feature hs-service only.

Return the HsDirParams of all the relevant hidden service directory “time periods”

This includes the current time period (as from .hs_time_period) plus additional time periods that we publish descriptors for when we are acting as a hidden service.

source

pub fn hs_dirs_download<'r, R>( &'r self, hsid: HsBlindId, period: TimePeriod, rng: &mut R ) -> Result<Vec<Relay<'r>>, Bug>
where R: Rng,

Available on crate feature hs-common only.

Return the relays in this network directory that will be used as hidden service directories

These are suitable to retrieve a given onion service’s descriptor at a given time period.

source

pub fn hs_dirs_upload( &self, hsid: HsBlindId, period: TimePeriod ) -> Result<impl Iterator<Item = Relay<'_>>, Bug>

Available on crate feature hs-service only.

Return the relays in this network directory that will be used as hidden service directories

Returns the relays that are suitable for storing a given onion service’s descriptors at the given time period.

source

pub fn hs_dirs<'r, R>( &'r self, hsid: &HsBlindId, op: HsDirOp, rng: &mut R ) -> Vec<Relay<'r>>
where R: Rng,

👎Deprecated: Use hs_dirs_upload or hs_dirs_download instead
Available on crate feature hs-common only.

Return the relays in this network directory that will be used as hidden service directories

Depending on op, these are suitable to either store, or retrieve, a given onion service’s descriptor at a given time period.

When op is Download, the order is random. When op is Upload, the order is not specified.

Return an error if the time period is not one returned by onion_service_time_period or onion_service_secondary_time_periods.

Trait Implementations§

source§

impl AsRef<NetParameters> for NetDir

source§

fn as_ref(&self) -> &NetParameters

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for NetDir

source§

fn clone(&self) -> NetDir

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for NetDir

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<NetDir> for TestNetDirProvider

Available on crate feature testing only.
source§

fn from(nd: NetDir) -> Self

Converts to this type from the input type.
source§

impl MdReceiver for NetDir

source§

fn missing_microdescs(&self) -> Box<dyn Iterator<Item = &MdDigest> + '_>

Return an iterator over the digests for all of the microdescriptors that this netdir is missing.
source§

fn add_microdesc(&mut self, md: Microdesc) -> bool

Add a microdescriptor to this netdir, if it was wanted. Read more
source§

fn n_missing(&self) -> usize

Return the number of missing microdescriptors.

Auto Trait Implementations§

§

impl Freeze for NetDir

§

impl RefUnwindSafe for NetDir

§

impl Send for NetDir

§

impl Sync for NetDir

§

impl Unpin for NetDir

§

impl UnwindSafe for NetDir

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more