#[cfg(feature = "build_docs")]
pub(crate) mod build;
mod md;
#[cfg(feature = "ns_consensus")]
mod ns;
use super::{NetstatusKwd, RelayFlags, RelayWeight};
use crate::doc;
use crate::parse::parser::Section;
use crate::types::misc::*;
use crate::types::version::TorVersion;
use crate::util::intern::InternCache;
use crate::{Error, ParseErrorKind as EK, Result};
use std::sync::Arc;
use std::{net, time};
use tor_llcrypto::pk::rsa::RsaIdentity;
use tor_protover::Protocols;
pub use md::MdConsensusRouterStatus;
#[cfg(feature = "ns_consensus")]
pub use ns::NsConsensusRouterStatus;
#[cfg_attr(
feature = "dangerous-expose-struct-fields",
visible::StructFields(pub),
visibility::make(pub),
non_exhaustive
)]
#[derive(Debug, Clone)]
struct GenericRouterStatus<D> {
nickname: Nickname,
identity: RsaIdentity,
addrs: Vec<net::SocketAddr>,
doc_digest: D,
flags: RelayFlags,
version: Option<Version>,
protos: Arc<Protocols>,
weight: RelayWeight,
}
#[derive(Clone, Debug, Eq, PartialEq, Hash, derive_more::Display)]
#[non_exhaustive]
pub enum Version {
Tor(TorVersion),
Other(Arc<str>),
}
static OTHER_VERSION_CACHE: InternCache<str> = InternCache::new();
impl std::str::FromStr for Version {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let mut elts = s.splitn(3, ' ');
if elts.next() == Some("Tor") {
if let Some(Ok(v)) = elts.next().map(str::parse) {
return Ok(Version::Tor(v));
}
}
Ok(Version::Other(OTHER_VERSION_CACHE.intern_ref(s)))
}
}
macro_rules! implement_accessors {
($name:ident) => {
impl $name {
pub fn orport_addrs(&self) -> impl Iterator<Item = &net::SocketAddr> {
self.rs.addrs.iter()
}
pub fn weight(&self) -> &RelayWeight {
&self.rs.weight
}
pub fn addrs(&self) -> &[net::SocketAddr] {
&self.rs.addrs[..]
}
pub fn protovers(&self) -> &Protocols {
&self.rs.protos
}
pub fn nickname(&self) -> &str {
self.rs.nickname.as_str()
}
pub fn flags(&self) -> &RelayFlags {
&self.rs.flags
}
pub fn version(&self) -> Option<&crate::doc::netstatus::rs::Version> {
self.rs.version.as_ref()
}
pub fn ed25519_id_is_usable(&self) -> bool {
!self.rs.flags.contains(RelayFlags::NO_ED_CONSENSUS)
}
pub fn is_flagged_bad_exit(&self) -> bool {
self.rs.flags.contains(RelayFlags::BAD_EXIT)
}
pub fn is_flagged_v2dir(&self) -> bool {
self.rs.flags.contains(RelayFlags::V2DIR)
}
pub fn is_flagged_exit(&self) -> bool {
self.rs.flags.contains(RelayFlags::EXIT)
}
pub fn is_flagged_guard(&self) -> bool {
self.rs.flags.contains(RelayFlags::GUARD)
}
}
};
}
pub(crate) use implement_accessors;
#[cfg_attr(feature = "dangerous-expose-struct-fields", visibility::make(pub))]
trait FromRsString: Sized {
fn decode(s: &str) -> Result<Self>;
}
impl<D> GenericRouterStatus<D>
where
D: FromRsString,
{
fn from_section(
sec: &Section<'_, NetstatusKwd>,
microdesc_format: bool,
) -> Result<GenericRouterStatus<D>> {
use NetstatusKwd::*;
let r_item = sec.required(RS_R)?;
let nickname = r_item.required_arg(0)?.parse()?;
let ident = r_item.required_arg(1)?.parse::<B64>()?;
let identity = RsaIdentity::from_bytes(ident.as_bytes()).ok_or_else(|| {
EK::BadArgument
.at_pos(r_item.pos())
.with_msg("Wrong identity length")
})?;
let skip = if microdesc_format { 0 } else { 1 };
let _ignore_published: time::SystemTime = {
let mut p = r_item.required_arg(2 + skip)?.to_string();
p.push(' ');
p.push_str(r_item.required_arg(3 + skip)?);
p.parse::<Iso8601TimeSp>()?.into()
};
let ipv4addr = r_item.required_arg(4 + skip)?.parse::<net::Ipv4Addr>()?;
let or_port = r_item.required_arg(5 + skip)?.parse::<u16>()?;
let _ = r_item.required_arg(6 + skip)?.parse::<u16>()?;
let a_items = sec.slice(RS_A);
let mut addrs = Vec::with_capacity(1 + a_items.len());
addrs.push(net::SocketAddr::V4(net::SocketAddrV4::new(
ipv4addr, or_port,
)));
for a_item in a_items {
addrs.push(a_item.required_arg(0)?.parse::<net::SocketAddr>()?);
}
let flags = RelayFlags::from_item(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)?;
doc::PROTOVERS_CACHE.intern(
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: D = if microdesc_format {
let m_item = sec.required(RS_M)?;
D::decode(m_item.required_arg(0)?)?
} else {
D::decode(r_item.required_arg(2)?)?
};
Ok(GenericRouterStatus {
nickname,
identity,
addrs,
doc_digest,
flags,
version,
protos,
weight,
})
}
}