From 1f62d3836c48a78cdd03288e8b400264e150f13f Mon Sep 17 00:00:00 2001 From: John Smith Date: Wed, 8 Feb 2023 21:43:55 -0500 Subject: [PATCH] more breaking everything --- veilid-core/src/crypto/types.rs | 27 ++++++++++++ veilid-core/src/routing_table/mod.rs | 42 +++++++++++++------ veilid-core/src/routing_table/privacy.rs | 8 ++-- .../src/routing_table/routing_table_inner.rs | 9 ++-- .../src/rpc_processor/coders/signal_info.rs | 5 ++- .../src/rpc_processor/coders/value_data.rs | 4 +- veilid-core/src/veilid_api/types.rs | 14 +++---- 7 files changed, 80 insertions(+), 29 deletions(-) diff --git a/veilid-core/src/crypto/types.rs b/veilid-core/src/crypto/types.rs index 5e9c0332..cf65857a 100644 --- a/veilid-core/src/crypto/types.rs +++ b/veilid-core/src/crypto/types.rs @@ -10,6 +10,33 @@ use rkyv::{Archive as RkyvArchive, Deserialize as RkyvDeserialize, Serialize as /// Cryptography version fourcc code pub type CryptoKind = FourCC; +#[derive( + Clone, + Copy, + Debug, + Serialize, + Deserialize, + PartialOrd, + Ord, + PartialEq, + Eq, + Hash, + RkyvArchive, + RkyvSerialize, + RkyvDeserialize, +)] +#[archive_attr(repr(C), derive(CheckBytes))] +pub struct KeyPair { + pub key: PublicKey, + pub secret: SecretKey, +} + +impl KeyPair { + pub fn new(key: PublicKey, secret: SecretKey) -> Self { + Self { key, secret } + } +} + #[derive( Clone, Copy, diff --git a/veilid-core/src/routing_table/mod.rs b/veilid-core/src/routing_table/mod.rs index dd25e234..328ccb94 100644 --- a/veilid-core/src/routing_table/mod.rs +++ b/veilid-core/src/routing_table/mod.rs @@ -70,10 +70,8 @@ pub(super) struct RoutingTableUnlockedInner { config: VeilidConfig, network_manager: NetworkManager, - /// The current node's public DHT key - node_id: PublicKey, - /// The current node's DHT key secret - node_id_secret: SecretKey, + /// The current node's public DHT keys and secrets + node_id_keypairs: BTreeMap, /// Buckets to kick on our next kick task kick_queue: Mutex>, /// Background process for computing statistics @@ -107,8 +105,19 @@ impl RoutingTable { RoutingTableUnlockedInner { config: config.clone(), network_manager, - node_id: c.network.node_id.unwrap(), - node_id_secret: c.network.node_id_secret.unwrap(), + node_id_keypairs: c + .network + .routing_table + .node_ids + .iter() + .map(|(k, v)| { + ( + *k, + KeyPair::new(v.node_id.unwrap(), v.node_id_secret.unwrap()), + ) + }) + .collect(), + kick_queue: Mutex::new(BTreeSet::default()), rolling_transfers_task: TickTask::new(ROLLING_TRANSFERS_INTERVAL_SECS), kick_buckets_task: TickTask::new(1), @@ -136,6 +145,9 @@ impl RoutingTable { pub fn network_manager(&self) -> NetworkManager { self.unlocked_inner.network_manager.clone() } + pub fn crypto(&self) -> Crypto { + self.network_manager().crypto() + } pub fn rpc_processor(&self) -> RPCProcessor { self.network_manager().rpc_processor() } @@ -149,12 +161,16 @@ impl RoutingTable { f(&*self.unlocked_inner.config.get()) } - pub fn node_id(&self) -> PublicKey { - self.unlocked_inner.node_id + pub fn node_id(&self, kind: CryptoKind) -> PublicKey { + self.unlocked_inner.node_id_keypairs.get(&kind).unwrap().key } - pub fn node_id_secret(&self) -> SecretKey { - self.unlocked_inner.node_id_secret + pub fn node_id_secret(&self, kind: CryptoKind) -> SecretKey { + self.unlocked_inner + .node_id_keypairs + .get(&kind) + .unwrap() + .secret } ///////////////////////////////////// @@ -453,8 +469,10 @@ impl RoutingTable { self.inner.write().purge_last_connections(); } - fn find_bucket_index(&self, node_id: PublicKey) -> usize { - distance(&node_id, &self.unlocked_inner.node_id) + fn find_bucket_index(&self, node_id: TypedKey) -> usize { + let crypto = self.crypto().get(node_id.kind).unwrap(); + + .distance(&node_id, &self.unlocked_inner.node_id) .first_nonzero_bit() .unwrap() } diff --git a/veilid-core/src/routing_table/privacy.rs b/veilid-core/src/routing_table/privacy.rs index 13e2094a..10dc804e 100644 --- a/veilid-core/src/routing_table/privacy.rs +++ b/veilid-core/src/routing_table/privacy.rs @@ -16,7 +16,7 @@ pub struct RouteHopData { #[derive(Clone, Debug)] pub enum RouteNode { /// Route node is optimized, no contact method information as this node id has been seen before - NodeId(NodeId), + NodeId(TypedKey), /// Route node with full contact method information to ensure the peer is reachable PeerInfo(PeerInfo), } @@ -57,14 +57,14 @@ pub enum PrivateRouteHops { #[derive(Clone, Debug)] pub struct PrivateRoute { /// The public key used for the entire route - pub public_key: PublicKey, + pub public_key: TypedKey, pub hop_count: u8, pub hops: PrivateRouteHops, } impl PrivateRoute { /// Empty private route is the form used when receiving the last hop - pub fn new_empty(public_key: PublicKey) -> Self { + pub fn new_empty(public_key: TypedKey) -> Self { Self { public_key, hop_count: 0, @@ -72,7 +72,7 @@ impl PrivateRoute { } } /// Stub route is the form used when no privacy is required, but you need to specify the destination for a safety route - pub fn new_stub(public_key: PublicKey, node: RouteNode) -> Self { + pub fn new_stub(public_key: TypedKey, node: RouteNode) -> Self { Self { public_key, hop_count: 1, diff --git a/veilid-core/src/routing_table/routing_table_inner.rs b/veilid-core/src/routing_table/routing_table_inner.rs index fc7139e7..5f94cc1e 100644 --- a/veilid-core/src/routing_table/routing_table_inner.rs +++ b/veilid-core/src/routing_table/routing_table_inner.rs @@ -52,15 +52,18 @@ impl RoutingTableInner { pub fn network_manager(&self) -> NetworkManager { self.unlocked_inner.network_manager.clone() } + pub fn crypto(&self) -> Crypto { + self.network_manager().crypto() + } pub fn rpc_processor(&self) -> RPCProcessor { self.network_manager().rpc_processor() } - pub fn node_id(&self) -> PublicKey { + pub fn node_id(&self, kind: CryptoKind) -> PublicKey { self.unlocked_inner.node_id } - pub fn node_id_secret(&self) -> SecretKey { + pub fn node_id_secret(&self, kind: CryptoKind) -> SecretKey { self.unlocked_inner.node_id_secret } @@ -654,7 +657,7 @@ impl RoutingTableInner { &mut self, outer_self: RoutingTable, routing_domain: RoutingDomain, - node_id: PublicKey, + node_ids: Vec, signed_node_info: SignedNodeInfo, allow_invalid: bool, ) -> Option { diff --git a/veilid-core/src/rpc_processor/coders/signal_info.rs b/veilid-core/src/rpc_processor/coders/signal_info.rs index 5e9edc84..0f51257e 100644 --- a/veilid-core/src/rpc_processor/coders/signal_info.rs +++ b/veilid-core/src/rpc_processor/coders/signal_info.rs @@ -34,6 +34,7 @@ pub fn encode_signal_info( pub fn decode_signal_info( reader: &veilid_capnp::operation_signal::Reader, + crypto: Crypto, ) -> Result { Ok( match reader @@ -52,7 +53,7 @@ pub fn decode_signal_info( let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol( "invalid peer info in hole punch signal info", ))?; - let peer_info = decode_peer_info(&pi_reader)?; + let peer_info = decode_peer_info(&pi_reader, crypto)?; SignalInfo::HolePunch { receipt, peer_info } } @@ -68,7 +69,7 @@ pub fn decode_signal_info( let pi_reader = r.get_peer_info().map_err(RPCError::map_protocol( "invalid peer info in reverse connect signal info", ))?; - let peer_info = decode_peer_info(&pi_reader)?; + let peer_info = decode_peer_info(&pi_reader, crypto)?; SignalInfo::ReverseConnect { receipt, peer_info } } diff --git a/veilid-core/src/rpc_processor/coders/value_data.rs b/veilid-core/src/rpc_processor/coders/value_data.rs index ba859423..70cbf0a4 100644 --- a/veilid-core/src/rpc_processor/coders/value_data.rs +++ b/veilid-core/src/rpc_processor/coders/value_data.rs @@ -5,6 +5,7 @@ pub fn encode_value_data( builder: &mut veilid_capnp::value_data::Builder, ) -> Result<(), RPCError> { builder.set_data(&value_data.data); + builder.set_schema(u32::from_be_bytes(value_data.schema.0)); builder.set_seq(value_data.seq); Ok(()) } @@ -12,5 +13,6 @@ pub fn encode_value_data( pub fn decode_value_data(reader: &veilid_capnp::value_data::Reader) -> Result { let data = reader.get_data().map_err(RPCError::protocol)?.to_vec(); let seq = reader.get_seq(); - Ok(ValueData { data, seq }) + let schema = FourCC::from(reader.get_schema().to_be_bytes()); + Ok(ValueData { data, schema, seq }) } diff --git a/veilid-core/src/veilid_api/types.rs b/veilid-core/src/veilid_api/types.rs index 6d8d06b9..975dea76 100644 --- a/veilid-core/src/veilid_api/types.rs +++ b/veilid-core/src/veilid_api/types.rs @@ -332,20 +332,20 @@ pub struct VeilidState { )] #[archive_attr(repr(C), derive(CheckBytes))] pub struct ValueData { - pub data: Vec, - pub schema: ValueSchema, pub seq: u32, + pub schema: ValueSchema, + pub data: Vec, } impl ValueData { - pub fn new(data: Vec, schema: ValueSchema) -> Self { + pub fn new(schema: ValueSchema, data: Vec) -> Self { Self { - data, - schema, seq: 0, + schema, + data, } } - pub fn new_with_seq(data: Vec, schema: ValueSchema, seq: u32) -> Self { - Self { data, schema, seq } + pub fn new_with_seq(seq: u32, schema: ValueSchema, data: Vec) -> Self { + Self { seq, schema, data } } pub fn change(&mut self, data: Vec) { self.data = data;