Cleanup, add NetworkConfig itself to log.

This commit is contained in:
Adam Ierymenko 2022-10-25 11:54:21 -04:00
parent 127e27326a
commit 0239991c0d
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
3 changed files with 23 additions and 15 deletions

View file

@ -62,16 +62,9 @@ impl Controller {
/// won't actually do anything. The reference the handler holds is weak to prevent /// won't actually do anything. The reference the handler holds is weak to prevent
/// a circular reference, so if the VL1Service is dropped this must be called again to /// a circular reference, so if the VL1Service is dropped this must be called again to
/// tell the controller handler about a new instance. /// tell the controller handler about a new instance.
pub fn set_service(&self, service: &Arc<VL1Service<dyn Database, Self, Self>>) { pub async fn set_service(&self, service: &Arc<VL1Service<dyn Database, Self, Self>>) {
*self.service.write().unwrap() = Arc::downgrade(service); *self.service.write().unwrap() = Arc::downgrade(service);
}
/// Start a change watcher to respond to changes detected by the database.
///
/// This should only be called once, though multiple calls won't do anything but create
/// unnecessary async tasks. If the database being used does not support changes, this
/// does nothing.
pub async fn start_change_watcher(&self) {
if let Some(cw) = self.database.changes().await.map(|mut ch| { if let Some(cw) = self.database.changes().await.map(|mut ch| {
let self2 = self.self_ref.upgrade().unwrap(); let self2 = self.self_ref.upgrade().unwrap();
self.runtime.spawn(async move { self.runtime.spawn(async move {
@ -89,6 +82,7 @@ impl Controller {
} }
} }
/// Compose and send network configuration packet.
fn send_network_config( fn send_network_config(
&self, &self,
peer: &Peer, peer: &Peer,
@ -142,9 +136,14 @@ impl Controller {
} }
} }
/// Called when the DB informs us of a change.
async fn handle_change_notification(self: Arc<Self>, _change: Change) {} async fn handle_change_notification(self: Arc<Self>, _change: Change) {}
async fn handle_network_config_request( /// Attempt to create a network configuration and return the result.
///
/// An error is only returned if a database or other unusual error occurs. Otherwise a rejection
/// reason is returned with None or an acceptance reason with a network configuration is returned.
async fn get_network_config(
self: &Arc<Self>, self: &Arc<Self>,
source_identity: &Identity, source_identity: &Identity,
network_id: NetworkId, network_id: NetworkId,
@ -335,12 +334,12 @@ impl InnerProtocol for Controller {
let node_fingerprint = Blob::from(peer.identity.fingerprint); let node_fingerprint = Blob::from(peer.identity.fingerprint);
let now = ms_since_epoch(); let now = ms_since_epoch();
let result = match self2.handle_network_config_request(&peer.identity, network_id, now).await { let (result, config) = match self2.get_network_config(&peer.identity, network_id, now).await {
Result::Ok((result, Some(config))) => { Result::Ok((result, Some(config))) => {
self2.send_network_config(peer.as_ref(), &config, Some(message_id)); self2.send_network_config(peer.as_ref(), &config, Some(message_id));
result (result, Some(config))
} }
Result::Ok((result, None)) => result, Result::Ok((result, None)) => (result, None),
Result::Err(_) => { Result::Err(_) => {
// TODO: log invalid request or internal error // TODO: log invalid request or internal error
return; return;
@ -363,6 +362,7 @@ impl InnerProtocol for Controller {
source_remote_endpoint, source_remote_endpoint,
source_hops, source_hops,
result, result,
config,
}) })
.await; .await;
}), }),

View file

@ -34,8 +34,7 @@ async fn run(database: Arc<dyn Database>, runtime: &Runtime) -> i32 {
let svc = svc.unwrap(); let svc = svc.unwrap();
svc.node().init_default_roots(); svc.node().init_default_roots();
handler.set_service(&svc); handler.set_service(&svc).await;
handler.start_change_watcher().await;
// Wait for kill signal on Unix-like platforms. // Wait for kill signal on Unix-like platforms.
#[cfg(unix)] #[cfg(unix)]
@ -49,6 +48,13 @@ async fn run(database: Arc<dyn Database>, runtime: &Runtime) -> i32 {
} }
} }
#[cfg(windows)]
{
// TODO: if anyone wants to use this on Windows you'll need to make it a service or wait
// for a stop signal or soemthing here.
todo!();
}
println!("Terminate signal received, shutting down..."); println!("Terminate signal received, shutting down...");
exitcode::OK exitcode::OK
} else { } else {

View file

@ -11,7 +11,7 @@ use std::collections::HashMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use zerotier_network_hypervisor::vl1::{Address, Endpoint}; use zerotier_network_hypervisor::vl1::{Address, Endpoint};
use zerotier_network_hypervisor::vl2::NetworkId; use zerotier_network_hypervisor::vl2::{NetworkConfig, NetworkId};
use zerotier_utils::blob::Blob; use zerotier_utils::blob::Blob;
/// A complete network with all member configuration information for import/export or blob storage. /// A complete network with all member configuration information for import/export or blob storage.
@ -111,6 +111,8 @@ pub struct RequestLogItem {
pub source_hops: u8, pub source_hops: u8,
#[serde(rename = "r")] #[serde(rename = "r")]
pub result: AuthorizationResult, pub result: AuthorizationResult,
#[serde(rename = "nc")]
pub config: Option<NetworkConfig>,
} }
impl ToString for RequestLogItem { impl ToString for RequestLogItem {