removing dev branch, many changes
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
use super::*;
|
||||
use core::sync::atomic::{AtomicU32, Ordering};
|
||||
use rkyv::{
|
||||
with::Skip, Archive as RkyvArchive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize,
|
||||
};
|
||||
|
||||
|
||||
/// Reliable pings are done with increased spacing between pings
|
||||
|
||||
@@ -73,7 +71,9 @@ pub struct BucketEntryLocalNetwork {
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct BucketEntryInner {
|
||||
/// The node ids matching this bucket entry, with the cryptography versions supported by this node as the 'kind' field
|
||||
node_ids: TypedKeySet,
|
||||
validated_node_ids: TypedKeySet,
|
||||
/// The node ids claimed by the remote node that use cryptography versions we do not support
|
||||
unsupported_node_ids: TypedKeySet,
|
||||
/// The set of envelope versions supported by the node inclusive of the requirements of any relay the node may be using
|
||||
envelope_support: Vec<u8>,
|
||||
/// If this node has updated it's SignedNodeInfo since our network
|
||||
@@ -122,9 +122,11 @@ impl BucketEntryInner {
|
||||
self.node_ref_tracks.remove(&track_id);
|
||||
}
|
||||
|
||||
/// Get node ids
|
||||
/// Get all node ids
|
||||
pub fn node_ids(&self) -> TypedKeySet {
|
||||
self.node_ids.clone()
|
||||
let mut node_ids = self.validated_node_ids.clone();
|
||||
node_ids.add_all(&self.unsupported_node_ids);
|
||||
node_ids
|
||||
}
|
||||
|
||||
/// Add a node id for a particular crypto kind.
|
||||
@@ -132,33 +134,40 @@ impl BucketEntryInner {
|
||||
/// Returns Ok(None) if no previous existing node id was associated with that crypto kind
|
||||
/// Results Err() if this operation would add more crypto kinds than we support
|
||||
pub fn add_node_id(&mut self, node_id: TypedKey) -> EyreResult<Option<TypedKey>> {
|
||||
if let Some(old_node_id) = self.node_ids.get(node_id.kind) {
|
||||
let total_node_id_count = self.validated_node_ids.len() + self.unsupported_node_ids.len();
|
||||
let node_ids = if VALID_CRYPTO_KINDS.contains(&node_id.kind) {
|
||||
&mut self.validated_node_ids
|
||||
} else {
|
||||
&mut self.unsupported_node_ids
|
||||
};
|
||||
|
||||
if let Some(old_node_id) = node_ids.get(node_id.kind) {
|
||||
// If this was already there we do nothing
|
||||
if old_node_id == node_id {
|
||||
return Ok(None);
|
||||
}
|
||||
// Won't change number of crypto kinds
|
||||
self.node_ids.add(node_id);
|
||||
node_ids.add(node_id);
|
||||
return Ok(Some(old_node_id));
|
||||
}
|
||||
// Check to ensure we aren't adding more crypto kinds than we support
|
||||
if self.node_ids.len() == MAX_CRYPTO_KINDS {
|
||||
if total_node_id_count == MAX_CRYPTO_KINDS {
|
||||
bail!("too many crypto kinds for this node");
|
||||
}
|
||||
self.node_ids.add(node_id);
|
||||
node_ids.add(node_id);
|
||||
Ok(None)
|
||||
}
|
||||
pub fn best_node_id(&self) -> TypedKey {
|
||||
self.node_ids.best().unwrap()
|
||||
self.validated_node_ids.best().unwrap()
|
||||
}
|
||||
|
||||
/// Get crypto kinds
|
||||
pub fn crypto_kinds(&self) -> Vec<CryptoKind> {
|
||||
self.node_ids.kinds()
|
||||
self.validated_node_ids.kinds()
|
||||
}
|
||||
/// Compare sets of crypto kinds
|
||||
pub fn common_crypto_kinds(&self, other: &[CryptoKind]) -> Vec<CryptoKind> {
|
||||
common_crypto_kinds(&self.node_ids.kinds(), other)
|
||||
common_crypto_kinds(&self.validated_node_ids.kinds(), other)
|
||||
}
|
||||
|
||||
|
||||
@@ -270,7 +279,7 @@ impl BucketEntryInner {
|
||||
}
|
||||
|
||||
// Update the envelope version support we have to use
|
||||
let envelope_support = signed_node_info.node_info().envelope_support.clone();
|
||||
let envelope_support = signed_node_info.node_info().envelope_support().to_vec();
|
||||
|
||||
// Update the signed node info
|
||||
*opt_current_sni = Some(Box::new(signed_node_info));
|
||||
@@ -333,10 +342,12 @@ impl BucketEntryInner {
|
||||
RoutingDomain::LocalNetwork => &self.local_network.signed_node_info,
|
||||
RoutingDomain::PublicInternet => &self.public_internet.signed_node_info,
|
||||
};
|
||||
opt_current_sni.as_ref().map(|s| PeerInfo {
|
||||
node_ids: self.node_ids.clone(),
|
||||
signed_node_info: *s.clone(),
|
||||
})
|
||||
// Peer info includes all node ids, even unvalidated ones
|
||||
let node_ids = self.node_ids();
|
||||
opt_current_sni.as_ref().map(|s| PeerInfo::new(
|
||||
node_ids,
|
||||
*s.clone(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn best_routing_domain(
|
||||
@@ -527,7 +538,7 @@ impl BucketEntryInner {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_our_node_info_ts(&mut self, routing_domain: RoutingDomain, seen_ts: Timestamp) {
|
||||
pub fn set_seen_our_node_info_ts(&mut self, routing_domain: RoutingDomain, seen_ts: Timestamp) {
|
||||
match routing_domain {
|
||||
RoutingDomain::LocalNetwork => {
|
||||
self.local_network.last_seen_our_node_info_ts = seen_ts;
|
||||
@@ -780,12 +791,14 @@ pub struct BucketEntry {
|
||||
|
||||
impl BucketEntry {
|
||||
pub(super) fn new(first_node_id: TypedKey) -> Self {
|
||||
let now = get_aligned_timestamp();
|
||||
let mut node_ids = TypedKeySet::new();
|
||||
node_ids.add(first_node_id);
|
||||
|
||||
// First node id should always be one we support since TypedKeySets are sorted and we must have at least one supported key
|
||||
assert!(VALID_CRYPTO_KINDS.contains(&first_node_id.kind));
|
||||
|
||||
let now = get_aligned_timestamp();
|
||||
let inner = BucketEntryInner {
|
||||
node_ids,
|
||||
validated_node_ids: TypedKeySet::from(first_node_id),
|
||||
unsupported_node_ids: TypedKeySet::new(),
|
||||
envelope_support: Vec::new(),
|
||||
updated_since_last_network_change: false,
|
||||
last_connections: BTreeMap::new(),
|
||||
|
||||
@@ -2,28 +2,6 @@ use super::*;
|
||||
use routing_table::tasks::bootstrap::BOOTSTRAP_TXT_VERSION_0;
|
||||
|
||||
impl RoutingTable {
|
||||
pub(crate) fn debug_info_nodeinfo(&self) -> String {
|
||||
let mut out = String::new();
|
||||
let inner = self.inner.read();
|
||||
out += "Routing Table Info:\n";
|
||||
|
||||
out += &format!(" Node Ids: {}\n", self.unlocked_inner.node_ids());
|
||||
out += &format!(
|
||||
" Self Latency Stats Accounting: {:#?}\n\n",
|
||||
inner.self_latency_stats_accounting
|
||||
);
|
||||
out += &format!(
|
||||
" Self Transfer Stats Accounting: {:#?}\n\n",
|
||||
inner.self_transfer_stats_accounting
|
||||
);
|
||||
out += &format!(
|
||||
" Self Transfer Stats: {:#?}\n\n",
|
||||
inner.self_transfer_stats
|
||||
);
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) async fn debug_info_txtrecord(&self) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
@@ -71,14 +49,34 @@ impl RoutingTable {
|
||||
node_ids,
|
||||
some_hostname.unwrap()
|
||||
);
|
||||
for short_url in short_urls {
|
||||
out += &format!(",{}", short_url);
|
||||
}
|
||||
out += &short_urls.join(",");
|
||||
out += "\n";
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn debug_info_nodeinfo(&self) -> String {
|
||||
let mut out = String::new();
|
||||
let inner = self.inner.read();
|
||||
out += "Routing Table Info:\n";
|
||||
|
||||
out += &format!(" Node Ids: {}\n", self.unlocked_inner.node_ids());
|
||||
out += &format!(
|
||||
" Self Latency Stats Accounting: {:#?}\n\n",
|
||||
inner.self_latency_stats_accounting
|
||||
);
|
||||
out += &format!(
|
||||
" Self Transfer Stats Accounting: {:#?}\n\n",
|
||||
inner.self_transfer_stats_accounting
|
||||
);
|
||||
out += &format!(
|
||||
" Self Transfer Stats: {:#?}\n\n",
|
||||
inner.self_transfer_stats
|
||||
);
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn debug_info_dialinfo(&self) -> String {
|
||||
let ldis = self.dial_info_details(RoutingDomain::LocalNetwork);
|
||||
let gdis = self.dial_info_details(RoutingDomain::PublicInternet);
|
||||
@@ -132,13 +130,25 @@ impl RoutingTable {
|
||||
for e in filtered_entries {
|
||||
let state = e.1.with(inner, |_rti, e| e.state(cur_ts));
|
||||
out += &format!(
|
||||
" {} [{}]\n",
|
||||
" {} [{}] {}\n",
|
||||
e.0.encode(),
|
||||
match state {
|
||||
BucketEntryState::Reliable => "R",
|
||||
BucketEntryState::Unreliable => "U",
|
||||
BucketEntryState::Dead => "D",
|
||||
}
|
||||
},
|
||||
e.1.with(inner, |_rti, e| {
|
||||
e.peer_stats()
|
||||
.latency
|
||||
.as_ref()
|
||||
.map(|l| {
|
||||
format!(
|
||||
"{:.2}ms",
|
||||
timestamp_to_secs(l.average.as_u64()) * 1000.0
|
||||
)
|
||||
})
|
||||
.unwrap_or_else(|| "???.??ms".to_string())
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
use super::*;
|
||||
|
||||
impl RoutingTable {
|
||||
/// Utility to find all closest nodes to a particular key, including possibly our own node and nodes further away from the key than our own, returning their peer info
|
||||
pub fn find_all_closest_peers(&self, key: TypedKey) -> NetworkResult<Vec<PeerInfo>> {
|
||||
let Some(own_peer_info) = self.get_own_peer_info(RoutingDomain::PublicInternet) else {
|
||||
// Our own node info is not yet available, drop this request.
|
||||
return NetworkResult::service_unavailable();
|
||||
};
|
||||
|
||||
// find N nodes closest to the target node in our routing table
|
||||
let filter = Box::new(
|
||||
move |rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| {
|
||||
// Ensure only things that are valid/signed in the PublicInternet domain are returned
|
||||
rti.filter_has_valid_signed_node_info(
|
||||
RoutingDomain::PublicInternet,
|
||||
true,
|
||||
opt_entry,
|
||||
)
|
||||
},
|
||||
) as RoutingTableEntryFilter;
|
||||
let filters = VecDeque::from([filter]);
|
||||
|
||||
let node_count = {
|
||||
let c = self.config.get();
|
||||
c.network.dht.max_find_node_count as usize
|
||||
};
|
||||
|
||||
let closest_nodes = self.find_closest_nodes(
|
||||
node_count,
|
||||
key,
|
||||
filters,
|
||||
// transform
|
||||
|rti, entry| {
|
||||
rti.transform_to_peer_info(RoutingDomain::PublicInternet, &own_peer_info, entry)
|
||||
},
|
||||
);
|
||||
|
||||
NetworkResult::value(closest_nodes)
|
||||
}
|
||||
|
||||
/// Utility to find nodes that are closer to a key than our own node, returning their peer info
|
||||
pub fn find_peers_closer_to_key(&self, key: TypedKey) -> NetworkResult<Vec<PeerInfo>> {
|
||||
// add node information for the requesting node to our routing table
|
||||
let crypto_kind = key.kind;
|
||||
let own_node_id = self.node_id(crypto_kind);
|
||||
|
||||
// find N nodes closest to the target node in our routing table
|
||||
// ensure the nodes returned are only the ones closer to the target node than ourself
|
||||
let Some(vcrypto) = self.crypto().get(crypto_kind) else {
|
||||
return NetworkResult::invalid_message("unsupported cryptosystem");
|
||||
};
|
||||
let own_distance = vcrypto.distance(&own_node_id.value, &key.value);
|
||||
|
||||
let filter = Box::new(
|
||||
move |rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| {
|
||||
// Exclude our own node
|
||||
let Some(entry) = opt_entry else {
|
||||
return false;
|
||||
};
|
||||
// Ensure only things that are valid/signed in the PublicInternet domain are returned
|
||||
if !rti.filter_has_valid_signed_node_info(
|
||||
RoutingDomain::PublicInternet,
|
||||
true,
|
||||
Some(entry.clone()),
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
// Ensure things further from the key than our own node are not included
|
||||
let Some(entry_node_id) = entry.with(rti, |_rti, e| e.node_ids().get(crypto_kind)) else {
|
||||
return false;
|
||||
};
|
||||
let entry_distance = vcrypto.distance(&entry_node_id.value, &key.value);
|
||||
if entry_distance >= own_distance {
|
||||
return false;
|
||||
}
|
||||
|
||||
true
|
||||
},
|
||||
) as RoutingTableEntryFilter;
|
||||
let filters = VecDeque::from([filter]);
|
||||
|
||||
let node_count = {
|
||||
let c = self.config.get();
|
||||
c.network.dht.max_find_node_count as usize
|
||||
};
|
||||
|
||||
//
|
||||
let closest_nodes = self.find_closest_nodes(
|
||||
node_count,
|
||||
key,
|
||||
filters,
|
||||
// transform
|
||||
|rti, entry| {
|
||||
entry.unwrap().with(rti, |_rti, e| {
|
||||
e.make_peer_info(RoutingDomain::PublicInternet).unwrap()
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
NetworkResult::value(closest_nodes)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod bucket;
|
||||
mod bucket_entry;
|
||||
mod debug;
|
||||
mod find_peers;
|
||||
mod node_ref;
|
||||
mod node_ref_filter;
|
||||
mod privacy;
|
||||
@@ -10,16 +11,21 @@ mod routing_domains;
|
||||
mod routing_table_inner;
|
||||
mod stats_accounting;
|
||||
mod tasks;
|
||||
mod types;
|
||||
|
||||
use crate::*;
|
||||
pub mod tests;
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::crypto::*;
|
||||
use crate::network_manager::*;
|
||||
use crate::rpc_processor::*;
|
||||
use bucket::*;
|
||||
use hashlink::LruCache;
|
||||
|
||||
pub use bucket_entry::*;
|
||||
pub use debug::*;
|
||||
use hashlink::LruCache;
|
||||
pub use find_peers::*;
|
||||
pub use node_ref::*;
|
||||
pub use node_ref_filter::*;
|
||||
pub use privacy::*;
|
||||
@@ -28,6 +34,7 @@ pub use routing_domain_editor::*;
|
||||
pub use routing_domains::*;
|
||||
pub use routing_table_inner::*;
|
||||
pub use stats_accounting::*;
|
||||
pub use types::*;
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -50,6 +57,8 @@ pub struct LowLevelPortInfo {
|
||||
}
|
||||
pub type RoutingTableEntryFilter<'t> =
|
||||
Box<dyn FnMut(&RoutingTableInner, Option<Arc<BucketEntry>>) -> bool + Send + 't>;
|
||||
pub type SerializedBuckets = Vec<Vec<u8>>;
|
||||
pub type SerializedBucketMap = BTreeMap<CryptoKind, SerializedBuckets>;
|
||||
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||
pub struct RoutingTableHealth {
|
||||
@@ -208,7 +217,7 @@ impl RoutingTable {
|
||||
unlocked_inner,
|
||||
};
|
||||
|
||||
this.start_tasks();
|
||||
this.setup_tasks();
|
||||
|
||||
this
|
||||
}
|
||||
@@ -259,7 +268,7 @@ impl RoutingTable {
|
||||
debug!("starting routing table terminate");
|
||||
|
||||
// Stop tasks
|
||||
self.stop_tasks().await;
|
||||
self.cancel_tasks().await;
|
||||
|
||||
// Load bucket entries from table db if possible
|
||||
debug!("saving routing table entries");
|
||||
@@ -285,14 +294,14 @@ impl RoutingTable {
|
||||
debug!("finished routing table terminate");
|
||||
}
|
||||
|
||||
/// Serialize routing table to table store
|
||||
async fn save_buckets(&self) -> EyreResult<()> {
|
||||
/// Serialize the routing table.
|
||||
fn serialized_buckets(&self) -> EyreResult<(SerializedBucketMap, SerializedBuckets)> {
|
||||
// Since entries are shared by multiple buckets per cryptokind
|
||||
// we need to get the list of all unique entries when serializing
|
||||
let mut all_entries: Vec<Arc<BucketEntry>> = Vec::new();
|
||||
|
||||
// Serialize all buckets and get map of entries
|
||||
let mut serialized_bucket_map: BTreeMap<CryptoKind, Vec<Vec<u8>>> = BTreeMap::new();
|
||||
let mut serialized_bucket_map: SerializedBucketMap = BTreeMap::new();
|
||||
{
|
||||
let mut entry_map: HashMap<*const BucketEntry, u32> = HashMap::new();
|
||||
let inner = &*self.inner.read();
|
||||
@@ -314,38 +323,55 @@ impl RoutingTable {
|
||||
all_entry_bytes.push(entry_bytes);
|
||||
}
|
||||
|
||||
Ok((serialized_bucket_map, all_entry_bytes))
|
||||
}
|
||||
|
||||
/// Write the serialized routing table to the table store.
|
||||
async fn save_buckets(&self) -> EyreResult<()> {
|
||||
let (serialized_bucket_map, all_entry_bytes) = self.serialized_buckets()?;
|
||||
|
||||
let table_store = self.unlocked_inner.network_manager().table_store();
|
||||
let tdb = table_store.open("routing_table", 1).await?;
|
||||
let dbx = tdb.transact();
|
||||
if let Err(e) = dbx.store_rkyv(0, b"serialized_bucket_map", &serialized_bucket_map) {
|
||||
dbx.rollback();
|
||||
return Err(e);
|
||||
return Err(e.into());
|
||||
}
|
||||
if let Err(e) = dbx.store_rkyv(0, b"all_entry_bytes", &all_entry_bytes) {
|
||||
dbx.rollback();
|
||||
return Err(e);
|
||||
return Err(e.into());
|
||||
}
|
||||
dbx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Deserialize routing table from table store
|
||||
async fn load_buckets(&self) -> EyreResult<()> {
|
||||
// Deserialize bucket map and all entries from the table store
|
||||
let tstore = self.unlocked_inner.network_manager().table_store();
|
||||
let tdb = tstore.open("routing_table", 1).await?;
|
||||
let Some(serialized_bucket_map): Option<BTreeMap<CryptoKind, Vec<Vec<u8>>>> = tdb.load_rkyv(0, b"serialized_bucket_map")? else {
|
||||
let Some(serialized_bucket_map): Option<SerializedBucketMap> = tdb.load_rkyv(0, b"serialized_bucket_map").await? else {
|
||||
log_rtab!(debug "no bucket map in saved routing table");
|
||||
return Ok(());
|
||||
};
|
||||
let Some(all_entry_bytes): Option<Vec<Vec<u8>>> = tdb.load_rkyv(0, b"all_entry_bytes")? else {
|
||||
let Some(all_entry_bytes): Option<SerializedBuckets> = tdb.load_rkyv(0, b"all_entry_bytes").await? else {
|
||||
log_rtab!(debug "no all_entry_bytes in saved routing table");
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
// Reconstruct all entries
|
||||
let inner = &mut *self.inner.write();
|
||||
self.populate_routing_table(inner, serialized_bucket_map, all_entry_bytes)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Write the deserialized table store data to the routing table.
|
||||
pub fn populate_routing_table(
|
||||
&self,
|
||||
inner: &mut RoutingTableInner,
|
||||
serialized_bucket_map: SerializedBucketMap,
|
||||
all_entry_bytes: SerializedBuckets,
|
||||
) -> EyreResult<()> {
|
||||
let mut all_entries: Vec<Arc<BucketEntry>> = Vec::with_capacity(all_entry_bytes.len());
|
||||
for entry_bytes in all_entry_bytes {
|
||||
let entryinner =
|
||||
@@ -789,8 +815,8 @@ impl RoutingTable {
|
||||
e.with(rti, |_rti, e| {
|
||||
if let Some(ni) = e.node_info(routing_domain) {
|
||||
let dif = DialInfoFilter::all()
|
||||
.with_protocol_type_set(ni.outbound_protocols)
|
||||
.with_address_type_set(ni.address_types);
|
||||
.with_protocol_type_set(ni.outbound_protocols())
|
||||
.with_address_type_set(ni.address_types());
|
||||
if dial_info.matches_filter(&dif) {
|
||||
return true;
|
||||
}
|
||||
@@ -848,7 +874,7 @@ impl RoutingTable {
|
||||
// does it have some dial info we need?
|
||||
let filter = |n: &NodeInfo| {
|
||||
let mut keep = false;
|
||||
for did in &n.dial_info_detail_list {
|
||||
for did in n.dial_info_detail_list() {
|
||||
if matches!(did.dial_info.address_type(), AddressType::IPV4) {
|
||||
for (n, protocol_type) in protocol_types.iter().enumerate() {
|
||||
if nodes_proto_v4[n] < max_per_type
|
||||
@@ -961,6 +987,16 @@ impl RoutingTable {
|
||||
.find_closest_nodes(node_count, node_id, filters, transform)
|
||||
}
|
||||
|
||||
pub fn sort_and_clean_closest_noderefs(
|
||||
&self,
|
||||
node_id: TypedKey,
|
||||
closest_nodes: &mut Vec<NodeRef>,
|
||||
) {
|
||||
self.inner
|
||||
.read()
|
||||
.sort_and_clean_closest_noderefs(node_id, closest_nodes)
|
||||
}
|
||||
|
||||
#[instrument(level = "trace", skip(self), ret)]
|
||||
pub fn register_find_node_answer(
|
||||
&self,
|
||||
@@ -971,12 +1007,12 @@ impl RoutingTable {
|
||||
let mut out = Vec::<NodeRef>::with_capacity(peers.len());
|
||||
for p in peers {
|
||||
// Ensure we're getting back nodes we asked for
|
||||
if !p.node_ids.kinds().contains(&crypto_kind) {
|
||||
if !p.node_ids().kinds().contains(&crypto_kind) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Don't register our own node
|
||||
if self.matches_own_node_id(&p.node_ids) {
|
||||
if self.matches_own_node_id(p.node_ids()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -170,17 +170,17 @@ pub trait NodeRefBase: Sized {
|
||||
) -> bool {
|
||||
self.operate(|_rti, e| e.has_seen_our_node_info_ts(routing_domain, our_node_info_ts))
|
||||
}
|
||||
fn set_our_node_info_ts(&self, routing_domain: RoutingDomain, seen_ts: Timestamp) {
|
||||
self.operate_mut(|_rti, e| e.set_our_node_info_ts(routing_domain, seen_ts));
|
||||
fn set_seen_our_node_info_ts(&self, routing_domain: RoutingDomain, seen_ts: Timestamp) {
|
||||
self.operate_mut(|_rti, e| e.set_seen_our_node_info_ts(routing_domain, seen_ts));
|
||||
}
|
||||
fn network_class(&self, routing_domain: RoutingDomain) -> Option<NetworkClass> {
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.network_class))
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.network_class()))
|
||||
}
|
||||
fn outbound_protocols(&self, routing_domain: RoutingDomain) -> Option<ProtocolTypeSet> {
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.outbound_protocols))
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.outbound_protocols()))
|
||||
}
|
||||
fn address_types(&self, routing_domain: RoutingDomain) -> Option<AddressTypeSet> {
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.address_types))
|
||||
self.operate(|_rt, e| e.node_info(routing_domain).map(|n| n.address_types()))
|
||||
}
|
||||
fn node_info_outbound_filter(&self, routing_domain: RoutingDomain) -> DialInfoFilter {
|
||||
let mut dif = DialInfoFilter::all();
|
||||
@@ -199,7 +199,7 @@ pub trait NodeRefBase: Sized {
|
||||
.and_then(|rpi| {
|
||||
// If relay is ourselves, then return None, because we can't relay through ourselves
|
||||
// and to contact this node we should have had an existing inbound connection
|
||||
if rti.unlocked_inner.matches_own_node_id(&rpi.node_ids) {
|
||||
if rti.unlocked_inner.matches_own_node_id(rpi.node_ids()) {
|
||||
return None;
|
||||
}
|
||||
|
||||
|
||||
@@ -22,6 +22,13 @@ pub enum RouteNode {
|
||||
}
|
||||
|
||||
impl RouteNode {
|
||||
pub fn validate(&self, crypto: Crypto) -> VeilidAPIResult<()> {
|
||||
match self {
|
||||
RouteNode::NodeId(_) => Ok(()),
|
||||
RouteNode::PeerInfo(pi) => pi.validate(crypto),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn node_ref(
|
||||
&self,
|
||||
routing_table: RoutingTable,
|
||||
@@ -48,10 +55,10 @@ impl RouteNode {
|
||||
RouteNode::NodeId(id) => {
|
||||
format!("{}", TypedKey::new(crypto_kind, *id))
|
||||
}
|
||||
RouteNode::PeerInfo(pi) => match pi.node_ids.get(crypto_kind) {
|
||||
RouteNode::PeerInfo(pi) => match pi.node_ids().get(crypto_kind) {
|
||||
Some(id) => format!("{}", id),
|
||||
None => {
|
||||
format!("({})?{}", crypto_kind, pi.node_ids)
|
||||
format!("({})?{}", crypto_kind, pi.node_ids())
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -66,6 +73,11 @@ pub struct RouteHop {
|
||||
/// The encrypted blob to pass to the next hop as its data (None for stubs)
|
||||
pub next_hop: Option<RouteHopData>,
|
||||
}
|
||||
impl RouteHop {
|
||||
pub fn validate(&self, crypto: Crypto) -> VeilidAPIResult<()> {
|
||||
self.node.validate(crypto)
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of hops a private route can have
|
||||
#[derive(Clone, Debug)]
|
||||
@@ -78,6 +90,15 @@ pub enum PrivateRouteHops {
|
||||
Empty,
|
||||
}
|
||||
|
||||
impl PrivateRouteHops {
|
||||
pub fn validate(&self, crypto: Crypto) -> VeilidAPIResult<()> {
|
||||
match self {
|
||||
PrivateRouteHops::FirstHop(rh) => rh.validate(crypto),
|
||||
PrivateRouteHops::Data(_) => Ok(()),
|
||||
PrivateRouteHops::Empty => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
/// A private route for receiver privacy
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PrivateRoute {
|
||||
@@ -108,6 +129,10 @@ impl PrivateRoute {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self, crypto: Crypto) -> VeilidAPIResult<()> {
|
||||
self.hops.validate(crypto)
|
||||
}
|
||||
|
||||
/// Check if this is a stub route
|
||||
pub fn is_stub(&self) -> bool {
|
||||
if let PrivateRouteHops::FirstHop(first_hop) = &self.hops {
|
||||
@@ -155,7 +180,7 @@ impl PrivateRoute {
|
||||
// Get the safety route to use from the spec
|
||||
Some(match &pr_first_hop.node {
|
||||
RouteNode::NodeId(n) => TypedKey::new(self.public_key.kind, *n),
|
||||
RouteNode::PeerInfo(p) => p.node_ids.get(self.public_key.kind).unwrap(),
|
||||
RouteNode::PeerInfo(p) => p.node_ids().get(self.public_key.kind).unwrap(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,6 @@ pub use route_spec_store_content::*;
|
||||
pub use route_stats::*;
|
||||
|
||||
use crate::veilid_api::*;
|
||||
use rkyv::{
|
||||
with::Skip, Archive as RkyvArchive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize,
|
||||
};
|
||||
|
||||
/// The size of the remote private route cache
|
||||
const REMOTE_PRIVATE_ROUTE_CACHE_SIZE: usize = 1024;
|
||||
|
||||
@@ -6,7 +6,6 @@ pub struct RouteSpecDetail {
|
||||
/// Crypto kind
|
||||
pub crypto_kind: CryptoKind,
|
||||
/// Secret key
|
||||
#[with(Skip)]
|
||||
pub secret_key: SecretKey,
|
||||
/// Route hops (node id keys)
|
||||
pub hops: Vec<PublicKey>,
|
||||
|
||||
@@ -115,7 +115,7 @@ impl RouteSpecStore {
|
||||
dr
|
||||
};
|
||||
|
||||
let update = VeilidUpdate::Route(VeilidStateRoute {
|
||||
let update = VeilidUpdate::RouteChange(VeilidRouteChange {
|
||||
dead_routes,
|
||||
dead_remote_routes,
|
||||
});
|
||||
@@ -1550,7 +1550,9 @@ impl RouteSpecStore {
|
||||
.get_root::<veilid_capnp::private_route::Reader>()
|
||||
.map_err(RPCError::internal)
|
||||
.wrap_err("failed to make reader for private_route")?;
|
||||
let private_route = decode_private_route(&pr_reader, crypto.clone()).wrap_err("failed to decode private route")?;
|
||||
let private_route = decode_private_route(&pr_reader).wrap_err("failed to decode private route")?;
|
||||
private_route.validate(crypto.clone()).wrap_err("failed to validate private route")?;
|
||||
|
||||
out.push(private_route);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ impl RouteSpecStoreContent {
|
||||
let table_store = routing_table.network_manager().table_store();
|
||||
let rsstdb = table_store.open("RouteSpecStore", 1).await?;
|
||||
let mut content: RouteSpecStoreContent =
|
||||
rsstdb.load_rkyv(0, b"content")?.unwrap_or_default();
|
||||
rsstdb.load_rkyv(0, b"content").await?.unwrap_or_default();
|
||||
|
||||
// Look up all route hop noderefs since we can't serialize those
|
||||
let mut dead_ids = Vec::new();
|
||||
@@ -55,47 +55,6 @@ impl RouteSpecStoreContent {
|
||||
content.remove_detail(&id);
|
||||
}
|
||||
|
||||
// Load secrets from pstore
|
||||
let pstore = routing_table.network_manager().protected_store();
|
||||
let secret_key_map: HashMap<PublicKey, SecretKey> = pstore
|
||||
.load_user_secret_rkyv("RouteSpecStore")
|
||||
.await?
|
||||
.unwrap_or_default();
|
||||
|
||||
// Ensure we got secret keys for all the public keys
|
||||
let mut got_secret_key_ids = HashSet::new();
|
||||
for (rsid, rssd) in content.details.iter_mut() {
|
||||
let mut found_all = true;
|
||||
for (pk, rsd) in rssd.iter_route_set_mut() {
|
||||
if let Some(sk) = secret_key_map.get(pk) {
|
||||
rsd.secret_key = *sk;
|
||||
} else {
|
||||
found_all = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if found_all {
|
||||
got_secret_key_ids.insert(rsid.clone());
|
||||
}
|
||||
}
|
||||
|
||||
// If we missed any, nuke those route ids
|
||||
let dead_ids: Vec<RouteId> = content
|
||||
.details
|
||||
.keys()
|
||||
.filter_map(|id| {
|
||||
if !got_secret_key_ids.contains(id) {
|
||||
Some(*id)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
for id in dead_ids {
|
||||
log_rtab!(debug "missing secret key, killing off private route: {}", id);
|
||||
content.remove_detail(&id);
|
||||
}
|
||||
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
@@ -106,18 +65,6 @@ impl RouteSpecStoreContent {
|
||||
let rsstdb = table_store.open("RouteSpecStore", 1).await?;
|
||||
rsstdb.store_rkyv(0, b"content", self).await?;
|
||||
|
||||
// Keep secrets in protected store as well
|
||||
let pstore = routing_table.network_manager().protected_store();
|
||||
|
||||
let mut out: HashMap<PublicKey, SecretKey> = HashMap::new();
|
||||
for (_rsid, rssd) in self.details.iter() {
|
||||
for (pk, rsd) in rssd.iter_route_set() {
|
||||
out.insert(*pk, rsd.secret_key);
|
||||
}
|
||||
}
|
||||
|
||||
let _ = pstore.save_user_secret_rkyv("RouteSpecStore", &out).await?; // ignore if this previously existed or not
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -102,14 +102,14 @@ impl RoutingDomainDetailCommon {
|
||||
}
|
||||
|
||||
fn make_peer_info(&self, rti: &RoutingTableInner) -> PeerInfo {
|
||||
let node_info = NodeInfo {
|
||||
network_class: self.network_class.unwrap_or(NetworkClass::Invalid),
|
||||
outbound_protocols: self.outbound_protocols,
|
||||
address_types: self.address_types,
|
||||
envelope_support: VALID_ENVELOPE_VERSIONS.to_vec(),
|
||||
crypto_support: VALID_CRYPTO_KINDS.to_vec(),
|
||||
dial_info_detail_list: self.dial_info_details.clone(),
|
||||
};
|
||||
let node_info = NodeInfo::new(
|
||||
self.network_class.unwrap_or(NetworkClass::Invalid),
|
||||
self.outbound_protocols,
|
||||
self.address_types,
|
||||
VALID_ENVELOPE_VERSIONS.to_vec(),
|
||||
VALID_CRYPTO_KINDS.to_vec(),
|
||||
self.dial_info_details.clone()
|
||||
);
|
||||
|
||||
let relay_info = self
|
||||
.relay_node
|
||||
@@ -117,8 +117,9 @@ impl RoutingDomainDetailCommon {
|
||||
.and_then(|rn| {
|
||||
let opt_relay_pi = rn.locked(rti).make_peer_info(self.routing_domain);
|
||||
if let Some(relay_pi) = opt_relay_pi {
|
||||
match relay_pi.signed_node_info {
|
||||
SignedNodeInfo::Direct(d) => Some((relay_pi.node_ids, d)),
|
||||
let (relay_ids, relay_sni) = relay_pi.destructure();
|
||||
match relay_sni {
|
||||
SignedNodeInfo::Direct(d) => Some((relay_ids, d)),
|
||||
SignedNodeInfo::Relayed(_) => {
|
||||
warn!("relay node should not have a relay itself! if this happens, a relay updated its signed node info and became a relay, which should cause the relay to be dropped");
|
||||
None
|
||||
@@ -230,8 +231,8 @@ fn first_filtered_dial_info_detail(
|
||||
) -> Option<DialInfoDetail> {
|
||||
let dial_info_filter = dial_info_filter.clone().filtered(
|
||||
&DialInfoFilter::all()
|
||||
.with_address_type_set(from_node.address_types)
|
||||
.with_protocol_type_set(from_node.outbound_protocols),
|
||||
.with_address_type_set(from_node.address_types())
|
||||
.with_protocol_type_set(from_node.outbound_protocols()),
|
||||
);
|
||||
|
||||
// Get first filtered dialinfo
|
||||
@@ -278,18 +279,18 @@ impl RoutingDomainDetail for PublicInternetRoutingDomainDetail {
|
||||
sequencing: Sequencing,
|
||||
) -> ContactMethod {
|
||||
// Get the nodeinfos for convenience
|
||||
let node_a = peer_a.signed_node_info.node_info();
|
||||
let node_b = peer_b.signed_node_info.node_info();
|
||||
let node_a = peer_a.signed_node_info().node_info();
|
||||
let node_b = peer_b.signed_node_info().node_info();
|
||||
|
||||
// Get the node ids that would be used between these peers
|
||||
let cck = common_crypto_kinds(&peer_a.node_ids.kinds(), &peer_b.node_ids.kinds());
|
||||
let cck = common_crypto_kinds(&peer_a.node_ids().kinds(), &peer_b.node_ids().kinds());
|
||||
let Some(best_ck) = cck.first().copied() else {
|
||||
// No common crypto kinds between these nodes, can't contact
|
||||
return ContactMethod::Unreachable;
|
||||
};
|
||||
|
||||
//let node_a_id = peer_a.node_ids.get(best_ck).unwrap();
|
||||
let node_b_id = peer_b.node_ids.get(best_ck).unwrap();
|
||||
//let node_a_id = peer_a.node_ids().get(best_ck).unwrap();
|
||||
let node_b_id = peer_b.node_ids().get(best_ck).unwrap();
|
||||
|
||||
// Get the best match dial info for node B if we have it
|
||||
if let Some(target_did) =
|
||||
@@ -302,17 +303,17 @@ impl RoutingDomainDetail for PublicInternetRoutingDomainDetail {
|
||||
}
|
||||
|
||||
// Get the target's inbound relay, it must have one or it is not reachable
|
||||
if let Some(node_b_relay) = peer_b.signed_node_info.relay_info() {
|
||||
if let Some(node_b_relay) = peer_b.signed_node_info().relay_info() {
|
||||
|
||||
// Note that relay_peer_info could be node_a, in which case a connection already exists
|
||||
// and we only get here if the connection had dropped, in which case node_a is unreachable until
|
||||
// it gets a new relay connection up
|
||||
if peer_b.signed_node_info.relay_ids().contains_any(&peer_a.node_ids) {
|
||||
if peer_b.signed_node_info().relay_ids().contains_any(peer_a.node_ids()) {
|
||||
return ContactMethod::Existing;
|
||||
}
|
||||
|
||||
// Get best node id to contact relay with
|
||||
let Some(node_b_relay_id) = peer_b.signed_node_info.relay_ids().get(best_ck) else {
|
||||
let Some(node_b_relay_id) = peer_b.signed_node_info().relay_ids().get(best_ck) else {
|
||||
// No best relay id
|
||||
return ContactMethod::Unreachable;
|
||||
};
|
||||
@@ -327,7 +328,7 @@ impl RoutingDomainDetail for PublicInternetRoutingDomainDetail {
|
||||
.is_some()
|
||||
{
|
||||
// Can node A receive anything inbound ever?
|
||||
if matches!(node_a.network_class, NetworkClass::InboundCapable) {
|
||||
if matches!(node_a.network_class(), NetworkClass::InboundCapable) {
|
||||
///////// Reverse connection
|
||||
|
||||
// Get the best match dial info for an reverse inbound connection from node B to node A
|
||||
@@ -390,17 +391,17 @@ impl RoutingDomainDetail for PublicInternetRoutingDomainDetail {
|
||||
}
|
||||
}
|
||||
// If the node B has no direct dial info, it needs to have an inbound relay
|
||||
else if let Some(node_b_relay) = peer_b.signed_node_info.relay_info() {
|
||||
else if let Some(node_b_relay) = peer_b.signed_node_info().relay_info() {
|
||||
|
||||
// Note that relay_peer_info could be node_a, in which case a connection already exists
|
||||
// and we only get here if the connection had dropped, in which case node_a is unreachable until
|
||||
// it gets a new relay connection up
|
||||
if peer_b.signed_node_info.relay_ids().contains_any(&peer_a.node_ids) {
|
||||
if peer_b.signed_node_info().relay_ids().contains_any(peer_a.node_ids()) {
|
||||
return ContactMethod::Existing;
|
||||
}
|
||||
|
||||
// Get best node id to contact relay with
|
||||
let Some(node_b_relay_id) = peer_b.signed_node_info.relay_ids().get(best_ck) else {
|
||||
let Some(node_b_relay_id) = peer_b.signed_node_info().relay_ids().get(best_ck) else {
|
||||
// No best relay id
|
||||
return ContactMethod::Unreachable;
|
||||
};
|
||||
@@ -419,7 +420,7 @@ impl RoutingDomainDetail for PublicInternetRoutingDomainDetail {
|
||||
}
|
||||
|
||||
// If node A can't reach the node by other means, it may need to use its own relay
|
||||
if let Some(node_a_relay_id) = peer_a.signed_node_info.relay_ids().get(best_ck) {
|
||||
if let Some(node_a_relay_id) = peer_a.signed_node_info().relay_ids().get(best_ck) {
|
||||
return ContactMethod::OutboundRelay(node_a_relay_id);
|
||||
}
|
||||
|
||||
@@ -484,8 +485,8 @@ impl RoutingDomainDetail for LocalNetworkRoutingDomainDetail {
|
||||
// Scope the filter down to protocols node A can do outbound
|
||||
let dial_info_filter = dial_info_filter.filtered(
|
||||
&DialInfoFilter::all()
|
||||
.with_address_type_set(peer_a.signed_node_info.node_info().address_types)
|
||||
.with_protocol_type_set(peer_a.signed_node_info.node_info().outbound_protocols),
|
||||
.with_address_type_set(peer_a.signed_node_info().node_info().address_types())
|
||||
.with_protocol_type_set(peer_a.signed_node_info().node_info().outbound_protocols()),
|
||||
);
|
||||
|
||||
// Get first filtered dialinfo
|
||||
@@ -509,7 +510,7 @@ impl RoutingDomainDetail for LocalNetworkRoutingDomainDetail {
|
||||
|
||||
let filter = |did: &DialInfoDetail| did.matches_filter(&dial_info_filter);
|
||||
|
||||
let opt_target_did = peer_b.signed_node_info.node_info().first_filtered_dial_info_detail(sort, filter);
|
||||
let opt_target_did = peer_b.signed_node_info().node_info().first_filtered_dial_info_detail(sort, filter);
|
||||
if let Some(target_did) = opt_target_did {
|
||||
return ContactMethod::Direct(target_did.dial_info);
|
||||
}
|
||||
|
||||
@@ -171,11 +171,11 @@ impl RoutingTableInner {
|
||||
node_info: &NodeInfo,
|
||||
) -> bool {
|
||||
// Should not be passing around nodeinfo with an invalid network class
|
||||
if matches!(node_info.network_class, NetworkClass::Invalid) {
|
||||
if matches!(node_info.network_class(), NetworkClass::Invalid) {
|
||||
return false;
|
||||
}
|
||||
// Ensure all of the dial info works in this routing domain
|
||||
for did in &node_info.dial_info_detail_list {
|
||||
for did in node_info.dial_info_detail_list() {
|
||||
if !self.ensure_dial_info_is_valid(routing_domain, &did.dial_info) {
|
||||
return false;
|
||||
}
|
||||
@@ -258,7 +258,7 @@ impl RoutingTableInner {
|
||||
} else {
|
||||
Some(
|
||||
rdd.common()
|
||||
.with_peer_info(self, |pi| pi.signed_node_info.timestamp()),
|
||||
.with_peer_info(self, |pi| pi.signed_node_info().timestamp()),
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -557,11 +557,18 @@ impl RoutingTableInner {
|
||||
.map(|nr| nr.same_bucket_entry(&entry))
|
||||
.unwrap_or(false);
|
||||
if e.needs_ping(cur_ts, is_our_relay) {
|
||||
debug!("needs_ping: {}", e.best_node_id());
|
||||
return true;
|
||||
}
|
||||
// If we need a ping because this node hasn't seen our latest node info, then do it
|
||||
if let Some(own_node_info_ts) = own_node_info_ts {
|
||||
if !e.has_seen_our_node_info_ts(routing_domain, own_node_info_ts) {
|
||||
//xxx remove this when we fix #208
|
||||
debug!(
|
||||
"!has_seen_our_node_info_ts: {} own_node_info_ts={}",
|
||||
e.best_node_id(),
|
||||
own_node_info_ts
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -803,37 +810,42 @@ impl RoutingTableInner {
|
||||
peer_info: PeerInfo,
|
||||
allow_invalid: bool,
|
||||
) -> Option<NodeRef> {
|
||||
// if our own node if is in the list then ignore it, as we don't add ourselves to our own routing table
|
||||
if self.unlocked_inner.matches_own_node_id(&peer_info.node_ids) {
|
||||
// if our own node is in the list, then ignore it as we don't add ourselves to our own routing table
|
||||
if self
|
||||
.unlocked_inner
|
||||
.matches_own_node_id(peer_info.node_ids())
|
||||
{
|
||||
log_rtab!(debug "can't register own node id in routing table");
|
||||
return None;
|
||||
}
|
||||
|
||||
// node can not be its own relay
|
||||
let rids = peer_info.signed_node_info.relay_ids();
|
||||
if self.unlocked_inner.matches_own_node_id(&rids) {
|
||||
let rids = peer_info.signed_node_info().relay_ids();
|
||||
let nids = peer_info.node_ids();
|
||||
if nids.contains_any(&rids) {
|
||||
log_rtab!(debug "node can not be its own relay");
|
||||
return None;
|
||||
}
|
||||
|
||||
if !allow_invalid {
|
||||
// verify signature
|
||||
if !peer_info.signed_node_info.has_any_signature() {
|
||||
log_rtab!(debug "signed node info for {:?} has invalid signature", &peer_info.node_ids);
|
||||
if !peer_info.signed_node_info().has_any_signature() {
|
||||
log_rtab!(debug "signed node info for {:?} has no valid signature", peer_info.node_ids());
|
||||
return None;
|
||||
}
|
||||
// verify signed node info is valid in this routing domain
|
||||
if !self.signed_node_info_is_valid_in_routing_domain(
|
||||
routing_domain,
|
||||
&peer_info.signed_node_info,
|
||||
peer_info.signed_node_info(),
|
||||
) {
|
||||
log_rtab!(debug "signed node info for {:?} not valid in the {:?} routing domain", peer_info.node_ids, routing_domain);
|
||||
log_rtab!(debug "signed node info for {:?} not valid in the {:?} routing domain", peer_info.node_ids(), routing_domain);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
self.create_node_ref(outer_self, &peer_info.node_ids, |_rti, e| {
|
||||
e.update_signed_node_info(routing_domain, peer_info.signed_node_info);
|
||||
let (node_ids, signed_node_info) = peer_info.destructure();
|
||||
self.create_node_ref(outer_self, &node_ids, |_rti, e| {
|
||||
e.update_signed_node_info(routing_domain, signed_node_info);
|
||||
})
|
||||
.map(|mut nr| {
|
||||
nr.set_filter(Some(
|
||||
@@ -1149,7 +1161,6 @@ impl RoutingTableInner {
|
||||
let vcrypto = self.unlocked_inner.crypto().get(crypto_kind).unwrap();
|
||||
|
||||
// Filter to ensure entries support the crypto kind in use
|
||||
|
||||
let filter = Box::new(
|
||||
move |_rti: &RoutingTableInner, opt_entry: Option<Arc<BucketEntry>>| {
|
||||
if let Some(entry) = opt_entry {
|
||||
@@ -1205,9 +1216,6 @@ impl RoutingTableInner {
|
||||
};
|
||||
|
||||
// distance is the next metric, closer nodes first
|
||||
// since multiple cryptosystems are in use, the distance for a key is the shortest
|
||||
// distance to that key over all supported cryptosystems
|
||||
|
||||
let da = vcrypto.distance(&a_key.value, &node_id.value);
|
||||
let db = vcrypto.distance(&b_key.value, &node_id.value);
|
||||
da.cmp(&db)
|
||||
@@ -1218,4 +1226,71 @@ impl RoutingTableInner {
|
||||
log_rtab!(">> find_closest_nodes: node count = {}", out.len());
|
||||
out
|
||||
}
|
||||
|
||||
pub fn sort_and_clean_closest_noderefs(
|
||||
&self,
|
||||
node_id: TypedKey,
|
||||
closest_nodes: &mut Vec<NodeRef>,
|
||||
) {
|
||||
// Lock all noderefs
|
||||
let kind = node_id.kind;
|
||||
let mut closest_nodes_locked: Vec<NodeRefLocked> = closest_nodes
|
||||
.iter()
|
||||
.filter_map(|x| {
|
||||
if x.node_ids().kinds().contains(&kind) {
|
||||
Some(x.locked(self))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Sort closest
|
||||
let sort = make_closest_noderef_sort(self.unlocked_inner.crypto(), node_id);
|
||||
closest_nodes_locked.sort_by(sort);
|
||||
|
||||
// Unlock noderefs
|
||||
*closest_nodes = closest_nodes_locked.iter().map(|x| x.unlocked()).collect();
|
||||
}
|
||||
}
|
||||
|
||||
fn make_closest_noderef_sort(
|
||||
crypto: Crypto,
|
||||
node_id: TypedKey,
|
||||
) -> impl Fn(&NodeRefLocked, &NodeRefLocked) -> core::cmp::Ordering {
|
||||
let cur_ts = get_aligned_timestamp();
|
||||
let kind = node_id.kind;
|
||||
// Get cryptoversion to check distance with
|
||||
let vcrypto = crypto.get(kind).unwrap();
|
||||
|
||||
move |a: &NodeRefLocked, b: &NodeRefLocked| -> core::cmp::Ordering {
|
||||
// same nodes are always the same
|
||||
if a.same_entry(b) {
|
||||
return core::cmp::Ordering::Equal;
|
||||
}
|
||||
|
||||
// reliable nodes come first, pessimistically treating our own node as unreliable
|
||||
a.operate(|_rti, a_entry| {
|
||||
b.operate(|_rti, b_entry| {
|
||||
let ra = a_entry.check_reliable(cur_ts);
|
||||
let rb = b_entry.check_reliable(cur_ts);
|
||||
if ra != rb {
|
||||
if ra {
|
||||
return core::cmp::Ordering::Less;
|
||||
} else {
|
||||
return core::cmp::Ordering::Greater;
|
||||
}
|
||||
}
|
||||
|
||||
// get keys
|
||||
let a_key = a_entry.node_ids().get(kind).unwrap();
|
||||
let b_key = b_entry.node_ids().get(kind).unwrap();
|
||||
|
||||
// distance is the next metric, closer nodes first
|
||||
let da = vcrypto.distance(&a_key.value, &node_id.value);
|
||||
let db = vcrypto.distance(&b_key.value, &node_id.value);
|
||||
da.cmp(&db)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -329,14 +329,15 @@ impl RoutingTable {
|
||||
let crypto_support = bsrec.node_ids.kinds();
|
||||
|
||||
// Make unsigned SignedNodeInfo
|
||||
let sni = SignedNodeInfo::Direct(SignedDirectNodeInfo::with_no_signature(NodeInfo {
|
||||
network_class: NetworkClass::InboundCapable, // Bootstraps are always inbound capable
|
||||
outbound_protocols: ProtocolTypeSet::only(ProtocolType::UDP), // Bootstraps do not participate in relaying and will not make outbound requests, but will have UDP enabled
|
||||
address_types: AddressTypeSet::all(), // Bootstraps are always IPV4 and IPV6 capable
|
||||
envelope_support: bsrec.envelope_support, // Envelope support is as specified in the bootstrap list
|
||||
crypto_support, // Crypto support is derived from list of node ids
|
||||
dial_info_detail_list: bsrec.dial_info_details, // Dial info is as specified in the bootstrap list
|
||||
}));
|
||||
let sni =
|
||||
SignedNodeInfo::Direct(SignedDirectNodeInfo::with_no_signature(NodeInfo::new(
|
||||
NetworkClass::InboundCapable, // Bootstraps are always inbound capable
|
||||
ProtocolTypeSet::only(ProtocolType::UDP), // Bootstraps do not participate in relaying and will not make outbound requests, but will have UDP enabled
|
||||
AddressTypeSet::all(), // Bootstraps are always IPV4 and IPV6 capable
|
||||
bsrec.envelope_support, // Envelope support is as specified in the bootstrap list
|
||||
crypto_support, // Crypto support is derived from list of node ids
|
||||
bsrec.dial_info_details, // Dial info is as specified in the bootstrap list
|
||||
)));
|
||||
|
||||
let pi = PeerInfo::new(bsrec.node_ids, sni);
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ pub mod rolling_transfers;
|
||||
use super::*;
|
||||
|
||||
impl RoutingTable {
|
||||
pub(crate) fn start_tasks(&self) {
|
||||
pub(crate) fn setup_tasks(&self) {
|
||||
// Set rolling transfers tick task
|
||||
{
|
||||
let this = self.clone();
|
||||
@@ -176,7 +176,7 @@ impl RoutingTable {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn stop_tasks(&self) {
|
||||
pub(crate) async fn cancel_tasks(&self) {
|
||||
// Cancel all tasks being ticked
|
||||
debug!("stopping rolling transfers task");
|
||||
if let Err(e) = self.unlocked_inner.rolling_transfers_task.stop().await {
|
||||
|
||||
@@ -13,8 +13,8 @@ impl RoutingTable {
|
||||
let Some(own_peer_info) = self.get_own_peer_info(RoutingDomain::PublicInternet) else {
|
||||
return Ok(());
|
||||
};
|
||||
let own_node_info = own_peer_info.signed_node_info.node_info();
|
||||
let network_class = own_node_info.network_class;
|
||||
let own_node_info = own_peer_info.signed_node_info().node_info();
|
||||
let network_class = own_node_info.network_class();
|
||||
|
||||
// Get routing domain editor
|
||||
let mut editor = self.edit_routing_domain(RoutingDomain::PublicInternet);
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod test_serialize;
|
||||
@@ -0,0 +1,84 @@
|
||||
use crate::*;
|
||||
|
||||
fn fake_routing_table() -> routing_table::RoutingTable {
|
||||
let veilid_config = VeilidConfig::new();
|
||||
let block_store = BlockStore::new(veilid_config.clone());
|
||||
let protected_store = ProtectedStore::new(veilid_config.clone());
|
||||
let table_store = TableStore::new(veilid_config.clone(), protected_store.clone());
|
||||
let crypto = Crypto::new(veilid_config.clone(), table_store.clone());
|
||||
let storage_manager = storage_manager::StorageManager::new(
|
||||
veilid_config.clone(),
|
||||
crypto.clone(),
|
||||
protected_store.clone(),
|
||||
table_store.clone(),
|
||||
block_store.clone(),
|
||||
);
|
||||
let network_manager = network_manager::NetworkManager::new(
|
||||
veilid_config.clone(),
|
||||
storage_manager,
|
||||
protected_store.clone(),
|
||||
table_store.clone(),
|
||||
block_store.clone(),
|
||||
crypto.clone(),
|
||||
);
|
||||
routing_table::RoutingTable::new(network_manager)
|
||||
}
|
||||
|
||||
pub async fn test_routingtable_buckets_round_trip() {
|
||||
let original = fake_routing_table();
|
||||
let copy = fake_routing_table();
|
||||
original.init().await.unwrap();
|
||||
copy.init().await.unwrap();
|
||||
|
||||
// Add lots of routes to `original` here to exercise all various types.
|
||||
|
||||
let (serialized_bucket_map, all_entry_bytes) = original.serialized_buckets().unwrap();
|
||||
|
||||
copy.populate_routing_table(
|
||||
&mut copy.inner.write(),
|
||||
serialized_bucket_map,
|
||||
all_entry_bytes,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Wrap to close lifetime of 'inner' which is borrowed here so terminate() can succeed
|
||||
// (it also .write() locks routing table inner)
|
||||
{
|
||||
let original_inner = &*original.inner.read();
|
||||
let copy_inner = &*copy.inner.read();
|
||||
|
||||
let routing_table_keys: Vec<_> = original_inner.buckets.keys().clone().collect();
|
||||
let copy_keys: Vec<_> = copy_inner.buckets.keys().clone().collect();
|
||||
|
||||
assert_eq!(routing_table_keys.len(), copy_keys.len());
|
||||
|
||||
for crypto in routing_table_keys {
|
||||
// The same keys are present in the original and copy RoutingTables.
|
||||
let original_buckets = original_inner.buckets.get(&crypto).unwrap();
|
||||
let copy_buckets = copy_inner.buckets.get(&crypto).unwrap();
|
||||
|
||||
// Recurse into RoutingTable.inner.buckets
|
||||
for (left_buckets, right_buckets) in original_buckets.iter().zip(copy_buckets.iter()) {
|
||||
// Recurse into RoutingTable.inner.buckets.entries
|
||||
for ((left_crypto, left_entries), (right_crypto, right_entries)) in
|
||||
left_buckets.entries().zip(right_buckets.entries())
|
||||
{
|
||||
assert_eq!(left_crypto, right_crypto);
|
||||
|
||||
assert_eq!(
|
||||
format!("{:?}", left_entries),
|
||||
format!("{:?}", right_entries)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Even if these are mocks, we should still practice good hygiene.
|
||||
original.terminate().await;
|
||||
copy.terminate().await;
|
||||
}
|
||||
|
||||
pub async fn test_all() {
|
||||
test_routingtable_buckets_round_trip().await;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use super::*;
|
||||
|
||||
// Keep member order appropriate for sorting < preference
|
||||
#[derive(
|
||||
Debug,
|
||||
Clone,
|
||||
PartialEq,
|
||||
PartialOrd,
|
||||
Ord,
|
||||
Eq,
|
||||
Hash,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
RkyvArchive,
|
||||
RkyvSerialize,
|
||||
RkyvDeserialize,
|
||||
)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct DialInfoDetail {
|
||||
pub class: DialInfoClass,
|
||||
pub dial_info: DialInfo,
|
||||
}
|
||||
|
||||
impl MatchesDialInfoFilter for DialInfoDetail {
|
||||
fn matches_filter(&self, filter: &DialInfoFilter) -> bool {
|
||||
self.dial_info.matches_filter(filter)
|
||||
}
|
||||
}
|
||||
|
||||
impl DialInfoDetail {
|
||||
pub fn ordered_sequencing_sort(a: &DialInfoDetail, b: &DialInfoDetail) -> core::cmp::Ordering {
|
||||
if a.class < b.class {
|
||||
return core::cmp::Ordering::Less;
|
||||
}
|
||||
if a.class > b.class {
|
||||
return core::cmp::Ordering::Greater;
|
||||
}
|
||||
DialInfo::ordered_sequencing_sort(&a.dial_info, &b.dial_info)
|
||||
}
|
||||
pub const NO_SORT: std::option::Option<
|
||||
for<'r, 's> fn(&'r DialInfoDetail, &'s DialInfoDetail) -> std::cmp::Ordering,
|
||||
> = None::<fn(&DialInfoDetail, &DialInfoDetail) -> core::cmp::Ordering>;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
use super::*;
|
||||
|
||||
#[allow(clippy::derive_hash_xor_eq)]
|
||||
#[derive(
|
||||
Debug,
|
||||
PartialOrd,
|
||||
Ord,
|
||||
Hash,
|
||||
EnumSetType,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
RkyvArchive,
|
||||
RkyvSerialize,
|
||||
RkyvDeserialize,
|
||||
)]
|
||||
#[enumset(repr = "u8")]
|
||||
#[archive_attr(repr(u8), derive(CheckBytes))]
|
||||
pub enum Direction {
|
||||
Inbound,
|
||||
Outbound,
|
||||
}
|
||||
pub type DirectionSet = EnumSet<Direction>;
|
||||
@@ -0,0 +1,21 @@
|
||||
mod dial_info_detail;
|
||||
mod direction;
|
||||
mod node_info;
|
||||
mod node_status;
|
||||
mod peer_info;
|
||||
mod routing_domain;
|
||||
mod signed_direct_node_info;
|
||||
mod signed_node_info;
|
||||
mod signed_relayed_node_info;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub use dial_info_detail::*;
|
||||
pub use direction::*;
|
||||
pub use node_info::*;
|
||||
pub use node_status::*;
|
||||
pub use peer_info::*;
|
||||
pub use routing_domain::*;
|
||||
pub use signed_direct_node_info::*;
|
||||
pub use signed_node_info::*;
|
||||
pub use signed_relayed_node_info::*;
|
||||
@@ -0,0 +1,164 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(
|
||||
Clone, Default, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize,
|
||||
)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct NodeInfo {
|
||||
network_class: NetworkClass,
|
||||
#[with(RkyvEnumSet)]
|
||||
outbound_protocols: ProtocolTypeSet,
|
||||
#[with(RkyvEnumSet)]
|
||||
address_types: AddressTypeSet,
|
||||
envelope_support: Vec<u8>,
|
||||
crypto_support: Vec<CryptoKind>,
|
||||
dial_info_detail_list: Vec<DialInfoDetail>,
|
||||
}
|
||||
|
||||
impl NodeInfo {
|
||||
pub fn new(
|
||||
network_class: NetworkClass,
|
||||
outbound_protocols: ProtocolTypeSet,
|
||||
address_types: AddressTypeSet,
|
||||
envelope_support: Vec<u8>,
|
||||
crypto_support: Vec<CryptoKind>,
|
||||
dial_info_detail_list: Vec<DialInfoDetail>,
|
||||
) -> Self {
|
||||
Self {
|
||||
network_class,
|
||||
outbound_protocols,
|
||||
address_types,
|
||||
envelope_support,
|
||||
crypto_support,
|
||||
dial_info_detail_list,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn network_class(&self) -> NetworkClass {
|
||||
self.network_class
|
||||
}
|
||||
pub fn outbound_protocols(&self) -> ProtocolTypeSet {
|
||||
self.outbound_protocols
|
||||
}
|
||||
pub fn address_types(&self) -> AddressTypeSet {
|
||||
self.address_types
|
||||
}
|
||||
pub fn envelope_support(&self) -> &[u8] {
|
||||
&self.envelope_support
|
||||
}
|
||||
pub fn crypto_support(&self) -> &[CryptoKind] {
|
||||
&self.crypto_support
|
||||
}
|
||||
pub fn dial_info_detail_list(&self) -> &[DialInfoDetail] {
|
||||
&self.dial_info_detail_list
|
||||
}
|
||||
|
||||
pub fn first_filtered_dial_info_detail<S, F>(
|
||||
&self,
|
||||
sort: Option<S>,
|
||||
filter: F,
|
||||
) -> Option<DialInfoDetail>
|
||||
where
|
||||
S: Fn(&DialInfoDetail, &DialInfoDetail) -> std::cmp::Ordering,
|
||||
F: Fn(&DialInfoDetail) -> bool,
|
||||
{
|
||||
if let Some(sort) = sort {
|
||||
let mut dids = self.dial_info_detail_list.clone();
|
||||
dids.sort_by(sort);
|
||||
for did in dids {
|
||||
if filter(&did) {
|
||||
return Some(did);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for did in &self.dial_info_detail_list {
|
||||
if filter(did) {
|
||||
return Some(did.clone());
|
||||
}
|
||||
}
|
||||
};
|
||||
None
|
||||
}
|
||||
|
||||
pub fn all_filtered_dial_info_details<S, F>(
|
||||
&self,
|
||||
sort: Option<S>,
|
||||
filter: F,
|
||||
) -> Vec<DialInfoDetail>
|
||||
where
|
||||
S: Fn(&DialInfoDetail, &DialInfoDetail) -> std::cmp::Ordering,
|
||||
F: Fn(&DialInfoDetail) -> bool,
|
||||
{
|
||||
let mut dial_info_detail_list = Vec::new();
|
||||
|
||||
if let Some(sort) = sort {
|
||||
let mut dids = self.dial_info_detail_list.clone();
|
||||
dids.sort_by(sort);
|
||||
for did in dids {
|
||||
if filter(&did) {
|
||||
dial_info_detail_list.push(did);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for did in &self.dial_info_detail_list {
|
||||
if filter(did) {
|
||||
dial_info_detail_list.push(did.clone());
|
||||
}
|
||||
}
|
||||
};
|
||||
dial_info_detail_list
|
||||
}
|
||||
|
||||
/// Does this node has some dial info
|
||||
pub fn has_dial_info(&self) -> bool {
|
||||
!self.dial_info_detail_list.is_empty()
|
||||
}
|
||||
|
||||
/// Is some relay required either for signal or inbound relay or outbound relay?
|
||||
pub fn requires_relay(&self) -> bool {
|
||||
match self.network_class {
|
||||
NetworkClass::InboundCapable => {
|
||||
for did in &self.dial_info_detail_list {
|
||||
if did.class.requires_relay() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
NetworkClass::OutboundOnly => {
|
||||
return true;
|
||||
}
|
||||
NetworkClass::WebApp => {
|
||||
return true;
|
||||
}
|
||||
NetworkClass::Invalid => {}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Can this node assist with signalling? Yes but only if it doesn't require signalling, itself.
|
||||
pub fn can_signal(&self) -> bool {
|
||||
// Must be inbound capable
|
||||
if !matches!(self.network_class, NetworkClass::InboundCapable) {
|
||||
return false;
|
||||
}
|
||||
// Do any of our dial info require signalling? if so, we can't offer signalling
|
||||
for did in &self.dial_info_detail_list {
|
||||
if did.class.requires_signal() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Can this node relay be an inbound relay?
|
||||
pub fn can_inbound_relay(&self) -> bool {
|
||||
// For now this is the same
|
||||
self.can_signal()
|
||||
}
|
||||
|
||||
/// Is this node capable of validating dial info
|
||||
pub fn can_validate_dial_info(&self) -> bool {
|
||||
// For now this is the same
|
||||
self.can_signal()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
use super::*;
|
||||
|
||||
/// RoutingDomain-specific status for each node
|
||||
/// is returned by the StatusA call
|
||||
|
||||
/// PublicInternet RoutingDomain Status
|
||||
#[derive(
|
||||
Clone, Debug, Default, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize,
|
||||
)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct PublicInternetNodeStatus {
|
||||
pub will_route: bool,
|
||||
pub will_tunnel: bool,
|
||||
pub will_signal: bool,
|
||||
pub will_relay: bool,
|
||||
pub will_validate_dial_info: bool,
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Clone, Debug, Default, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize,
|
||||
)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct LocalNetworkNodeStatus {
|
||||
pub will_relay: bool,
|
||||
pub will_validate_dial_info: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize)]
|
||||
#[archive_attr(repr(u8), derive(CheckBytes))]
|
||||
pub enum NodeStatus {
|
||||
PublicInternet(PublicInternetNodeStatus),
|
||||
LocalNetwork(LocalNetworkNodeStatus),
|
||||
}
|
||||
|
||||
impl NodeStatus {
|
||||
pub fn will_route(&self) -> bool {
|
||||
match self {
|
||||
NodeStatus::PublicInternet(pi) => pi.will_route,
|
||||
NodeStatus::LocalNetwork(_) => false,
|
||||
}
|
||||
}
|
||||
pub fn will_tunnel(&self) -> bool {
|
||||
match self {
|
||||
NodeStatus::PublicInternet(pi) => pi.will_tunnel,
|
||||
NodeStatus::LocalNetwork(_) => false,
|
||||
}
|
||||
}
|
||||
pub fn will_signal(&self) -> bool {
|
||||
match self {
|
||||
NodeStatus::PublicInternet(pi) => pi.will_signal,
|
||||
NodeStatus::LocalNetwork(_) => false,
|
||||
}
|
||||
}
|
||||
pub fn will_relay(&self) -> bool {
|
||||
match self {
|
||||
NodeStatus::PublicInternet(pi) => pi.will_relay,
|
||||
NodeStatus::LocalNetwork(ln) => ln.will_relay,
|
||||
}
|
||||
}
|
||||
pub fn will_validate_dial_info(&self) -> bool {
|
||||
match self {
|
||||
NodeStatus::PublicInternet(pi) => pi.will_validate_dial_info,
|
||||
NodeStatus::LocalNetwork(ln) => ln.will_validate_dial_info,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct PeerInfo {
|
||||
node_ids: TypedKeySet,
|
||||
signed_node_info: SignedNodeInfo,
|
||||
}
|
||||
|
||||
impl PeerInfo {
|
||||
pub fn new(node_ids: TypedKeySet, signed_node_info: SignedNodeInfo) -> Self {
|
||||
assert!(node_ids.len() > 0 && node_ids.len() <= MAX_CRYPTO_KINDS);
|
||||
Self {
|
||||
node_ids,
|
||||
signed_node_info,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self, crypto: Crypto) -> VeilidAPIResult<()> {
|
||||
let validated_node_ids = self.signed_node_info.validate(&self.node_ids, crypto)?;
|
||||
if validated_node_ids.is_empty() {
|
||||
// Shouldn't get here because signed node info validation also checks this
|
||||
apibail_generic!("no validated node ids");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn node_ids(&self) -> &TypedKeySet {
|
||||
&self.node_ids
|
||||
}
|
||||
pub fn signed_node_info(&self) -> &SignedNodeInfo {
|
||||
&self.signed_node_info
|
||||
}
|
||||
pub fn destructure(self) -> (TypedKeySet, SignedNodeInfo) {
|
||||
(self.node_ids, self.signed_node_info)
|
||||
}
|
||||
|
||||
pub fn validate_vec(peer_info_vec: &mut Vec<PeerInfo>, crypto: Crypto) {
|
||||
let mut n = 0usize;
|
||||
while n < peer_info_vec.len() {
|
||||
let pi = peer_info_vec.get(n).unwrap();
|
||||
if pi.validate(crypto.clone()).is_err() {
|
||||
peer_info_vec.remove(n);
|
||||
} else {
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
use super::*;
|
||||
|
||||
// Routing domain here is listed in order of preference, keep in order
|
||||
#[allow(clippy::derive_hash_xor_eq)]
|
||||
#[derive(
|
||||
Debug,
|
||||
Ord,
|
||||
PartialOrd,
|
||||
Hash,
|
||||
EnumSetType,
|
||||
Serialize,
|
||||
Deserialize,
|
||||
RkyvArchive,
|
||||
RkyvSerialize,
|
||||
RkyvDeserialize,
|
||||
)]
|
||||
#[enumset(repr = "u8")]
|
||||
#[archive_attr(repr(u8), derive(CheckBytes))]
|
||||
pub enum RoutingDomain {
|
||||
LocalNetwork = 0,
|
||||
PublicInternet = 1,
|
||||
}
|
||||
impl RoutingDomain {
|
||||
pub const fn count() -> usize {
|
||||
2
|
||||
}
|
||||
pub const fn all() -> [RoutingDomain; RoutingDomain::count()] {
|
||||
// Routing domain here is listed in order of preference, keep in order
|
||||
[RoutingDomain::LocalNetwork, RoutingDomain::PublicInternet]
|
||||
}
|
||||
}
|
||||
pub type RoutingDomainSet = EnumSet<RoutingDomain>;
|
||||
@@ -0,0 +1,93 @@
|
||||
use super::*;
|
||||
|
||||
/// Signed NodeInfo that can be passed around amongst peers and verifiable
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct SignedDirectNodeInfo {
|
||||
node_info: NodeInfo,
|
||||
timestamp: Timestamp,
|
||||
signatures: Vec<TypedSignature>,
|
||||
}
|
||||
impl SignedDirectNodeInfo {
|
||||
/// Returns a new SignedDirectNodeInfo that has its signatures validated.
|
||||
/// On success, this will modify the node_ids set to only include node_ids whose signatures validate.
|
||||
/// All signatures are stored however, as this can be passed to other nodes that may be able to validate those signatures.
|
||||
pub fn new(node_info: NodeInfo, timestamp: Timestamp, signatures: Vec<TypedSignature>) -> Self {
|
||||
Self {
|
||||
node_info,
|
||||
timestamp,
|
||||
signatures,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self, node_ids: &TypedKeySet, crypto: Crypto) -> VeilidAPIResult<TypedKeySet> {
|
||||
let node_info_bytes = Self::make_signature_bytes(&self.node_info, self.timestamp)?;
|
||||
|
||||
// Verify the signatures that we can
|
||||
let validated_node_ids =
|
||||
crypto.verify_signatures(node_ids, &node_info_bytes, &self.signatures)?;
|
||||
if validated_node_ids.len() == 0 {
|
||||
apibail_generic!("no valid node ids in direct node info");
|
||||
}
|
||||
|
||||
Ok(validated_node_ids)
|
||||
}
|
||||
|
||||
pub fn make_signatures(
|
||||
crypto: Crypto,
|
||||
typed_key_pairs: Vec<TypedKeyPair>,
|
||||
node_info: NodeInfo,
|
||||
) -> VeilidAPIResult<Self> {
|
||||
let timestamp = get_aligned_timestamp();
|
||||
let node_info_bytes = Self::make_signature_bytes(&node_info, timestamp)?;
|
||||
let typed_signatures =
|
||||
crypto.generate_signatures(&node_info_bytes, &typed_key_pairs, |kp, s| {
|
||||
TypedSignature::new(kp.kind, s)
|
||||
})?;
|
||||
Ok(Self {
|
||||
node_info,
|
||||
timestamp,
|
||||
signatures: typed_signatures,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_signature_bytes(
|
||||
node_info: &NodeInfo,
|
||||
timestamp: Timestamp,
|
||||
) -> VeilidAPIResult<Vec<u8>> {
|
||||
let mut node_info_bytes = Vec::new();
|
||||
|
||||
// Add nodeinfo to signature
|
||||
let mut ni_msg = ::capnp::message::Builder::new_default();
|
||||
let mut ni_builder = ni_msg.init_root::<veilid_capnp::node_info::Builder>();
|
||||
encode_node_info(node_info, &mut ni_builder).map_err(VeilidAPIError::internal)?;
|
||||
node_info_bytes.append(&mut builder_to_vec(ni_msg).map_err(VeilidAPIError::internal)?);
|
||||
|
||||
// Add timestamp to signature
|
||||
node_info_bytes.append(&mut timestamp.as_u64().to_le_bytes().to_vec());
|
||||
|
||||
Ok(node_info_bytes)
|
||||
}
|
||||
|
||||
pub fn with_no_signature(node_info: NodeInfo) -> Self {
|
||||
Self {
|
||||
node_info,
|
||||
timestamp: get_aligned_timestamp(),
|
||||
signatures: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_any_signature(&self) -> bool {
|
||||
!self.signatures.is_empty()
|
||||
}
|
||||
|
||||
pub fn node_info(&self) -> &NodeInfo {
|
||||
&self.node_info
|
||||
}
|
||||
pub fn timestamp(&self) -> Timestamp {
|
||||
self.timestamp
|
||||
}
|
||||
pub fn signatures(&self) -> &[TypedSignature] {
|
||||
&self.signatures
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize)]
|
||||
#[archive_attr(repr(u8), derive(CheckBytes))]
|
||||
pub enum SignedNodeInfo {
|
||||
Direct(SignedDirectNodeInfo),
|
||||
Relayed(SignedRelayedNodeInfo),
|
||||
}
|
||||
|
||||
impl SignedNodeInfo {
|
||||
pub fn validate(&self, node_ids: &TypedKeySet, crypto: Crypto) -> VeilidAPIResult<TypedKeySet> {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(d) => d.validate(node_ids, crypto),
|
||||
SignedNodeInfo::Relayed(r) => r.validate(node_ids, crypto),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has_any_signature(&self) -> bool {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(d) => d.has_any_signature(),
|
||||
SignedNodeInfo::Relayed(r) => r.has_any_signature(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn timestamp(&self) -> Timestamp {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(d) => d.timestamp(),
|
||||
SignedNodeInfo::Relayed(r) => r.timestamp(),
|
||||
}
|
||||
}
|
||||
pub fn node_info(&self) -> &NodeInfo {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(d) => &d.node_info(),
|
||||
SignedNodeInfo::Relayed(r) => &r.node_info(),
|
||||
}
|
||||
}
|
||||
pub fn relay_ids(&self) -> TypedKeySet {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(_) => TypedKeySet::new(),
|
||||
SignedNodeInfo::Relayed(r) => r.relay_ids().clone(),
|
||||
}
|
||||
}
|
||||
pub fn relay_info(&self) -> Option<&NodeInfo> {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(_) => None,
|
||||
SignedNodeInfo::Relayed(r) => Some(r.relay_info().node_info()),
|
||||
}
|
||||
}
|
||||
pub fn relay_peer_info(&self) -> Option<PeerInfo> {
|
||||
match self {
|
||||
SignedNodeInfo::Direct(_) => None,
|
||||
SignedNodeInfo::Relayed(r) => Some(PeerInfo::new(
|
||||
r.relay_ids().clone(),
|
||||
SignedNodeInfo::Direct(r.relay_info().clone()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
pub fn has_any_dial_info(&self) -> bool {
|
||||
self.node_info().has_dial_info()
|
||||
|| self
|
||||
.relay_info()
|
||||
.map(|relay_ni| relay_ni.has_dial_info())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn has_sequencing_matched_dial_info(&self, sequencing: Sequencing) -> bool {
|
||||
// Check our dial info
|
||||
for did in self.node_info().dial_info_detail_list() {
|
||||
match sequencing {
|
||||
Sequencing::NoPreference | Sequencing::PreferOrdered => return true,
|
||||
Sequencing::EnsureOrdered => {
|
||||
if did.dial_info.protocol_type().is_connection_oriented() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Check our relay if we have one
|
||||
return self
|
||||
.relay_info()
|
||||
.map(|relay_ni| {
|
||||
for did in relay_ni.dial_info_detail_list() {
|
||||
match sequencing {
|
||||
Sequencing::NoPreference | Sequencing::PreferOrdered => return true,
|
||||
Sequencing::EnsureOrdered => {
|
||||
if did.dial_info.protocol_type().is_connection_oriented() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
})
|
||||
.unwrap_or_default();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
use super::*;
|
||||
|
||||
/// Signed NodeInfo with a relay that can be passed around amongst peers and verifiable
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, RkyvArchive, RkyvSerialize, RkyvDeserialize)]
|
||||
#[archive_attr(repr(C), derive(CheckBytes))]
|
||||
pub struct SignedRelayedNodeInfo {
|
||||
node_info: NodeInfo,
|
||||
relay_ids: TypedKeySet,
|
||||
relay_info: SignedDirectNodeInfo,
|
||||
timestamp: Timestamp,
|
||||
signatures: Vec<TypedSignature>,
|
||||
}
|
||||
|
||||
impl SignedRelayedNodeInfo {
|
||||
/// Returns a new SignedRelayedNodeInfo that has its signatures validated.
|
||||
/// On success, this will modify the node_ids set to only include node_ids whose signatures validate.
|
||||
/// All signatures are stored however, as this can be passed to other nodes that may be able to validate those signatures.
|
||||
pub fn new(
|
||||
node_info: NodeInfo,
|
||||
relay_ids: TypedKeySet,
|
||||
relay_info: SignedDirectNodeInfo,
|
||||
timestamp: Timestamp,
|
||||
signatures: Vec<TypedSignature>,
|
||||
) -> Self {
|
||||
Self {
|
||||
node_info,
|
||||
relay_ids,
|
||||
relay_info,
|
||||
timestamp,
|
||||
signatures,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn validate(&self, node_ids: &TypedKeySet, crypto: Crypto) -> VeilidAPIResult<TypedKeySet> {
|
||||
// Ensure the relay info for the node has a superset of the crypto kinds of the node it is relaying
|
||||
if common_crypto_kinds(
|
||||
self.node_info.crypto_support(),
|
||||
self.relay_info.node_info().crypto_support(),
|
||||
)
|
||||
.len()
|
||||
!= self.node_info.crypto_support().len()
|
||||
{
|
||||
apibail_generic!("relay should have superset of node crypto kinds");
|
||||
}
|
||||
|
||||
// Verify signatures
|
||||
let node_info_bytes = Self::make_signature_bytes(
|
||||
&self.node_info,
|
||||
&self.relay_ids,
|
||||
&self.relay_info,
|
||||
self.timestamp,
|
||||
)?;
|
||||
let validated_node_ids =
|
||||
crypto.verify_signatures(node_ids, &node_info_bytes, &self.signatures)?;
|
||||
if validated_node_ids.len() == 0 {
|
||||
apibail_generic!("no valid node ids in relayed node info");
|
||||
}
|
||||
Ok(validated_node_ids)
|
||||
}
|
||||
|
||||
pub fn make_signatures(
|
||||
crypto: Crypto,
|
||||
typed_key_pairs: Vec<TypedKeyPair>,
|
||||
node_info: NodeInfo,
|
||||
relay_ids: TypedKeySet,
|
||||
relay_info: SignedDirectNodeInfo,
|
||||
) -> VeilidAPIResult<Self> {
|
||||
let timestamp = get_aligned_timestamp();
|
||||
let node_info_bytes =
|
||||
Self::make_signature_bytes(&node_info, &relay_ids, &relay_info, timestamp)?;
|
||||
let typed_signatures =
|
||||
crypto.generate_signatures(&node_info_bytes, &typed_key_pairs, |kp, s| {
|
||||
TypedSignature::new(kp.kind, s)
|
||||
})?;
|
||||
Ok(Self {
|
||||
node_info,
|
||||
relay_ids,
|
||||
relay_info,
|
||||
timestamp,
|
||||
signatures: typed_signatures,
|
||||
})
|
||||
}
|
||||
|
||||
fn make_signature_bytes(
|
||||
node_info: &NodeInfo,
|
||||
relay_ids: &[TypedKey],
|
||||
relay_info: &SignedDirectNodeInfo,
|
||||
timestamp: Timestamp,
|
||||
) -> VeilidAPIResult<Vec<u8>> {
|
||||
let mut sig_bytes = Vec::new();
|
||||
|
||||
// Add nodeinfo to signature
|
||||
let mut ni_msg = ::capnp::message::Builder::new_default();
|
||||
let mut ni_builder = ni_msg.init_root::<veilid_capnp::node_info::Builder>();
|
||||
encode_node_info(node_info, &mut ni_builder).map_err(VeilidAPIError::internal)?;
|
||||
sig_bytes.append(&mut builder_to_vec(ni_msg).map_err(VeilidAPIError::internal)?);
|
||||
|
||||
// Add relay ids to signature
|
||||
for relay_id in relay_ids {
|
||||
let mut rid_msg = ::capnp::message::Builder::new_default();
|
||||
let mut rid_builder = rid_msg.init_root::<veilid_capnp::typed_key::Builder>();
|
||||
encode_typed_key(relay_id, &mut rid_builder);
|
||||
sig_bytes.append(&mut builder_to_vec(rid_msg).map_err(VeilidAPIError::internal)?);
|
||||
}
|
||||
|
||||
// Add relay info to signature
|
||||
let mut ri_msg = ::capnp::message::Builder::new_default();
|
||||
let mut ri_builder = ri_msg.init_root::<veilid_capnp::signed_direct_node_info::Builder>();
|
||||
encode_signed_direct_node_info(relay_info, &mut ri_builder)
|
||||
.map_err(VeilidAPIError::internal)?;
|
||||
sig_bytes.append(&mut builder_to_vec(ri_msg).map_err(VeilidAPIError::internal)?);
|
||||
|
||||
// Add timestamp to signature
|
||||
sig_bytes.append(&mut timestamp.as_u64().to_le_bytes().to_vec());
|
||||
|
||||
Ok(sig_bytes)
|
||||
}
|
||||
|
||||
pub fn has_any_signature(&self) -> bool {
|
||||
!self.signatures.is_empty()
|
||||
}
|
||||
|
||||
pub fn node_info(&self) -> &NodeInfo {
|
||||
&self.node_info
|
||||
}
|
||||
pub fn timestamp(&self) -> Timestamp {
|
||||
self.timestamp
|
||||
}
|
||||
pub fn relay_ids(&self) -> &TypedKeySet {
|
||||
&self.relay_ids
|
||||
}
|
||||
pub fn relay_info(&self) -> &SignedDirectNodeInfo {
|
||||
&self.relay_info
|
||||
}
|
||||
pub fn signatures(&self) -> &[TypedSignature] {
|
||||
&self.signatures
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user