Skip to main content

tor_netdoc/doc/netstatus/rs/
each_flavor.rs

1//! router status entries - items that vary by consensus flavor
2//!
3//! **This file is reincluded multiple times**,
4//! by the macros in [`crate::doc::ns_variety_definition_macros`],
5//! once for votes, and once for each consensus flavour.
6//! It is *not* a module `crate::doc::netstatus::rs::each_flavor`.
7//!
8//! Each time this file is included by one of the macros mentioned above,
9//! the `ns_***` macros (such as `ns_const_name!`) may expand to different values.
10//!
11//! See [`crate::doc::ns_variety_definition_macros`].
12
13use super::*;
14use void::ResultVoidExt as _;
15
16impl RouterStatus {
17    /// Return an iterator of ORPort addresses for this routerstatus
18    pub fn addrs(&self) -> impl Iterator<Item = net::SocketAddr> {
19        chain!(
20            [std::net::SocketAddrV4::new(self.r.ip, self.r.or_port).into()],
21            self.a.iter().copied(),
22        )
23    }
24    /// Return the declared weight of this routerstatus in the directory.
25    pub fn weight(&self) -> &RelayWeight {
26        &self.weight.effective
27    }
28    /// Return the protovers that this routerstatus says it implements.
29    pub fn protovers(&self) -> &Protocols {
30        &self.protos
31    }
32    /// Return the nickname of this routerstatus.
33    pub fn nickname(&self) -> &str {
34        self.r.nickname.as_str()
35    }
36    /// Return the relay flags of this routerstatus.
37    pub fn flags(&self) -> &RelayFlags {
38        &self.flags.known
39    }
40    /// Return the version of this routerstatus.
41    pub fn version(&self) -> Option<&crate::doc::netstatus::rs::SoftwareVersion> {
42        self.version.as_ref()
43    }
44    /// Return true if the ed25519 identity on this relay reflects a
45    /// true consensus among the authorities.
46    pub fn ed25519_id_is_usable(&self) -> bool {
47        !self.flags.contains(RelayFlag::NoEdConsensus)
48    }
49    /// Return true if this routerstatus is listed with the BadExit flag.
50    pub fn is_flagged_bad_exit(&self) -> bool {
51        self.flags.contains(RelayFlag::BadExit)
52    }
53    /// Return true if this routerstatus is listed with the v2dir flag.
54    pub fn is_flagged_v2dir(&self) -> bool {
55        self.flags.contains(RelayFlag::V2Dir)
56    }
57    /// Return true if this routerstatus is listed with the Exit flag.
58    pub fn is_flagged_exit(&self) -> bool {
59        self.flags.contains(RelayFlag::Exit)
60    }
61    /// Return true if this routerstatus is listed with the Guard flag.
62    pub fn is_flagged_guard(&self) -> bool {
63        self.flags.contains(RelayFlag::Guard)
64    }
65    /// Return true if this routerstatus is listed with the HSDir flag.
66    pub fn is_flagged_hsdir(&self) -> bool {
67        self.flags.contains(RelayFlag::HSDir)
68    }
69    /// Return true if this routerstatus is listed with the Stable flag.
70    pub fn is_flagged_stable(&self) -> bool {
71        self.flags.contains(RelayFlag::Stable)
72    }
73    /// Return true if this routerstatus is listed with the Fast flag.
74    pub fn is_flagged_fast(&self) -> bool {
75        self.flags.contains(RelayFlag::Fast)
76    }
77    /// Return true if this routerstatus is listed with the MiddleOnly flag.
78    pub fn is_flagged_middle_only(&self) -> bool {
79        self.flags.contains(RelayFlag::MiddleOnly)
80    }
81}
82
83impl RouterStatus {
84    /// Return RSA identity for the relay described by this RouterStatus
85    pub fn rsa_identity(&self) -> &RsaIdentity {
86        &self.r.identity
87    }
88
89    /// Return the networkstatus consensus flavor in which this
90    /// routerstatus appears.
91    pub(crate) fn flavor() -> ConsensusFlavor {
92        FLAVOR
93    }
94
95    /// Parse a generic routerstatus from a section.
96    ///
97    /// Requires that the section obeys the right SectionRules,
98    /// matching `consensus_flavor`.
99    pub(crate) fn from_section(sec: &Section<'_, NetstatusKwd>) -> Result<RouterStatus> {
100        use NetstatusKwd::*;
101        // R line
102        let r_item = sec.required(RS_R)?;
103        let nickname = r_item
104            .required_arg(0)?
105            .parse()
106            .map_err(|e: InvalidNickname| {
107                EK::BadArgument.with_msg(e.to_string()).at_pos(r_item.pos())
108            })?;
109        let ident = r_item.required_arg(1)?;
110        let identity = ident.parse::<Base64Fingerprint>()?;
111        // Fields to skip in the "r" line.
112        let n_skip = match FLAVOR {
113            ConsensusFlavor::Microdesc => 0,
114            ConsensusFlavor::Plain => 1,
115        };
116        // We check that the published time is well-formed, but we never use it
117        // for anything in a consensus document.
118        let _ignore_published: time::SystemTime = {
119            // TODO: It's annoying to have to do this allocation, since we
120            // already have a slice that contains both of these arguments.
121            // Instead, we could get a slice of arguments: we'd have to add
122            // a feature for that.
123            let mut p = r_item.required_arg(2 + n_skip)?.to_string();
124            p.push(' ');
125            p.push_str(r_item.required_arg(3 + n_skip)?);
126            p.parse::<Iso8601TimeSp>()?.into()
127        };
128        let ip = r_item.required_arg(4 + n_skip)?.parse::<net::Ipv4Addr>()?;
129        let or_port = r_item.required_arg(5 + n_skip)?.parse::<u16>()?;
130        let _ = r_item.required_arg(6 + n_skip)?.parse::<u16>()?;
131
132        // main address and A lines.
133        let a_items = sec.slice(RS_A);
134        let a = a_items
135            .iter()
136            .map(|a_item| Ok(a_item.required_arg(0)?.parse::<net::SocketAddr>()?))
137            .collect::<Result<Vec<_>>>()?;
138
139        // S line
140        //
141        // Wrong for votes, but this code doesn't run for votes.
142        let flags = DocRelayFlags::from_item_consensus(sec.required(RS_S)?)?;
143
144        // V line
145        let version = sec
146            .maybe(RS_V)
147            .args_as_str()
148            .map(str::parse)
149            .transpose()
150            .void_unwrap();
151
152        // PR line
153        let protos = {
154            let tok = sec.required(RS_PR)?;
155            Protocols::from_str_c_compatible(tok.args_as_str())
156                .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?
157        };
158
159        // W line
160        let weight = sec
161            .get(RS_W)
162            .map(RelayWeightsItem::from_item)
163            .transpose()?
164            .unwrap_or(RelayWeightsItem::new_no_info());
165
166        // No p line
167        // no ID line
168
169        // Try to find the document digest.  This is in different
170        // places depending on the kind of consensus we're in.
171        let doc_digest: DocDigest = match FLAVOR {
172            ConsensusFlavor::Microdesc => {
173                // M line
174                let m_item = sec.required(RS_M)?;
175                DocDigest::decode(m_item.required_arg(0)?)?
176            }
177            ConsensusFlavor::Plain => DocDigest::decode(r_item.required_arg(2)?)?,
178        };
179
180        ns_choose! { (
181            let r_doc_digest = doc_digest;
182            let m_doc_digest = NotPresent;
183        ) (
184            let r_doc_digest = NotPresent;
185            let m_doc_digest = doc_digest;
186        ) (
187            let r_doc_digest = doc_digest;
188            let m_doc_digest = NotPresent;
189        ) };
190
191        #[allow(clippy::useless_conversion)] // sometimes doc_digest needs into
192        Ok(RouterStatus {
193            r: RouterStatusIntroItem {
194                nickname,
195                identity,
196                or_port,
197                doc_digest: r_doc_digest.into(),
198                publication: IgnoredPublicationTimeSp,
199                ip,
200                dir_port: 0,
201                __non_exhaustive: (),
202            },
203            m: m_doc_digest.into(),
204            a,
205            flags,
206            version,
207            protos,
208            weight,
209            port_policy: ns_expr!(None, NotPresent, Void {}),
210            ed25519_id: NotPresent,
211            stats: NotPresent,
212            __non_exhaustive: (),
213        })
214    }
215}
216
217impl FromRsString for DocDigest {
218    fn decode(s: &str) -> Result<DocDigest> {
219        s.parse::<B64>()?
220            .check_len(DOC_DIGEST_LEN..=DOC_DIGEST_LEN)?
221            .as_bytes()
222            .try_into()
223            .map_err(|_| Error::from(internal!("correct length on digest, but unable to convert")))
224    }
225}