From 29fa689f0b01af384188c96387bb4033c655d5b3 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 26 Jan 2021 15:03:31 -0500 Subject: [PATCH] Reorganize some stuff, add bindings needed in service. --- .gitignore | 3 +- Makefile | 5 ++- osdep/MacEthernetTapAgent.c | 1 + osdep/rust-osdep.h | 36 +++++++++++++++ rust-zerotier-core/src/address.rs | 1 + rust-zerotier-core/src/bindings/mod.rs | 15 ------- rust-zerotier-core/src/buffer.rs | 1 + rust-zerotier-core/src/identity.rs | 2 + rust-zerotier-core/src/inetaddress.rs | 4 +- rust-zerotier-core/src/lib.rs | 45 ++++++++++++------- rust-zerotier-core/src/locator.rs | 2 + rust-zerotier-core/src/mac.rs | 16 +++++++ rust-zerotier-core/src/multicastgroup.rs | 26 +++++++++++ rust-zerotier-service/src/main.rs | 4 ++ rust-zerotier-service/src/vnp/mac_feth_tap.rs | 10 +++++ rust-zerotier-service/src/vnp/mod.rs | 17 +++++++ 16 files changed, 152 insertions(+), 36 deletions(-) create mode 100644 osdep/rust-osdep.h delete mode 100644 rust-zerotier-core/src/bindings/mod.rs create mode 100644 rust-zerotier-core/src/multicastgroup.rs create mode 100644 rust-zerotier-service/src/vnp/mac_feth_tap.rs create mode 100644 rust-zerotier-service/src/vnp/mod.rs diff --git a/.gitignore b/.gitignore index f8912667d..a839feeb9 100644 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,7 @@ node_modules *.vcxproj.backup /*.db /*.opendb -/rust-zerotier-core/src/bindings/capi.rs +/rust-zerotier-core/src/capi.rs /rust-zerotier-core/target /rust-zerotier-service/target +/rust-zerotier-service/src/osdep.rs diff --git a/Makefile b/Makefile index f66ec4c74..a0a897a45 100644 --- a/Makefile +++ b/Makefile @@ -30,9 +30,10 @@ clean: FORCE distclean: FORCE rm -rf ${BUILDDIR} -rust-zerotier-core-bindgen: FORCE +rust-bindgen: FORCE cargo install bindgen rm -f rust-zerotier-core/src/bindings/capi.rs - bindgen --no-doc-comments --no-layout-tests --no-derive-debug core/zerotier.h >rust-zerotier-core/src/bindings/capi.rs + bindgen --no-doc-comments --no-layout-tests --no-derive-debug core/zerotier.h >rust-zerotier-core/src/capi.rs + bindgen --no-doc-comments --no-layout-tests --no-derive-debug osdep/rust-osdep.h >rust-zerotier-service/src/osdep.rs FORCE: diff --git a/osdep/MacEthernetTapAgent.c b/osdep/MacEthernetTapAgent.c index 3a44eadd2..871ec7e46 100644 --- a/osdep/MacEthernetTapAgent.c +++ b/osdep/MacEthernetTapAgent.c @@ -225,6 +225,7 @@ int main(int argc,char **argv) const char *mac = argv[2]; const char *mtu = argv[3]; const char *metric = argv[4]; + int i = NDRV_SETDMXSPEC; s_ndrvfd = socket(AF_NDRV,SOCK_RAW,0); if (s_ndrvfd < 0) { diff --git a/osdep/rust-osdep.h b/osdep/rust-osdep.h new file mode 100644 index 000000000..5a522ef4d --- /dev/null +++ b/osdep/rust-osdep.h @@ -0,0 +1,36 @@ +#ifdef __APPLE__ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif diff --git a/rust-zerotier-core/src/address.rs b/rust-zerotier-core/src/address.rs index f6a09ea40..144a7fec1 100644 --- a/rust-zerotier-core/src/address.rs +++ b/rust-zerotier-core/src/address.rs @@ -29,6 +29,7 @@ impl From for Address { } impl From<&str> for Address { + #[inline(always)] fn from(s: &str) -> Address { Address(u64::from_str_radix(s, 16).unwrap_or(0)) } diff --git a/rust-zerotier-core/src/bindings/mod.rs b/rust-zerotier-core/src/bindings/mod.rs deleted file mode 100644 index e7287f54d..000000000 --- a/rust-zerotier-core/src/bindings/mod.rs +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c)2013-2020 ZeroTier, Inc. - * - * Use of this software is governed by the Business Source License included - * in the LICENSE.TXT file in the project's root directory. - * - * Change Date: 2025-01-01 - * - * On the date above, in accordance with the Business Source License, use - * of this software will be governed by version 2.0 of the Apache License. - */ -/****/ - -#[allow(non_snake_case,non_upper_case_globals,non_camel_case_types,dead_code,improper_ctypes)] -pub mod capi; diff --git a/rust-zerotier-core/src/buffer.rs b/rust-zerotier-core/src/buffer.rs index 8fe56c5e0..67eb97185 100644 --- a/rust-zerotier-core/src/buffer.rs +++ b/rust-zerotier-core/src/buffer.rs @@ -70,6 +70,7 @@ impl Buffer { } impl Drop for Buffer { + #[inline(always)] fn drop(&mut self) { // NOTE: in node.rs std::mem::forget() is used to prevent this from // being called on buffers that have been returned via one of the diff --git a/rust-zerotier-core/src/identity.rs b/rust-zerotier-core/src/identity.rs index c9d238aed..f679f9349 100644 --- a/rust-zerotier-core/src/identity.rs +++ b/rust-zerotier-core/src/identity.rs @@ -145,6 +145,7 @@ impl PartialEq for Identity { impl Eq for Identity {} impl Clone for Identity { + #[inline(always)] fn clone(&self) -> Identity { unsafe { return Identity::new_from_capi(ztcore::ZT_Identity_clone(self.capi), true); @@ -153,6 +154,7 @@ impl Clone for Identity { } impl Drop for Identity { + #[inline(always)] fn drop(&mut self) { if self.requires_delete { unsafe { diff --git a/rust-zerotier-core/src/inetaddress.rs b/rust-zerotier-core/src/inetaddress.rs index e7a165b56..caadabe46 100644 --- a/rust-zerotier-core/src/inetaddress.rs +++ b/rust-zerotier-core/src/inetaddress.rs @@ -11,16 +11,16 @@ */ /****/ +use std::cmp::Ordering; use std::ffi::CString; use std::mem::{MaybeUninit, transmute}; +use std::os::raw::{c_uint, c_void}; use num_derive::{FromPrimitive, ToPrimitive}; use num_traits::FromPrimitive; use crate::*; use crate::bindings::capi as ztcore; -use std::os::raw::{c_void, c_uint}; -use std::cmp::Ordering; // WARNING: here be dragons! This defines an opaque blob in Rust that shadows // and is of the exact size as an opaque blob in C that shadows and is the diff --git a/rust-zerotier-core/src/lib.rs b/rust-zerotier-core/src/lib.rs index 8bc79ae29..138ff9199 100644 --- a/rust-zerotier-core/src/lib.rs +++ b/rust-zerotier-core/src/lib.rs @@ -14,7 +14,8 @@ use std::os::raw::{c_char, c_int}; use num_derive::{FromPrimitive, ToPrimitive}; -mod bindings; +#[allow(non_snake_case,non_upper_case_globals,non_camel_case_types,dead_code,improper_ctypes)] +mod capi; mod identity; mod address; @@ -31,8 +32,9 @@ mod mac; mod buffer; mod portableatomici64; mod virtualnetworkconfig; +mod multicastgroup; -use bindings::capi as ztcore; +use crate::capi as ztcore; pub use identity::*; pub use address::Address; @@ -49,34 +51,45 @@ pub use mac::MAC; pub use buffer::Buffer; pub use portableatomici64::PortableAtomicI64; pub use virtualnetworkconfig::*; +pub use multicastgroup::MulticastGroup; +/// Recommended minimum thread stack size for ZeroTier threads. pub const RECOMMENDED_THREAD_STACK_SIZE: usize = 262144; +/// Default TCP and UDP port for ZeroTier. pub const DEFAULT_PORT: u16 = ztcore::ZT_DEFAULT_PORT as u16; +/// Size of a ZeroTier core "Buffer" in bytes. pub const BUF_SIZE: u32 = ztcore::ZT_BUF_SIZE; +/// Minimum physical MTU. pub const MIN_MTU: u32 = ztcore::ZT_MIN_MTU; + +/// Maximum physical MTU. pub const MAX_MTU: u32 = ztcore::ZT_MAX_MTU; + +/// Default physica UDP MTU (not including IP or UDP headers). pub const DEFAULT_UDP_MTU: u32 = ztcore::ZT_DEFAULT_UDP_MTU; + +/// Maximum UDP MTU (we never actually get this high). pub const MAX_UDP_MTU: u32 = ztcore::ZT_MAX_UDP_MTU; #[allow(non_snake_case,non_upper_case_globals)] pub mod RulePacketCharacteristicFlags { - pub const Inbound: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_INBOUND as u64; - pub const Multicast: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST as u64; - pub const Broadcast: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST as u64; - pub const SenderIpAuthenticated: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED as u64; - pub const SenderMacAuthenticated: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED as u64; - pub const TcpFlagNS: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS as u64; - pub const TcpFlagCWR: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR as u64; - pub const TcpFlagECE: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE as u64; - pub const TcpFlagURG: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG as u64; - pub const TcpFlagACK: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK as u64; - pub const TcpFlagPSH: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH as u64; - pub const TcpFlagRST: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST as u64; - pub const TcpFlagSYN: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN as u64; - pub const TcpFlagFIN: u64 = crate::bindings::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN as u64; + pub const Inbound: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_INBOUND as u64; + pub const Multicast: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_MULTICAST as u64; + pub const Broadcast: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_BROADCAST as u64; + pub const SenderIpAuthenticated: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_SENDER_IP_AUTHENTICATED as u64; + pub const SenderMacAuthenticated: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_SENDER_MAC_AUTHENTICATED as u64; + pub const TcpFlagNS: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_NS as u64; + pub const TcpFlagCWR: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_CWR as u64; + pub const TcpFlagECE: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_ECE as u64; + pub const TcpFlagURG: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_URG as u64; + pub const TcpFlagACK: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_ACK as u64; + pub const TcpFlagPSH: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_PSH as u64; + pub const TcpFlagRST: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_RST as u64; + pub const TcpFlagSYN: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_SYN as u64; + pub const TcpFlagFIN: u64 = crate::capi::ZT_RULE_PACKET_CHARACTERISTICS_TCP_FIN as u64; } #[derive(FromPrimitive, ToPrimitive, PartialEq, Eq)] diff --git a/rust-zerotier-core/src/locator.rs b/rust-zerotier-core/src/locator.rs index 1661fd1eb..94ef08388 100644 --- a/rust-zerotier-core/src/locator.rs +++ b/rust-zerotier-core/src/locator.rs @@ -70,6 +70,7 @@ impl Locator { } impl Drop for Locator { + #[inline(always)] fn drop(&mut self) { if self.requires_delete { unsafe { @@ -80,6 +81,7 @@ impl Drop for Locator { } impl Clone for Locator { + #[inline(always)] fn clone(&self) -> Locator { Locator::new_from_string(self.to_string().as_str()).ok().unwrap() } diff --git a/rust-zerotier-core/src/mac.rs b/rust-zerotier-core/src/mac.rs index 833cf0cdf..e7846eaab 100644 --- a/rust-zerotier-core/src/mac.rs +++ b/rust-zerotier-core/src/mac.rs @@ -11,6 +11,8 @@ */ /****/ +use std::cmp::Ordering; + pub struct MAC(pub u64); impl ToString for MAC { @@ -47,6 +49,20 @@ impl PartialEq for MAC { impl Eq for MAC {} +impl Ord for MAC { + #[inline(always)] + fn cmp(&self, other: &Self) -> Ordering { + self.0.cmp(&other.0) + } +} + +impl PartialOrd for MAC { + #[inline(always)] + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.0.cmp(&other.0)) + } +} + impl Clone for MAC { #[inline(always)] fn clone(&self) -> Self { diff --git a/rust-zerotier-core/src/multicastgroup.rs b/rust-zerotier-core/src/multicastgroup.rs new file mode 100644 index 000000000..c69d99100 --- /dev/null +++ b/rust-zerotier-core/src/multicastgroup.rs @@ -0,0 +1,26 @@ +use crate::MAC; +use std::cmp::Ordering; + +#[derive(Clone, PartialEq, Eq)] +pub struct MulticastGroup { + pub mac: MAC, + pub adi: u32, +} + +impl Ord for MulticastGroup { + #[inline(always)] + fn cmp(&self, other: &Self) -> Ordering { + let o1 = self.mac.0.cmp(&other.mac.0); + if o1 == Ordering::Equal { + return self.adi.cmp(&other.adi); + } + o1 + } +} + +impl PartialOrd for MulticastGroup { + #[inline(always)] + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} diff --git a/rust-zerotier-service/src/main.rs b/rust-zerotier-service/src/main.rs index 97d97ff09..dc4323cc3 100644 --- a/rust-zerotier-service/src/main.rs +++ b/rust-zerotier-service/src/main.rs @@ -17,6 +17,10 @@ mod physicallink; mod log; mod store; mod network; +mod vnp; + +#[allow(non_snake_case,non_upper_case_globals,non_camel_case_types,dead_code,improper_ctypes)] +mod osdep; use std::cell::Cell; use std::collections::BTreeMap; diff --git a/rust-zerotier-service/src/vnp/mac_feth_tap.rs b/rust-zerotier-service/src/vnp/mac_feth_tap.rs new file mode 100644 index 000000000..42b86560f --- /dev/null +++ b/rust-zerotier-service/src/vnp/mac_feth_tap.rs @@ -0,0 +1,10 @@ +use crate::vnp::Port; + +pub struct MacFethTap { +} + +impl MacFethTap { + pub fn new() -> MacFethTap { + MacFethTap{} + } +} diff --git a/rust-zerotier-service/src/vnp/mod.rs b/rust-zerotier-service/src/vnp/mod.rs new file mode 100644 index 000000000..4105071c0 --- /dev/null +++ b/rust-zerotier-service/src/vnp/mod.rs @@ -0,0 +1,17 @@ +// VNP = virtual network port, a.k.a. tun/tap device driver interface. + +#[cfg(target_os = "macos")] +mod mac_feth_tap; + +pub trait Port { + fn set_enabled(&self, enabled: bool); + fn is_enabled(&self, ) -> bool; + fn set_ips(&self, zerotier_core::InetAddress: &ip); + fn ips(&self) -> Vec; + fn device_name(&self) -> String; + fn routing_device_name(&self) -> String; + fn set_friendly_name(&self, friendly_name: &str); + fn friendly_name(&self) -> String; + fn get_multicast_groups(&self) -> std::collections::BTreeSet; +} +