From 112d1be84dcf706a6d44d71931db039b3f744b46 Mon Sep 17 00:00:00 2001 From: Erik Hollensbe Date: Mon, 18 Apr 2022 15:48:27 -0700 Subject: [PATCH] various cleanups to get the full project closer to compiling Signed-off-by: Erik Hollensbe --- Makefile | 2 +- zerotier-network-hypervisor/Cargo.toml | 2 ++ zerotier-network-hypervisor/src/vl1/address.rs | 4 +++- zerotier-network-hypervisor/src/vl1/identity.rs | 5 +++++ zerotier-network-hypervisor/src/vl1/mod.rs | 2 ++ .../src/vl1/system_interface.rs | 17 +++++++++++++++++ zerotier-system-service/Cargo.lock | 4 ++++ zerotier-system-service/Cargo.toml | 2 ++ zerotier-system-service/src/fastudpsocket.rs | 16 ++++++++-------- zerotier-system-service/src/localconfig.rs | 5 ++++- zerotier-system-service/src/log.rs | 2 +- zerotier-system-service/src/service.rs | 4 ++-- zerotier-system-service/src/store.rs | 8 ++++---- zerotier-system-service/src/utils.rs | 17 ++++++++++------- zerotier-system-service/src/vnic/common.rs | 2 +- zerotier-system-service/src/vnic/vnic.rs | 9 +++++---- 16 files changed, 71 insertions(+), 30 deletions(-) create mode 100644 zerotier-network-hypervisor/src/vl1/system_interface.rs diff --git a/Makefile b/Makefile index b9bedde05..932d49a3c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: clean: FORCE - rm -rf zerotier-core-crypto/target zerotier-network-hypervisor/target zerotier-system-service/target syncwhole/target aes-gmac-siv/target iblt/target + for i in */Cargo.toml; do cd $$(dirname $$i); cargo clean || exit 1; cd ..; done test: for i in */Cargo.toml; do cd $$(dirname $$i); cargo test || exit 1; cd ..; done diff --git a/zerotier-network-hypervisor/Cargo.toml b/zerotier-network-hypervisor/Cargo.toml index c38726837..d27fa30a2 100644 --- a/zerotier-network-hypervisor/Cargo.toml +++ b/zerotier-network-hypervisor/Cargo.toml @@ -19,6 +19,8 @@ dashmap = "^4" parking_lot = "^0" lazy_static = "^1" highway = "^0" +serde = "^1" +serde_json = "^1" [target."cfg(not(windows))".dependencies] libc = "^0" diff --git a/zerotier-network-hypervisor/src/vl1/address.rs b/zerotier-network-hypervisor/src/vl1/address.rs index 608f9bf41..7342ab12f 100644 --- a/zerotier-network-hypervisor/src/vl1/address.rs +++ b/zerotier-network-hypervisor/src/vl1/address.rs @@ -10,12 +10,14 @@ use std::hash::{Hash, Hasher}; use std::num::NonZeroU64; use std::str::FromStr; +use serde::{Deserialize, Serialize}; + use crate::error::InvalidFormatError; use crate::util::buffer::Buffer; use crate::util::hex::HEX_CHARS; use crate::vl1::protocol::{ADDRESS_RESERVED_PREFIX, ADDRESS_SIZE}; -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[repr(transparent)] pub struct Address(NonZeroU64); diff --git a/zerotier-network-hypervisor/src/vl1/identity.rs b/zerotier-network-hypervisor/src/vl1/identity.rs index e113c57cf..d1ea6d125 100644 --- a/zerotier-network-hypervisor/src/vl1/identity.rs +++ b/zerotier-network-hypervisor/src/vl1/identity.rs @@ -17,6 +17,8 @@ use std::str::FromStr; use lazy_static::lazy_static; +use serde::{Deserialize, Serialize}; + use zerotier_core_crypto::c25519::*; use zerotier_core_crypto::hash::{hmac_sha512, SHA384, SHA384_HASH_SIZE, SHA512, SHA512_HASH_SIZE}; use zerotier_core_crypto::hex; @@ -74,6 +76,9 @@ pub struct Identity { pub fingerprint: [u8; SHA512_HASH_SIZE], } +#[derive(Eq, PartialEq, Clone, Debug, Ord, PartialOrd, Deserialize, Serialize)] +pub struct NetworkId(pub u64); + #[inline(always)] fn concat_arrays_2(a: &[u8; A], b: &[u8; B]) -> [u8; S] { assert_eq!(A + B, S); diff --git a/zerotier-network-hypervisor/src/vl1/mod.rs b/zerotier-network-hypervisor/src/vl1/mod.rs index 0538cd007..f7ebae691 100644 --- a/zerotier-network-hypervisor/src/vl1/mod.rs +++ b/zerotier-network-hypervisor/src/vl1/mod.rs @@ -21,6 +21,7 @@ pub(crate) mod peer; #[allow(unused)] pub(crate) mod protocol; pub(crate) mod symmetricsecret; +pub(crate) mod system_interface; pub(crate) mod whoisqueue; pub use address::Address; @@ -32,5 +33,6 @@ pub use mac::MAC; pub use node::{Node, SystemInterface}; pub use path::Path; pub use peer::Peer; +pub use system_interface::VL1SystemInterface; pub use protocol::{PACKET_FRAGMENT_COUNT_MAX, PACKET_SIZE_MAX}; diff --git a/zerotier-network-hypervisor/src/vl1/system_interface.rs b/zerotier-network-hypervisor/src/vl1/system_interface.rs new file mode 100644 index 000000000..89d6b386b --- /dev/null +++ b/zerotier-network-hypervisor/src/vl1/system_interface.rs @@ -0,0 +1,17 @@ +use super::{Endpoint, Identity}; +use std::num::NonZeroI64; + +pub trait VL1SystemInterface { + fn event_node_is_up(&self); + fn event_node_is_down(&self); + fn event_identity_collision(&self); + fn event_online_status_change(&self, online: bool); + fn event_user_message(&self, source: &Identity, message_type: u64, message: &[u8]); + fn load_node_identity(&self) -> Option>; + fn save_node_identity(&self, _: &Identity, public: &[u8], secret: &[u8]); + fn wire_send(&self, endpoint: &Endpoint, local_socket: Option, local_interface: Option, data: &[&[u8]], packet_ttl: u8) -> bool; + fn check_path(&self, id: &Identity, endpoint: &Endpoint, local_socket: Option, local_interface: Option) -> bool; + fn get_path_hints(&self, id: &Identity) -> Option<&[(&Endpoint, Option, Option)]>; + fn time_ticks(&self) -> i64; + fn time_clock(&self) -> i64; +} diff --git a/zerotier-system-service/Cargo.lock b/zerotier-system-service/Cargo.lock index f4c9e457e..203cb5e18 100644 --- a/zerotier-system-service/Cargo.lock +++ b/zerotier-system-service/Cargo.lock @@ -2179,6 +2179,8 @@ dependencies = [ "libc", "lz4_flex", "parking_lot", + "serde", + "serde_json", "winapi", "zerotier-core-crypto", ] @@ -2191,11 +2193,13 @@ dependencies = [ "clap", "colored", "digest_auth", + "hex", "libc", "mach", "num-traits", "num_cpus", "parking_lot", + "rand 0.7.3", "serde", "serde_json", "smol", diff --git a/zerotier-system-service/Cargo.toml b/zerotier-system-service/Cargo.toml index 844c8da39..027bec424 100644 --- a/zerotier-system-service/Cargo.toml +++ b/zerotier-system-service/Cargo.toml @@ -25,6 +25,8 @@ smol = "^1" tide = { version = "^0", features = ["h1-server"], default-features = false } digest_auth = "^0" chrono = "^0" +hex = "^0" +rand = "^0" [target."cfg(windows)".dependencies] winapi = { version = "^0", features = ["handleapi", "ws2ipdef", "ws2tcpip"] } diff --git a/zerotier-system-service/src/fastudpsocket.rs b/zerotier-system-service/src/fastudpsocket.rs index 0193249e4..706d9d16e 100644 --- a/zerotier-system-service/src/fastudpsocket.rs +++ b/zerotier-system-service/src/fastudpsocket.rs @@ -21,8 +21,8 @@ use std::sync::Arc; use num_traits::cast::AsPrimitive; -use crate::debug; -use zerotier_network_hypervisor::vl1::InetAddress; +//use crate::debug; +use zerotier_network_hypervisor::vl1::{InetAddress, InetAddressFamily}; use zerotier_network_hypervisor::{PacketBuffer, PacketBufferPool}; const FAST_UDP_SOCKET_MAX_THREADS: usize = 4; @@ -224,7 +224,7 @@ impl FastUDPSocket { let packet_buffer_pool_copy = packet_buffer_pool.clone(); s.threads.push( std::thread::Builder::new() - .stack_size(zerotier_core::RECOMMENDED_THREAD_STACK_SIZE) + //.stack_size(zerotier_core::RECOMMENDED_THREAD_STACK_SIZE) .spawn(move || { let mut from_address = InetAddress::new(); while thread_run.load(Ordering::Relaxed) { @@ -255,7 +255,7 @@ impl FastUDPSocket { } #[inline(always)] - pub fn send(&self, to_address: &InetAddress, data: &[u8], packet_ttl: i32) { + pub fn send(&self, to_address: &InetAddress, data: &[&[u8]], packet_ttl: u8) { debug_assert!(!self.sockets.is_empty()); fast_udp_socket_sendto(unsafe { self.sockets.get_unchecked(0) }, to_address, data, packet_ttl); } @@ -279,7 +279,7 @@ impl Drop for FastUDPSocket { self.thread_run.store(false, Ordering::Relaxed); for s in self.sockets.iter() { unsafe { - libc::sendto(s.get().as_(), tmp.as_ptr().cast(), 0, 0, (&self.bind_address as *const InetAddress).cast(), std::mem::size_of::() as osdep::socklen_t); + libc::sendto(s.get().as_(), tmp.as_ptr().cast(), 0, 0, (&self.bind_address as *const InetAddress).cast(), std::mem::size_of::() as libc::socklen_t); } } for s in self.sockets.iter() { @@ -308,7 +308,7 @@ mod tests { #[test] fn test_udp_bind_and_transfer() { { - let pool = Arc::new(PacketBufferPool::new(64, PacketBufferFactory)); + let pool = Arc::new(PacketBufferPool::new(64, PacketBufferFactory::new())); let ba0 = InetAddress::new_from_string("127.0.0.1/23333"); assert!(ba0.is_some()); @@ -334,8 +334,8 @@ mod tests { let data_bytes = [0_u8; 1024]; loop { - s0.send(&ba1, &data_bytes, 0); - s1.send(&ba0, &data_bytes, 0); + s0.send(&ba1, &[&data_bytes], 0); + s1.send(&ba0, &[&data_bytes], 0); if cnt0.load(Ordering::Relaxed) > 10000 && cnt1.load(Ordering::Relaxed) > 10000 { break; } diff --git a/zerotier-system-service/src/localconfig.rs b/zerotier-system-service/src/localconfig.rs index db01e958a..9c4cf26b0 100644 --- a/zerotier-system-service/src/localconfig.rs +++ b/zerotier-system-service/src/localconfig.rs @@ -10,6 +10,7 @@ use std::collections::BTreeMap; use serde::{Deserialize, Serialize}; +use zerotier_network_hypervisor::vl1::identity::NetworkId; use zerotier_network_hypervisor::vl1::{Address, InetAddress}; pub const UNASSIGNED_PRIVILEGED_PORTS: [u16; 299] = [ @@ -21,6 +22,8 @@ pub const UNASSIGNED_PRIVILEGED_PORTS: [u16; 299] = [ 1009, 1023, ]; +pub const DEFAULT_PORT: u16 = 9993; + #[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] #[serde(default)] pub struct LocalConfigPhysicalPathConfig { @@ -129,7 +132,7 @@ impl Default for LocalConfigSettings { } LocalConfigSettings { - primary_port: zerotier_core::DEFAULT_PORT, + primary_port: DEFAULT_PORT, port_mapping: true, log: LocalConfigLogSettings::default(), interface_prefix_blacklist: bl, diff --git a/zerotier-system-service/src/log.rs b/zerotier-system-service/src/log.rs index c17710342..6766b3a71 100644 --- a/zerotier-system-service/src/log.rs +++ b/zerotier-system-service/src/log.rs @@ -64,7 +64,7 @@ impl Log { fn log_internal(&mut self, pfx: &str, s: &str) { if !s.is_empty() { - let log_line = format!("{}[{}] {}{}\n", l.prefix.as_str(), chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(), pfx, s); + let log_line = format!("{}[{}] {}{}\n", self.prefix.as_str(), chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(), pfx, s); if !self.path.is_empty() { if self.file.is_none() { let f = OpenOptions::new().read(true).write(true).create(true).open(self.path.as_str()); diff --git a/zerotier-system-service/src/service.rs b/zerotier-system-service/src/service.rs index ab4997d0a..31c1fbf44 100644 --- a/zerotier-system-service/src/service.rs +++ b/zerotier-system-service/src/service.rs @@ -19,7 +19,7 @@ use zerotier_network_hypervisor::{Interface, NetworkHypervisor}; use crate::localconfig::LocalConfig; use crate::log::Log; -use crate::store::{platform_default_home_path, StateObjectType, Store}; +use crate::store::{StateObjectType, Store}; use crate::utils::{ms_monotonic, ms_since_epoch}; use crate::GlobalCommandLineFlags; @@ -105,7 +105,7 @@ impl VL1SystemInterface for ServiceInterface { impl SwitchInterface for ServiceInterface {} -impl Interface for ServiceInterface {} +//impl Interface for ServiceInterface {} pub fn run(global_cli_flags: &GlobalCommandLineFlags) -> i32 { let store = Store::new(global_cli_flags.base_path.as_str(), &global_cli_flags.auth_token_path_override, &global_cli_flags.auth_token_override); diff --git a/zerotier-system-service/src/store.rs b/zerotier-system-service/src/store.rs index 00bfaa992..6db6e7687 100644 --- a/zerotier-system-service/src/store.rs +++ b/zerotier-system-service/src/store.rs @@ -12,10 +12,10 @@ use std::path::{Path, PathBuf}; use std::str::FromStr; use std::sync::Mutex; -use zerotier_core::{NetworkId, StateObjectType}; - use crate::localconfig::LocalConfig; +use zerotier_network_hypervisor::vl1::identity::NetworkId; + const ZEROTIER_PID: &'static str = "zerotier.pid"; const ZEROTIER_URI: &'static str = "zerotier.uri"; const LOCAL_CONF: &'static str = "local.conf"; @@ -128,7 +128,7 @@ impl Store { if token2.is_empty() { if generate_if_missing { let mut rb = [0_u8; 32]; - unsafe { crate::osdep::getSecureRandom(rb.as_mut_ptr().cast(), 64) }; + unsafe { rb.fill_with(rand::random) }; token.reserve(rb.len()); for b in rb.iter() { if *b > 127_u8 { @@ -234,7 +234,7 @@ impl Store { } pub fn write_pid(&self) -> std::io::Result<()> { - let pid = unsafe { crate::osdep::getpid() }.to_string(); + let pid = unsafe { libc::getpid() }.to_string(); self.write_file(ZEROTIER_PID, pid.as_bytes()) } diff --git a/zerotier-system-service/src/utils.rs b/zerotier-system-service/src/utils.rs index fd8205f2f..106cf7391 100644 --- a/zerotier-system-service/src/utils.rs +++ b/zerotier-system-service/src/utils.rs @@ -11,6 +11,7 @@ use std::fs::File; use std::io::Read; use std::path::Path; use std::str::FromStr; +use std::time::UNIX_EPOCH; use serde::de::DeserializeOwned; use serde::Serialize; @@ -18,10 +19,10 @@ use serde::Serialize; use zerotier_core_crypto::hex; use zerotier_network_hypervisor::vl1::Identity; -use crate::osdep; +//use crate::osdep; pub fn ms_since_epoch() -> i64 { - std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_millis() as i64 + std::time::SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as i64 } #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -39,7 +40,9 @@ pub fn ms_monotonic() -> i64 { } #[cfg(not(any(target_os = "macos", target_os = "ios")))] -pub fn ms_monotonic() -> i64 {} +pub fn ms_monotonic() -> i64 { + std::time::SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis() as i64 +} pub fn parse_bool(v: &str) -> Result { if !v.is_empty() { @@ -104,20 +107,20 @@ pub fn parse_cli_identity(input: &str, validate: bool) -> Result String { let mut nonce_plaintext: [u64; 2] = [timestamp as u64, timestamp as u64]; unsafe { - osdep::encryptHttpAuthNonce(nonce_plaintext.as_mut_ptr().cast()); - hex::encode(*nonce_plaintext.as_ptr().cast::<[u8; 16]>()) + //osdep::encryptHttpAuthNonce(nonce_plaintext.as_mut_ptr().cast()); + hex::to_string(&nonce_plaintext.as_ptr().cast::<[u8]>()) } } /// Decrypt HTTP auth nonce encrypted by this process and return the timestamp. /// This returns zero if the input was not valid. pub fn decrypt_http_auth_nonce(nonce: &str) -> i64 { - let nonce = hex::decode(nonce.trim()); + let nonce = hex::from_string(nonce.trim()); if !nonce.is_err() { let mut nonce = nonce.unwrap(); if nonce.len() == 16 { unsafe { - osdep::decryptHttpAuthNonce(nonce.as_mut_ptr().cast()); + //osdep::decryptHttpAuthNonce(nonce.as_mut_ptr().cast()); let nonce = *nonce.as_ptr().cast::<[u64; 2]>(); if nonce[0] == nonce[1] { return nonce[0] as i64; diff --git a/zerotier-system-service/src/vnic/common.rs b/zerotier-system-service/src/vnic/common.rs index d844205c3..de76fe841 100644 --- a/zerotier-system-service/src/vnic/common.rs +++ b/zerotier-system-service/src/vnic/common.rs @@ -59,6 +59,6 @@ pub fn get_l2_multicast_subscriptions(dev: &str) -> HashSet { /// Linux stores this stuff in /proc and it needs to be fetched from there. #[cfg(target_os = "linux")] pub fn get_l2_multicast_subscriptions(dev: &str) -> HashSet { - let mut groups: HashSet = HashSet::new(); + let mut groups: HashSet = HashSet::new(); groups } diff --git a/zerotier-system-service/src/vnic/vnic.rs b/zerotier-system-service/src/vnic/vnic.rs index f890755a3..75aad87a6 100644 --- a/zerotier-system-service/src/vnic/vnic.rs +++ b/zerotier-system-service/src/vnic/vnic.rs @@ -6,19 +6,20 @@ * https://www.zerotier.com/ */ +use zerotier_network_hypervisor::vl1::{InetAddress, MAC}; use zerotier_network_hypervisor::vl2::MulticastGroup; /// Virtual network interface pub trait VNIC { /// Add a new IPv4 or IPv6 address to this interface, returning true on success. - fn add_ip(&self, ip: &zerotier_core::InetAddress) -> bool; + fn add_ip(&self, ip: &InetAddress) -> bool; /// Remove an IPv4 or IPv6 address, returning true on success. /// Nothing happens if the address is not found. - fn remove_ip(&self, ip: &zerotier_core::InetAddress) -> bool; + fn remove_ip(&self, ip: &InetAddress) -> bool; /// Enumerate all IPs on this interface including ones assigned outside ZeroTier. - fn ips(&self) -> Vec; + fn ips(&self) -> Vec; /// Get the OS-specific device name for this interface, e.g. zt## or tap##. fn device_name(&self) -> String; @@ -30,5 +31,5 @@ pub trait VNIC { fn get_multicast_groups(&self) -> std::collections::BTreeSet; /// Inject an Ethernet frame into this port. - fn put(&self, source_mac: &zerotier_core::MAC, dest_mac: &zerotier_core::MAC, ethertype: u16, vlan_id: u16, data: *const u8, len: usize) -> bool; + fn put(&self, source_mac: &MAC, dest_mac: &MAC, ethertype: u16, vlan_id: u16, data: *const u8, len: usize) -> bool; }