From 0acc731bf4c88551d0dc8b58c69bd5a1ca4ca0f5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 19 Apr 2021 17:13:05 -0400 Subject: [PATCH] Simplify Locator slightly. --- core/CAPI.cpp | 27 +++++++++++++++++------- core/Locator.cpp | 30 ++++++++++----------------- core/Locator.hpp | 22 ++++++++++---------- core/zerotier.h | 27 +++++++++++++++--------- rust-zerotier-core/src/buffer.rs | 4 +--- rust-zerotier-core/src/certificate.rs | 13 +++--------- rust-zerotier-core/src/locator.rs | 29 ++++++++++---------------- 7 files changed, 74 insertions(+), 78 deletions(-) diff --git a/core/CAPI.cpp b/core/CAPI.cpp index 01f3c9115..21bdda857 100644 --- a/core/CAPI.cpp +++ b/core/CAPI.cpp @@ -489,13 +489,6 @@ ZT_MAYBE_UNUSED char *ZT_Locator_toString( return reinterpret_cast(loc)->toString(buf); } -ZT_MAYBE_UNUSED const ZT_Fingerprint *ZT_Locator_fingerprint(const ZT_Locator *loc) -{ - if (!loc) - return nullptr; - return (ZT_Fingerprint *) (&(reinterpret_cast(loc)->signer())); -} - ZT_MAYBE_UNUSED int64_t ZT_Locator_revision(const ZT_Locator *loc) { if (!loc) @@ -503,6 +496,26 @@ ZT_MAYBE_UNUSED int64_t ZT_Locator_revision(const ZT_Locator *loc) return reinterpret_cast(loc)->revision(); } +ZT_MAYBE_UNUSED uint64_t ZT_Locator_signer(const ZT_Locator *loc) +{ + if (!loc) + return 0; + return reinterpret_cast(loc)->signer().toInt(); +} + +ZT_MAYBE_UNUSED int ZT_Locator_equals(const ZT_Locator *a, const ZT_Locator *b) +{ + if (a) { + if (b) { + if (*reinterpret_cast(a) == *reinterpret_cast(b)) + return 1; + } + } else if (!b) { + return 1; + } + return 0; +} + ZT_MAYBE_UNUSED unsigned int ZT_Locator_endpointCount(const ZT_Locator *loc) { return (loc) ? (unsigned int) (reinterpret_cast(loc)->endpoints().size()) : 0; } diff --git a/core/Locator.cpp b/core/Locator.cpp index 1c95b5173..46b2c86b9 100644 --- a/core/Locator.cpp +++ b/core/Locator.cpp @@ -49,7 +49,7 @@ bool Locator::add(const Endpoint &ep, const SharedPtr< const EndpointAttributes bool Locator::sign(const int64_t rev, const Identity &id) noexcept { m_revision = rev; - m_signer = id.fingerprint(); + m_signer = id.address(); m_sortEndpoints(); @@ -67,7 +67,7 @@ bool Locator::sign(const int64_t rev, const Identity &id) noexcept bool Locator::verify(const Identity &id) const noexcept { try { - if ((m_revision > 0) && (m_signer == id.fingerprint())) { + if ((m_revision > 0) && (m_signer == id.address())) { uint8_t signdata[ZT_LOCATOR_MARSHAL_SIZE_MAX]; const unsigned int signlen = marshal(signdata, true); return id.verify(signdata, signlen, m_signature.data(), m_signature.size()); @@ -80,7 +80,7 @@ char *Locator::toString(char s[ZT_LOCATOR_STRING_SIZE_MAX]) const noexcept { static_assert(ZT_LOCATOR_STRING_SIZE_MAX > ((((ZT_LOCATOR_MARSHAL_SIZE_MAX / 5) + 1) * 8) + ZT_ADDRESS_LENGTH_HEX + 1), "overflow"); uint8_t bin[ZT_LOCATOR_MARSHAL_SIZE_MAX]; - Address(m_signer.address).toString(s); + m_signer.toString(s); s[ZT_ADDRESS_LENGTH_HEX] = '@'; Utils::b32e(bin, marshal(bin, false), s + (ZT_ADDRESS_LENGTH_HEX + 1), ZT_LOCATOR_STRING_SIZE_MAX - (ZT_ADDRESS_LENGTH_HEX + 1)); return s; @@ -102,17 +102,13 @@ bool Locator::fromString(const char *s) noexcept int Locator::marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX], const bool excludeSignature) const noexcept { Utils::storeBigEndian(data, (uint64_t) m_revision); - int p = 8; + m_signer.copyTo(data + 8); + int p = 8 + ZT_ADDRESS_LENGTH; - int l = m_signer.marshal(data + p); - if (l <= 0) - return -1; - p += l; - - Utils::storeBigEndian(data + p, (uint16_t) m_endpoints.size()); + Utils::storeBigEndian(data + p, (uint16_t)m_endpoints.size()); p += 2; for (Vector< std::pair< Endpoint, SharedPtr< const EndpointAttributes > > >::const_iterator e(m_endpoints.begin());e != m_endpoints.end();++e) { - l = e->first.marshal(data + p); + int l = e->first.marshal(data + p); if (l <= 0) return -1; p += l; @@ -141,15 +137,11 @@ int Locator::marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX], const bool exclu int Locator::unmarshal(const uint8_t *data, const int len) noexcept { - if (unlikely(len < 8)) + if (unlikely(len < (8 + ZT_ADDRESS_LENGTH))) return -1; m_revision = (int64_t)Utils::loadBigEndian(data); - int p = 8; - - int l = m_signer.unmarshal(data + p, len - p); - if (l <= 0) - return -1; - p += l; + m_signer.setTo(data + 8); + int p = 8 + ZT_ADDRESS_LENGTH; if (unlikely(p + 2) > len) return -1; @@ -160,7 +152,7 @@ int Locator::unmarshal(const uint8_t *data, const int len) noexcept m_endpoints.resize(endpointCount); m_endpoints.shrink_to_fit(); for (unsigned int i = 0;i < endpointCount;++i) { - l = m_endpoints[i].first.unmarshal(data + p, len - p); + int l = m_endpoints[i].first.unmarshal(data + p, len - p); if (l <= 0) return -1; p += l; diff --git a/core/Locator.hpp b/core/Locator.hpp index 1167b5c52..bfb27b2d2 100644 --- a/core/Locator.hpp +++ b/core/Locator.hpp @@ -35,7 +35,7 @@ */ #define ZT_LOCATOR_MAX_ENDPOINTS 16 -#define ZT_LOCATOR_MARSHAL_SIZE_MAX (8 + ZT_FINGERPRINT_MARSHAL_SIZE + 2 + (ZT_LOCATOR_MAX_ENDPOINTS * (ZT_ENDPOINT_MARSHAL_SIZE_MAX + ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE)) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE) +#define ZT_LOCATOR_MARSHAL_SIZE_MAX (8 + ZT_ADDRESS_LENGTH + 2 + (ZT_LOCATOR_MAX_ENDPOINTS * (ZT_ENDPOINT_MARSHAL_SIZE_MAX + ZT_LOCATOR_MAX_ENDPOINT_ATTRIBUTES_SIZE)) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE) /** * Maximum size of a string format Locator (this is way larger than needed) @@ -109,16 +109,16 @@ public: m_revision(0) {} - explicit Locator(const char *const str) noexcept; - - ZT_INLINE Locator(const Locator &loc) noexcept: - m_revision(loc.m_revision), - m_signer(loc.m_signer), - m_endpoints(loc.m_endpoints), - m_signature(loc.m_signature), + ZT_INLINE Locator(const Locator &l) noexcept: + m_revision(l.m_revision), + m_signer(l.m_signer), + m_endpoints(l.m_endpoints), + m_signature(l.m_signature), __refCount(0) {} + explicit Locator(const char *const str) noexcept; + /** * @return Timestamp (a.k.a. revision number) set by Location signer */ @@ -126,9 +126,9 @@ public: { return m_revision; } /** - * @return Fingerprint of identity that signed this locator + * @return ZeroTier address of signer */ - ZT_INLINE const Fingerprint &signer() const noexcept + ZT_INLINE Address signer() const noexcept { return m_signer; } /** @@ -232,7 +232,7 @@ private: void m_sortEndpoints() noexcept; int64_t m_revision; - Fingerprint m_signer; + Address m_signer; Vector >> m_endpoints; FCV< uint8_t, ZT_SIGNATURE_BUFFER_SIZE > m_signature; std::atomic< int > __refCount; diff --git a/core/zerotier.h b/core/zerotier.h index 5008f412b..b9b366968 100644 --- a/core/zerotier.h +++ b/core/zerotier.h @@ -2689,16 +2689,6 @@ ZT_SDK_API char *ZT_Locator_toString( char *buf, int capacity); -/** - * Get a pointer to the fingerprint of this locator's signer. - * - * The returned pointer remains valid as long as the Locator is not deleted. - * - * @param loc Locator to query - * @return Pointer to fingerprint of signer - */ -ZT_SDK_API const ZT_Fingerprint *ZT_Locator_fingerprint(const ZT_Locator *loc); - /** * Get a locator's revision * @@ -2707,6 +2697,23 @@ ZT_SDK_API const ZT_Fingerprint *ZT_Locator_fingerprint(const ZT_Locator *loc); */ ZT_SDK_API int64_t ZT_Locator_revision(const ZT_Locator *loc); +/** + * Get a locator's signer + * + * @param loc Locator to query + * @return 40-bit ZeroTier address of signer + */ +ZT_SDK_API uint64_t ZT_Locator_signer(const ZT_Locator *loc); + +/** + * Compare two locators + * + * @param a First locator + * @param b Second locator + * @return Non-zero if a equals b + */ +ZT_SDK_API int ZT_Locator_equals(const ZT_Locator *a, const ZT_Locator *b); + /** * Get the number of endpoints in this locator * diff --git a/rust-zerotier-core/src/buffer.rs b/rust-zerotier-core/src/buffer.rs index d4ce7e8ae..44f2120d3 100644 --- a/rust-zerotier-core/src/buffer.rs +++ b/rust-zerotier-core/src/buffer.rs @@ -84,8 +84,6 @@ impl Buffer { impl Drop for Buffer { #[inline(always)] fn drop(&mut self) { - unsafe { - ztcore::ZT_freeBuffer(self.zt_core_buf as *mut c_void); - } + unsafe { ztcore::ZT_freeBuffer(self.zt_core_buf as *mut c_void); } } } diff --git a/rust-zerotier-core/src/certificate.rs b/rust-zerotier-core/src/certificate.rs index 3b6d9ee77..3a58733a0 100644 --- a/rust-zerotier-core/src/certificate.rs +++ b/rust-zerotier-core/src/certificate.rs @@ -19,7 +19,7 @@ use std::pin::Pin; use std::ptr::{copy_nonoverlapping, null, null_mut}; use num_derive::{FromPrimitive, ToPrimitive}; -use num_traits::FromPrimitive; +use num_traits::{FromPrimitive, ToPrimitive}; use serde::{Deserialize, Serialize}; use crate::*; @@ -42,19 +42,12 @@ fn vec_to_array(v: &Vec) -> [u8; L] { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -#[derive(FromPrimitive, PartialEq, Eq, Clone, Copy)] +#[derive(FromPrimitive, ToPrimitive, PartialEq, Eq, Clone, Copy)] pub enum CertificatePublicKeyAlgorithm { None = ztcore::ZT_CertificatePublicKeyAlgorithm_ZT_CERTIFICATE_PUBLIC_KEY_ALGORITHM_NONE as isize, ECDSANistP384 = ztcore::ZT_CertificatePublicKeyAlgorithm_ZT_CERTIFICATE_PUBLIC_KEY_ALGORITHM_ECDSA_NIST_P_384 as isize, } -impl CertificatePublicKeyAlgorithm { - #[inline(always)] - pub fn to_i32(&self) -> i32 { - *self as i32 - } -} - impl From for CertificatePublicKeyAlgorithm { #[inline(always)] fn from(n: i32) -> CertificatePublicKeyAlgorithm { @@ -534,7 +527,7 @@ impl Certificate { let mut private_key = [0_u8; ztcore::ZT_CERTIFICATE_MAX_PRIVATE_KEY_SIZE as usize]; let mut public_key_size: c_int = 0; let mut private_key_size: c_int = 0; - let r = unsafe { ztcore::ZT_Certificate_newKeyPair(alg.to_i32() as ztcore::ZT_CertificatePublicKeyAlgorithm, public_key.as_mut_ptr(), &mut public_key_size, private_key.as_mut_ptr(), &mut private_key_size) }; + let r = unsafe { ztcore::ZT_Certificate_newKeyPair(alg.to_i32().unwrap() as ztcore::ZT_CertificatePublicKeyAlgorithm, public_key.as_mut_ptr(), &mut public_key_size, private_key.as_mut_ptr(), &mut private_key_size) }; if r == 0 { if public_key_size > 0 && private_key_size > 0 { Ok((public_key[0..public_key_size as usize].to_vec(), private_key[0..private_key_size as usize].to_vec())) diff --git a/rust-zerotier-core/src/locator.rs b/rust-zerotier-core/src/locator.rs index 34ff35c45..97fea4d81 100644 --- a/rust-zerotier-core/src/locator.rs +++ b/rust-zerotier-core/src/locator.rs @@ -45,7 +45,7 @@ impl Locator { pub(crate) fn new_from_capi(l: *const ztcore::ZT_Locator, requires_delete: bool) -> Locator { Locator{ capi: l, - requires_delete: requires_delete + requires_delete } } @@ -66,9 +66,12 @@ impl Locator { #[inline(always)] pub fn revision(&self) -> i64 { - unsafe { - ztcore::ZT_Locator_revision(self.capi) as i64 - } + unsafe { ztcore::ZT_Locator_revision(self.capi) as i64 } + } + + #[inline(always)] + pub fn signer(&self) -> Address { + unsafe { Address(ztcore::ZT_Locator_signer(self.capi)) } } pub fn endpoints(&self) -> Vec { @@ -86,28 +89,17 @@ impl Locator { eps } + #[inline(always)] pub fn verify(&self, id: &Identity) -> bool { unsafe { ztcore::ZT_Locator_verify(self.capi, id.capi) != 0 } } - - pub fn signer(&self) -> Fingerprint { - unsafe { - let fp = ztcore::ZT_Locator_fingerprint(self.capi); - if fp.is_null() { - panic!("ZT_Locator_fingerprint() returned null, should not be allowed"); - } - Fingerprint::new_from_capi(&*fp) - } - } } impl Drop for Locator { #[inline(always)] fn drop(&mut self) { if self.requires_delete { - unsafe { - ztcore::ZT_Locator_delete(self.capi); - } + unsafe { ztcore::ZT_Locator_delete(self.capi); } } } } @@ -132,8 +124,9 @@ impl ToString for Locator { } impl PartialEq for Locator { + #[inline(always)] fn eq(&self, other: &Locator) -> bool { - self.to_string() == other.to_string() + unsafe { ztcore::ZT_Locator_equals(self.capi, other.capi) != 0 } } }