From 1affb6814c97e5ec10978f21e0a7323d51f807f9 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 19 Feb 2020 10:32:02 -0800 Subject: [PATCH] A little more cleanup and build fixes. --- node/Endpoint.hpp | 6 +- node/InetAddress.cpp | 4 +- node/Utils.hpp | 206 +++++++++++++++++++++++-------------------- 3 files changed, 118 insertions(+), 98 deletions(-) diff --git a/node/Endpoint.hpp b/node/Endpoint.hpp index 9d18223a0..ec7266c5a 100644 --- a/node/Endpoint.hpp +++ b/node/Endpoint.hpp @@ -57,7 +57,7 @@ public: ZT_ALWAYS_INLINE Endpoint() noexcept { memoryZero(this); } - ZT_ALWAYS_INLINE Endpoint(const InetAddress &sa) + explicit ZT_ALWAYS_INLINE Endpoint(const InetAddress &sa) { switch (sa.ss_family) { case AF_INET: @@ -88,7 +88,9 @@ public: explicit ZT_ALWAYS_INLINE Endpoint(const char *url) : _t(TYPE_URL) - { Utils::scopy(_v.url,sizeof(_v.url),url); } + { + Utils::scopy(_v.url,sizeof(_v.url),url); + } /** * @return InetAddress or NIL if not of this type diff --git a/node/InetAddress.cpp b/node/InetAddress.cpp index b7c7e2f45..16680e933 100644 --- a/node/InetAddress.cpp +++ b/node/InetAddress.cpp @@ -192,9 +192,9 @@ bool InetAddress::fromString(const char *ipSlashPort) noexcept in->sin_family = AF_INET; in->sin_port = Utils::hton((uint16_t)port); return true; - } else { - return false; } + + return false; } InetAddress InetAddress::netmask() const noexcept diff --git a/node/Utils.hpp b/node/Utils.hpp index f09c89b5b..aaa837e81 100644 --- a/node/Utils.hpp +++ b/node/Utils.hpp @@ -91,8 +91,32 @@ char *decimal(unsigned long n,char s[24]) noexcept; * @return Pointer to s containing hex string with trailing zero byte */ char *hex(uint8_t i,char s[3]) noexcept; + +/** + * Convert an unsigned integer into hex + * + * @param i Any unsigned integer + * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur. + * @return Pointer to s containing hex string with trailing zero byte + */ char *hex(uint16_t i,char s[5]) noexcept; + +/** + * Convert an unsigned integer into hex + * + * @param i Any unsigned integer + * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur. + * @return Pointer to s containing hex string with trailing zero byte + */ char *hex(uint32_t i,char s[9]) noexcept; + +/** + * Convert an unsigned integer into hex + * + * @param i Any unsigned integer + * @param s Buffer to receive hex, must be at least (2*sizeof(i))+1 in size or overflow will occur. + * @return Pointer to s containing hex string with trailing zero byte + */ char *hex(uint64_t i,char s[17]) noexcept; /** @@ -240,15 +264,9 @@ static ZT_ALWAYS_INLINE char *stok(char *str,const char *delim,char **saveptr) n #endif } -static ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) noexcept { return (unsigned int)strtoul(s,nullptr,10); } - -static ZT_ALWAYS_INLINE unsigned long long strToU64(const char *s) noexcept +static ZT_ALWAYS_INLINE unsigned int strToUInt(const char *s) noexcept { -#ifdef __WINDOWS__ - return (unsigned long long)_strtoui64(s,(char **)0,10); -#else - return strtoull(s,nullptr,10); -#endif + return (unsigned int)strtoul(s,nullptr,10); } static ZT_ALWAYS_INLINE unsigned long long hexStrToU64(const char *s) noexcept @@ -282,6 +300,92 @@ static ZT_ALWAYS_INLINE unsigned long hashString(const void *restrict key,const return h; } +/** + * Decode a big-endian value from a byte stream + * + * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t) + * @param p Byte stream, must be at least sizeof(I) in size + * @return Decoded integer + */ +template +static ZT_ALWAYS_INLINE I loadBigEndian(const void *const p) noexcept +{ +#ifdef ZT_NO_UNALIGNED_ACCESS + I x = (I)0; + for(unsigned int k=0;k(&x)[k] = reinterpret_cast(p)[(sizeof(I)-1)-k]; +#else + reinterpret_cast(&x)[k] = reinterpret_cast(p)[k]; +#endif + } + return x; +#else + return ntoh(*reinterpret_cast(p)); +#endif +} + +/** + * Save an integer in big-endian format + * + * @tparam I Integer type to store (usually inferred) + * @param p Byte stream to write (must be at least sizeof(I)) + * #param i Integer to write + */ +template +static ZT_ALWAYS_INLINE void storeBigEndian(void *const p,const I i) noexcept +{ +#ifdef ZT_NO_UNALIGNED_ACCESS + for(unsigned int k=0;k(p)[k] = reinterpret_cast(&i)[(sizeof(I)-1)-k]; +#else + reinterpret_cast(p)[k] = reinterpret_cast(&i)[k]; +#endif + } +#else + *reinterpret_cast(p) = hton(i); +#endif +} + +/** + * Copy bits from memory into an integer type without modifying their order + * + * @tparam I Type to load + * @param p Byte stream, must be at least sizeof(I) in size + * @return Loaded raw integer + */ +template +static ZT_ALWAYS_INLINE I loadAsIsEndian(const void *const p) noexcept +{ +#ifdef ZT_NO_UNALIGNED_ACCESS + I x = (I)0; + for(unsigned int k=0;k(&x)[k] = reinterpret_cast(p)[k]; + return x; +#else + return *reinterpret_cast(p); +#endif +} + +/** + * Copy bits from memory into an integer type without modifying their order + * + * @tparam I Type to store + * @param p Byte array (must be at least sizeof(I)) + * @param i Integer to store + */ +template +static ZT_ALWAYS_INLINE void storeAsIsEndian(void *const p,const I i) noexcept +{ +#ifdef ZT_NO_UNALIGNED_ACCESS + for(unsigned int k=0;k(p)[k] = reinterpret_cast(&i)[k]; +#else + *reinterpret_cast(p) = i; +#endif +} + #ifdef __GNUC__ static ZT_ALWAYS_INLINE unsigned int countBits(const uint8_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); } static ZT_ALWAYS_INLINE unsigned int countBits(const uint16_t v) noexcept { return (unsigned int)__builtin_popcount((unsigned int)v); } @@ -410,92 +514,6 @@ template static ZT_ALWAYS_INLINE T ntoh(T n) noexcept { return n; } #endif -/** - * Decode a big-endian value from a byte stream - * - * @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t) - * @param p Byte stream, must be at least sizeof(I) in size - * @return Decoded integer - */ -template -static ZT_ALWAYS_INLINE I loadBigEndian(const void *const p) noexcept -{ -#ifdef ZT_NO_UNALIGNED_ACCESS - I x = (I)0; - for(unsigned int k=0;k(&x)[k] = reinterpret_cast(p)[(sizeof(I)-1)-k]; -#else - reinterpret_cast(&x)[k] = reinterpret_cast(p)[k]; -#endif - } - return x; -#else - return ntoh(*reinterpret_cast(p)); -#endif -} - -/** - * Save an integer in big-endian format - * - * @tparam I Integer type to store (usually inferred) - * @param p Byte stream to write (must be at least sizeof(I)) - * #param i Integer to write - */ -template -static ZT_ALWAYS_INLINE void storeBigEndian(void *const p,const I i) noexcept -{ -#ifdef ZT_NO_UNALIGNED_ACCESS - for(unsigned int k=0;k(p)[k] = reinterpret_cast(&i)[(sizeof(I)-1)-k]; -#else - reinterpret_cast(p)[k] = reinterpret_cast(&i)[k]; -#endif - } -#else - *reinterpret_cast(p) = hton(i); -#endif -} - -/** - * Copy bits from memory into an integer type without modifying their order - * - * @tparam I Type to load - * @param p Byte stream, must be at least sizeof(I) in size - * @return Loaded raw integer - */ -template -static ZT_ALWAYS_INLINE I loadAsIsEndian(const void *const p) noexcept -{ -#ifdef ZT_NO_UNALIGNED_ACCESS - I x = (I)0; - for(unsigned int k=0;k(&x)[k] = reinterpret_cast(p)[k]; - return x; -#else - return *reinterpret_cast(p); -#endif -} - -/** - * Copy bits from memory into an integer type without modifying their order - * - * @tparam I Type to store - * @param p Byte array (must be at least sizeof(I)) - * @param i Integer to store - */ -template -static ZT_ALWAYS_INLINE void storeAsIsEndian(void *const p,const I i) noexcept -{ -#ifdef ZT_NO_UNALIGNED_ACCESS - for(unsigned int k=0;k(p)[k] = reinterpret_cast(&i)[k]; -#else - *reinterpret_cast(p) = i; -#endif -} - } // namespace Utils } // namespace ZeroTier