Skip to main content

tor_netdoc/doc/netstatus/
rs.rs

1//! Routerstatus-specific parts of networkstatus parsing.
2//!
3//! This is a private module; relevant pieces are re-exported by its
4//! parent.
5
6#[cfg(feature = "build_docs")]
7pub(crate) mod build;
8pub(crate) mod md;
9pub(crate) mod plain;
10#[cfg(feature = "incomplete")]
11pub(crate) mod vote;
12
13use super::{ConsensusFlavor, ConsensusMethods, consensus_methods_comma_separated};
14use crate::doc::netstatus::NetstatusKwd;
15use crate::doc::netstatus::{IgnoredPublicationTimeSp, Protocols, RelayWeight, RelayWeightsItem};
16use crate::encode::ItemEncoder;
17use crate::parse::parser::Section;
18use crate::parse2::ItemArgumentParseable;
19use crate::types::misc::*;
20use crate::types::relay_flags::{self, DocRelayFlags, RelayFlag, RelayFlags};
21use crate::types::version::TorVersion;
22use crate::{Error, NetdocErrorKind as EK, Result};
23use derive_deftly::Deftly;
24use itertools::chain;
25use std::sync::Arc;
26use std::{net, time};
27use tor_basic_utils::intern::InternCache;
28use tor_error::{Bug, internal};
29use tor_llcrypto::pk::rsa::RsaIdentity;
30
31/// A version as presented in a router status.
32///
33/// This can either be a parsed Tor version, or an unparsed string.
34//
35// TODO: This might want to merge, at some point, with routerdesc::RelayPlatform.
36#[derive(Clone, Debug, Eq, PartialEq, Hash, derive_more::Display)]
37#[non_exhaustive]
38pub enum SoftwareVersion {
39    /// A Tor version
40    CTor(TorVersion),
41    /// A string we couldn't parse.
42    Other(Arc<str>),
43}
44
45/// A cache of unparsable version strings.
46///
47/// We use this because we expect there not to be very many distinct versions of
48/// relay software in existence.
49static OTHER_VERSION_CACHE: InternCache<str> = InternCache::new();
50
51/// `m` item in votes
52///
53/// <https://spec.torproject.org/dir-spec/consensus-formats.html#item:m>
54///
55/// This is different to the `m` line in microdesc consensuses.
56/// Plain consensuses don't have `m` lines at all.
57///
58/// ### Non-invariants
59///
60///  * There may be overlapping or even contradictory information.
61///  * It might not be sorted.
62///    Users of the structure who need to emit reproducible document encodings.
63///    must sort it.
64///  * These non-invariants apply both within one instance of this struct,
65///    and across multiple instances of it within a `RouterStatus`.
66#[derive(Debug, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Deftly)]
67#[derive_deftly(ItemValueParseable)]
68#[cfg_attr(feature = "incomplete", derive_deftly(ItemValueEncodable))] // untested
69#[non_exhaustive]
70pub struct RouterStatusMdDigestsVote {
71    /// The methods for which this document is applicable.
72    #[deftly(netdoc(with = consensus_methods_comma_separated))]
73    pub consensus_methods: ConsensusMethods,
74
75    /// The various hashes of this document.
76    pub digests: Vec<IdentifiedDigest>,
77}
78
79impl std::str::FromStr for SoftwareVersion {
80    type Err = Error;
81
82    fn from_str(s: &str) -> Result<Self> {
83        let mut elts = s.splitn(3, ' ');
84        if elts.next() == Some("Tor") {
85            if let Some(Ok(v)) = elts.next().map(str::parse) {
86                return Ok(SoftwareVersion::CTor(v));
87            }
88        }
89
90        Ok(SoftwareVersion::Other(OTHER_VERSION_CACHE.intern_ref(s)))
91    }
92}
93
94/// Helper to decode a document digest in the format in which it
95/// appears in a given kind of routerstatus.
96trait FromRsString: Sized {
97    /// Try to decode the given object.
98    fn decode(s: &str) -> Result<Self>;
99}