tor_netdoc/doc/netstatus/rs/
each_flavor.rs1use super::*;
14
15impl RouterStatus {
16 pub fn addrs(&self) -> impl Iterator<Item = net::SocketAddr> {
18 chain!(
19 [std::net::SocketAddrV4::new(self.r.ip, self.r.or_port).into()],
20 self.a.iter().copied(),
21 )
22 }
23 pub fn weight(&self) -> &RelayWeight {
25 &self.weight
26 }
27 pub fn protovers(&self) -> &Protocols {
29 &self.protos
30 }
31 pub fn nickname(&self) -> &str {
33 self.r.nickname.as_str()
34 }
35 pub fn flags(&self) -> &RelayFlags {
37 &self.flags
38 }
39 pub fn version(&self) -> Option<&crate::doc::netstatus::rs::Version> {
41 self.version.as_ref()
42 }
43 pub fn ed25519_id_is_usable(&self) -> bool {
46 !self.flags.contains(RelayFlags::NO_ED_CONSENSUS)
47 }
48 pub fn is_flagged_bad_exit(&self) -> bool {
50 self.flags.contains(RelayFlags::BAD_EXIT)
51 }
52 pub fn is_flagged_v2dir(&self) -> bool {
54 self.flags.contains(RelayFlags::V2DIR)
55 }
56 pub fn is_flagged_exit(&self) -> bool {
58 self.flags.contains(RelayFlags::EXIT)
59 }
60 pub fn is_flagged_guard(&self) -> bool {
62 self.flags.contains(RelayFlags::GUARD)
63 }
64 pub fn is_flagged_hsdir(&self) -> bool {
66 self.flags.contains(RelayFlags::HSDIR)
67 }
68 pub fn is_flagged_stable(&self) -> bool {
70 self.flags.contains(RelayFlags::STABLE)
71 }
72 pub fn is_flagged_fast(&self) -> bool {
74 self.flags.contains(RelayFlags::FAST)
75 }
76 pub fn is_flagged_middle_only(&self) -> bool {
78 self.flags.contains(RelayFlags::MIDDLE_ONLY)
79 }
80}
81
82impl RouterStatus {
83 pub fn rsa_identity(&self) -> &RsaIdentity {
85 &self.r.identity
86 }
87
88 pub(crate) fn flavor() -> ConsensusFlavor {
91 FLAVOR
92 }
93
94 pub(crate) fn from_section(
99 sec: &Section<'_, NetstatusKwd>,
100 ) -> Result<RouterStatus> {
101 use NetstatusKwd::*;
102 let r_item = sec.required(RS_R)?;
104 let nickname = r_item.required_arg(0)?.parse()?;
105 let ident = r_item.required_arg(1)?;
106 let identity = ident.parse::<Base64Fingerprint>()?;
107 let n_skip = match FLAVOR {
109 ConsensusFlavor::Microdesc => 0,
110 ConsensusFlavor::Plain => 1,
111 };
112 let _ignore_published: time::SystemTime = {
115 let mut p = r_item.required_arg(2 + n_skip)?.to_string();
120 p.push(' ');
121 p.push_str(r_item.required_arg(3 + n_skip)?);
122 p.parse::<Iso8601TimeSp>()?.into()
123 };
124 let ip = r_item.required_arg(4 + n_skip)?.parse::<net::Ipv4Addr>()?;
125 let or_port = r_item.required_arg(5 + n_skip)?.parse::<u16>()?;
126 let _ = r_item.required_arg(6 + n_skip)?.parse::<u16>()?;
127
128 let a_items = sec.slice(RS_A);
130 let a = a_items.iter().map(|a_item| {
131 Ok(a_item.required_arg(0)?.parse::<net::SocketAddr>()?)
132 }).collect::<Result<Vec<_>>>()?;
133
134 let flags = RelayFlags::from_item(sec.required(RS_S)?)?;
136
137 let version = sec.maybe(RS_V).args_as_str().map(str::parse).transpose()?;
139
140 let protos = {
142 let tok = sec.required(RS_PR)?;
143 doc::PROTOVERS_CACHE.intern(
144 tok.args_as_str()
145 .parse::<Protocols>()
146 .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?,
147 )
148 };
149
150 let weight = sec
152 .get(RS_W)
153 .map(RelayWeight::from_item)
154 .transpose()?
155 .unwrap_or_default();
156
157 let doc_digest: DocDigest = match FLAVOR {
163 ConsensusFlavor::Microdesc => {
164 let m_item = sec.required(RS_M)?;
166 DocDigest::decode(m_item.required_arg(0)?)?
167 }
168 ConsensusFlavor::Plain => DocDigest::decode(r_item.required_arg(2)?)?,
169 };
170
171 ns_choose! { (
172 let r_doc_digest = doc_digest;
173 let m_doc_digest = NotPresent;
174 ) (
175 let r_doc_digest = NotPresent;
176 let m_doc_digest = doc_digest;
177 ) (
178 let r_doc_digest = doc_digest;
179 let m_doc_digest = NotPresent;
180 ) };
181
182 Ok(RouterStatus {
183 r: RouterStatusIntroItem {
184 nickname,
185 identity,
186 or_port,
187 doc_digest: r_doc_digest,
188 publication: IgnoredPublicationTimeSp,
189 ip,
190 },
191 m: m_doc_digest,
192 a,
193 flags,
194 version,
195 protos,
196 weight,
197 })
198 }
199}
200
201impl FromRsString for DocDigest {
202 fn decode(s: &str) -> Result<DocDigest> {
203 s.parse::<B64>()?
204 .check_len(DOC_DIGEST_LEN..=DOC_DIGEST_LEN)?
205 .as_bytes()
206 .try_into()
207 .map_err(|_| Error::from(internal!("correct length on digest, but unable to convert")))
208 }
209}