use super::*;
impl RouterStatus {
pub fn addrs(&self) -> impl Iterator<Item = net::SocketAddr> {
chain!(
[std::net::SocketAddrV4::new(self.r.ip, self.r.or_port).into()],
self.a.iter().copied(),
)
}
pub fn weight(&self) -> &RelayWeight {
&self.weight
}
pub fn protovers(&self) -> &Protocols {
&self.protos
}
pub fn nickname(&self) -> &str {
self.r.nickname.as_str()
}
pub fn flags(&self) -> &RelayFlags {
&self.flags.known
}
pub fn version(&self) -> Option<&crate::doc::netstatus::rs::SoftwareVersion> {
self.version.as_ref()
}
pub fn ed25519_id_is_usable(&self) -> bool {
!self.flags.contains(RelayFlag::NoEdConsensus)
}
pub fn is_flagged_bad_exit(&self) -> bool {
self.flags.contains(RelayFlag::BadExit)
}
pub fn is_flagged_v2dir(&self) -> bool {
self.flags.contains(RelayFlag::V2Dir)
}
pub fn is_flagged_exit(&self) -> bool {
self.flags.contains(RelayFlag::Exit)
}
pub fn is_flagged_guard(&self) -> bool {
self.flags.contains(RelayFlag::Guard)
}
pub fn is_flagged_hsdir(&self) -> bool {
self.flags.contains(RelayFlag::HSDir)
}
pub fn is_flagged_stable(&self) -> bool {
self.flags.contains(RelayFlag::Stable)
}
pub fn is_flagged_fast(&self) -> bool {
self.flags.contains(RelayFlag::Fast)
}
pub fn is_flagged_middle_only(&self) -> bool {
self.flags.contains(RelayFlag::MiddleOnly)
}
}
impl RouterStatus {
pub fn rsa_identity(&self) -> &RsaIdentity {
&self.r.identity
}
pub(crate) fn flavor() -> ConsensusFlavor {
FLAVOR
}
pub(crate) fn from_section(sec: &Section<'_, NetstatusKwd>) -> Result<RouterStatus> {
use NetstatusKwd::*;
let r_item = sec.required(RS_R)?;
let nickname = r_item.required_arg(0)?.parse()?;
let ident = r_item.required_arg(1)?;
let identity = ident.parse::<Base64Fingerprint>()?;
let n_skip = match FLAVOR {
ConsensusFlavor::Microdesc => 0,
ConsensusFlavor::Plain => 1,
};
let _ignore_published: time::SystemTime = {
let mut p = r_item.required_arg(2 + n_skip)?.to_string();
p.push(' ');
p.push_str(r_item.required_arg(3 + n_skip)?);
p.parse::<Iso8601TimeSp>()?.into()
};
let ip = r_item.required_arg(4 + n_skip)?.parse::<net::Ipv4Addr>()?;
let or_port = r_item.required_arg(5 + n_skip)?.parse::<u16>()?;
let _ = r_item.required_arg(6 + n_skip)?.parse::<u16>()?;
let a_items = sec.slice(RS_A);
let a = a_items
.iter()
.map(|a_item| Ok(a_item.required_arg(0)?.parse::<net::SocketAddr>()?))
.collect::<Result<Vec<_>>>()?;
let flags = DocRelayFlags::from_item_consensus(sec.required(RS_S)?)?;
let version = sec.maybe(RS_V).args_as_str().map(str::parse).transpose()?;
let protos = {
let tok = sec.required(RS_PR)?;
tok.args_as_str()
.parse::<Protocols>()
.map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?
};
let weight = sec
.get(RS_W)
.map(RelayWeight::from_item)
.transpose()?
.unwrap_or_default();
let doc_digest: DocDigest = match FLAVOR {
ConsensusFlavor::Microdesc => {
let m_item = sec.required(RS_M)?;
DocDigest::decode(m_item.required_arg(0)?)?
}
ConsensusFlavor::Plain => DocDigest::decode(r_item.required_arg(2)?)?,
};
ns_choose! { (
let r_doc_digest = doc_digest;
let m_doc_digest = NotPresent;
) (
let r_doc_digest = NotPresent;
let m_doc_digest = doc_digest;
) (
let r_doc_digest = doc_digest;
let m_doc_digest = NotPresent;
) };
Ok(RouterStatus {
r: RouterStatusIntroItem {
nickname,
identity,
or_port,
doc_digest: r_doc_digest,
publication: IgnoredPublicationTimeSp,
ip,
},
m: m_doc_digest,
a,
flags,
version,
protos,
weight,
})
}
}
impl FromRsString for DocDigest {
fn decode(s: &str) -> Result<DocDigest> {
s.parse::<B64>()?
.check_len(DOC_DIGEST_LEN..=DOC_DIGEST_LEN)?
.as_bytes()
.try_into()
.map_err(|_| Error::from(internal!("correct length on digest, but unable to convert")))
}
}