tor_netdoc/doc/netstatus/rs/
each_flavor.rs1use super::*;
14use void::ResultVoidExt as _;
15
16impl RouterStatus {
17 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 pub fn bandwidth(&self) -> &RelayBandwidth {
26 &self.w.effective
27 }
28 #[deprecated = "use .bandwidth()"]
30 pub fn weight(&self) -> &RelayBandwidth {
31 self.bandwidth()
32 }
33 pub fn protovers(&self) -> &Protocols {
35 &self.protos
36 }
37 pub fn nickname(&self) -> &str {
39 self.r.nickname.as_str()
40 }
41 pub fn flags(&self) -> &RelayFlags {
43 &self.flags.known
44 }
45 pub fn version(&self) -> Option<&crate::doc::netstatus::rs::SoftwareVersion> {
47 self.version.as_ref()
48 }
49 pub fn ed25519_id_is_usable(&self) -> bool {
52 !self.flags.contains(RelayFlag::NoEdConsensus)
53 }
54 pub fn is_flagged_bad_exit(&self) -> bool {
56 self.flags.contains(RelayFlag::BadExit)
57 }
58 pub fn is_flagged_v2dir(&self) -> bool {
60 self.flags.contains(RelayFlag::V2Dir)
61 }
62 pub fn is_flagged_exit(&self) -> bool {
64 self.flags.contains(RelayFlag::Exit)
65 }
66 pub fn is_flagged_guard(&self) -> bool {
68 self.flags.contains(RelayFlag::Guard)
69 }
70 pub fn is_flagged_hsdir(&self) -> bool {
72 self.flags.contains(RelayFlag::HSDir)
73 }
74 pub fn is_flagged_stable(&self) -> bool {
76 self.flags.contains(RelayFlag::Stable)
77 }
78 pub fn is_flagged_fast(&self) -> bool {
80 self.flags.contains(RelayFlag::Fast)
81 }
82 pub fn is_flagged_middle_only(&self) -> bool {
84 self.flags.contains(RelayFlag::MiddleOnly)
85 }
86}
87
88impl RouterStatus {
89 pub fn rsa_identity(&self) -> &RsaIdentity {
91 &self.r.identity
92 }
93
94 pub(crate) fn flavor() -> ConsensusFlavor {
97 FLAVOR
98 }
99
100 pub(crate) fn from_section(sec: &Section<'_, NetstatusKwd>) -> Result<RouterStatus> {
105 use NetstatusKwd::*;
106 let r_item = sec.required(RS_R)?;
108 let nickname = r_item
109 .required_arg(0)?
110 .parse()
111 .map_err(|e: InvalidNickname| {
112 EK::BadArgument.with_msg(e.to_string()).at_pos(r_item.pos())
113 })?;
114 let ident = r_item.required_arg(1)?;
115 let identity = ident.parse::<Base64Fingerprint>()?;
116 let n_skip = match FLAVOR {
118 ConsensusFlavor::Microdesc => 0,
119 ConsensusFlavor::Plain => 1,
120 };
121 let _ignore_published: time::SystemTime = {
124 let mut p = r_item.required_arg(2 + n_skip)?.to_string();
129 p.push(' ');
130 p.push_str(r_item.required_arg(3 + n_skip)?);
131 p.parse::<Iso8601TimeSp>()?.into()
132 };
133 let ip = r_item.required_arg(4 + n_skip)?.parse::<net::Ipv4Addr>()?;
134 let or_port = r_item.required_arg(5 + n_skip)?.parse::<u16>()?;
135 let _ = r_item.required_arg(6 + n_skip)?.parse::<u16>()?;
136
137 let a_items = sec.slice(RS_A);
139 let a = a_items
140 .iter()
141 .map(|a_item| Ok(a_item.required_arg(0)?.parse::<net::SocketAddr>()?))
142 .collect::<Result<Vec<_>>>()?;
143
144 let flags = DocRelayFlags::from_item_consensus(sec.required(RS_S)?)?;
148
149 let version = sec
151 .maybe(RS_V)
152 .args_as_str()
153 .map(str::parse)
154 .transpose()
155 .void_unwrap();
156
157 let protos = {
159 let tok = sec.required(RS_PR)?;
160 Protocols::from_str_c_compatible(tok.args_as_str())
161 .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?
162 };
163
164 let weight = sec
166 .get(RS_W)
167 .map(RelayBandwidthItem::from_item)
168 .transpose()?
169 .unwrap_or_else(RelayBandwidthItem::new_no_info);
170
171 let doc_digest: DocDigest = match FLAVOR {
177 ConsensusFlavor::Microdesc => {
178 let m_item = sec.required(RS_M)?;
180 DocDigest::decode(m_item.required_arg(0)?)?
181 }
182 ConsensusFlavor::Plain => DocDigest::decode(r_item.required_arg(2)?)?,
183 };
184
185 ns_choose! { (
186 let r_doc_digest = doc_digest;
187 let m_doc_digest = NotPresent;
188 ) (
189 let r_doc_digest = NotPresent;
190 let m_doc_digest = doc_digest;
191 ) (
192 let r_doc_digest = doc_digest;
193 let m_doc_digest = NotPresent;
194 ) };
195
196 #[allow(clippy::useless_conversion)] Ok(RouterStatus {
198 r: RouterStatusIntroItem {
199 nickname,
200 identity,
201 or_port,
202 doc_digest: r_doc_digest.into(),
203 publication: IgnoredPublicationTimeSp,
204 ip,
205 dir_port: 0,
206 __non_exhaustive: (),
207 },
208 m: m_doc_digest.into(),
209 a,
210 flags,
211 version,
212 protos,
213 w: weight,
214 port_policy: ns_expr!(None, NotPresent, Void {}),
215 ed25519_id: NotPresent,
216 stats: NotPresent,
217 __non_exhaustive: (),
218 })
219 }
220}
221
222impl FromRsString for DocDigest {
223 fn decode(s: &str) -> Result<DocDigest> {
224 s.parse::<B64>()?
225 .check_len(DOC_DIGEST_LEN..=DOC_DIGEST_LEN)?
226 .as_bytes()
227 .try_into()
228 .map_err(|_| Error::from(internal!("correct length on digest, but unable to convert")))
229 }
230}