2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
2019-01-14 18:25:53 +00:00
|
|
|
* Copyright (C) 2011-2019 ZeroTier, Inc. https://www.zerotier.com/
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2019-01-14 18:25:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-04-28 03:47:25 +00:00
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* You can be released from the requirements of the license by purchasing
|
|
|
|
* a commercial license. Buying such a license is mandatory as soon as you
|
|
|
|
* develop commercial closed-source software that incorporates or links
|
|
|
|
* directly against ZeroTier software without disclosing the source code
|
|
|
|
* of your own application.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
|
|
|
|
2013-12-07 00:49:20 +00:00
|
|
|
#ifndef ZT_UTILS_HPP
|
|
|
|
#define ZT_UTILS_HPP
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2013-08-28 20:01:27 +00:00
|
|
|
#include <string.h>
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <time.h>
|
2013-07-17 18:10:44 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2013-07-25 21:53:57 +00:00
|
|
|
#include <map>
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-08-13 01:25:36 +00:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Miscellaneous utility functions and global constants
|
|
|
|
*/
|
|
|
|
class Utils
|
|
|
|
{
|
|
|
|
public:
|
2019-08-14 19:48:45 +00:00
|
|
|
/**
|
|
|
|
* Hexadecimal characters 0-f
|
|
|
|
*/
|
|
|
|
static const char HEXCHARS[16];
|
|
|
|
|
2013-09-13 20:53:47 +00:00
|
|
|
/**
|
|
|
|
* Perform a time-invariant binary comparison
|
|
|
|
*
|
|
|
|
* @param a First binary string
|
|
|
|
* @param b Second binary string
|
|
|
|
* @param len Length of strings
|
|
|
|
* @return True if strings are equal
|
|
|
|
*/
|
|
|
|
static inline bool secureEq(const void *a,const void *b,unsigned int len)
|
|
|
|
{
|
2016-03-30 23:49:21 +00:00
|
|
|
uint8_t diff = 0;
|
2015-07-07 17:59:59 +00:00
|
|
|
for(unsigned int i=0;i<len;++i)
|
2016-03-30 23:49:21 +00:00
|
|
|
diff |= ( (reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i] );
|
2015-07-07 17:59:59 +00:00
|
|
|
return (diff == 0);
|
2013-09-13 20:53:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 07:14:12 +00:00
|
|
|
/**
|
2019-08-14 19:48:45 +00:00
|
|
|
* Zero memory, ensuring to avoid any compiler optimizations or other things that may stop this.
|
2014-04-18 07:14:12 +00:00
|
|
|
*/
|
2017-02-13 17:03:48 +00:00
|
|
|
static void burn(void *ptr,unsigned int len);
|
2014-04-18 07:14:12 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2017-07-06 23:11:11 +00:00
|
|
|
* @param n Number to convert
|
|
|
|
* @param s Buffer, at least 24 bytes in size
|
|
|
|
* @return String containing 'n' in base 10 form
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2017-07-06 23:11:11 +00:00
|
|
|
static char *decimal(unsigned long n,char s[24]);
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2017-07-07 13:50:40 +00:00
|
|
|
static inline char *hex(uint64_t i,char s[17])
|
2017-07-06 23:11:11 +00:00
|
|
|
{
|
|
|
|
s[0] = HEXCHARS[(i >> 60) & 0xf];
|
|
|
|
s[1] = HEXCHARS[(i >> 56) & 0xf];
|
|
|
|
s[2] = HEXCHARS[(i >> 52) & 0xf];
|
|
|
|
s[3] = HEXCHARS[(i >> 48) & 0xf];
|
|
|
|
s[4] = HEXCHARS[(i >> 44) & 0xf];
|
|
|
|
s[5] = HEXCHARS[(i >> 40) & 0xf];
|
|
|
|
s[6] = HEXCHARS[(i >> 36) & 0xf];
|
|
|
|
s[7] = HEXCHARS[(i >> 32) & 0xf];
|
|
|
|
s[8] = HEXCHARS[(i >> 28) & 0xf];
|
|
|
|
s[9] = HEXCHARS[(i >> 24) & 0xf];
|
|
|
|
s[10] = HEXCHARS[(i >> 20) & 0xf];
|
|
|
|
s[11] = HEXCHARS[(i >> 16) & 0xf];
|
|
|
|
s[12] = HEXCHARS[(i >> 12) & 0xf];
|
|
|
|
s[13] = HEXCHARS[(i >> 8) & 0xf];
|
|
|
|
s[14] = HEXCHARS[(i >> 4) & 0xf];
|
|
|
|
s[15] = HEXCHARS[i & 0xf];
|
|
|
|
s[16] = (char)0;
|
|
|
|
return s;
|
|
|
|
}
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2017-07-07 13:50:40 +00:00
|
|
|
static inline char *hex10(uint64_t i,char s[11])
|
2017-07-06 23:11:11 +00:00
|
|
|
{
|
|
|
|
s[0] = HEXCHARS[(i >> 36) & 0xf];
|
|
|
|
s[1] = HEXCHARS[(i >> 32) & 0xf];
|
|
|
|
s[2] = HEXCHARS[(i >> 28) & 0xf];
|
|
|
|
s[3] = HEXCHARS[(i >> 24) & 0xf];
|
|
|
|
s[4] = HEXCHARS[(i >> 20) & 0xf];
|
|
|
|
s[5] = HEXCHARS[(i >> 16) & 0xf];
|
|
|
|
s[6] = HEXCHARS[(i >> 12) & 0xf];
|
|
|
|
s[7] = HEXCHARS[(i >> 8) & 0xf];
|
|
|
|
s[8] = HEXCHARS[(i >> 4) & 0xf];
|
|
|
|
s[9] = HEXCHARS[i & 0xf];
|
|
|
|
s[10] = (char)0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-07-07 13:50:40 +00:00
|
|
|
static inline char *hex(uint32_t i,char s[9])
|
|
|
|
{
|
|
|
|
s[0] = HEXCHARS[(i >> 28) & 0xf];
|
|
|
|
s[1] = HEXCHARS[(i >> 24) & 0xf];
|
|
|
|
s[2] = HEXCHARS[(i >> 20) & 0xf];
|
|
|
|
s[3] = HEXCHARS[(i >> 16) & 0xf];
|
|
|
|
s[4] = HEXCHARS[(i >> 12) & 0xf];
|
|
|
|
s[5] = HEXCHARS[(i >> 8) & 0xf];
|
|
|
|
s[6] = HEXCHARS[(i >> 4) & 0xf];
|
|
|
|
s[7] = HEXCHARS[i & 0xf];
|
|
|
|
s[8] = (char)0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *hex(uint16_t i,char s[5])
|
2017-07-06 23:11:11 +00:00
|
|
|
{
|
|
|
|
s[0] = HEXCHARS[(i >> 12) & 0xf];
|
|
|
|
s[1] = HEXCHARS[(i >> 8) & 0xf];
|
|
|
|
s[2] = HEXCHARS[(i >> 4) & 0xf];
|
|
|
|
s[3] = HEXCHARS[i & 0xf];
|
|
|
|
s[4] = (char)0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-07-07 13:50:40 +00:00
|
|
|
static inline char *hex(uint8_t i,char s[3])
|
2017-07-06 23:11:11 +00:00
|
|
|
{
|
|
|
|
s[0] = HEXCHARS[(i >> 4) & 0xf];
|
|
|
|
s[1] = HEXCHARS[i & 0xf];
|
|
|
|
s[2] = (char)0;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char *hex(const void *d,unsigned int l,char *s)
|
|
|
|
{
|
2017-07-13 23:31:16 +00:00
|
|
|
char *const save = s;
|
2017-07-06 23:11:11 +00:00
|
|
|
for(unsigned int i=0;i<l;++i) {
|
2017-07-13 22:08:57 +00:00
|
|
|
const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
|
|
|
|
*(s++) = HEXCHARS[b >> 4];
|
2017-07-06 23:11:11 +00:00
|
|
|
*(s++) = HEXCHARS[b & 0xf];
|
|
|
|
}
|
|
|
|
*s = (char)0;
|
|
|
|
return save;
|
|
|
|
}
|
|
|
|
|
2019-08-14 19:48:45 +00:00
|
|
|
static unsigned int unhex(const char *h,void *buf,unsigned int buflen);
|
|
|
|
static unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen);
|
2018-05-31 00:45:29 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
2013-12-12 19:33:41 +00:00
|
|
|
* Generate secure random bytes
|
|
|
|
*
|
|
|
|
* This will try to use whatever OS sources of entropy are available. It's
|
|
|
|
* guarded by an internal mutex so it's thread-safe.
|
|
|
|
*
|
2013-07-04 20:56:19 +00:00
|
|
|
* @param buf Buffer to fill
|
|
|
|
* @param bytes Number of random bytes to generate
|
|
|
|
*/
|
|
|
|
static void getSecureRandom(void *buf,unsigned int bytes);
|
|
|
|
|
2019-08-14 19:48:45 +00:00
|
|
|
/**
|
|
|
|
* Get a 64-bit unsigned secure random number
|
|
|
|
*/
|
|
|
|
static inline uint64_t getSecureRandom64()
|
|
|
|
{
|
|
|
|
uint64_t x;
|
|
|
|
getSecureRandom(&x,sizeof(x));
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2019-08-07 21:41:58 +00:00
|
|
|
static int b32e(const uint8_t *data,int length,char *result,int bufSize);
|
2019-08-14 19:48:45 +00:00
|
|
|
static int b32d(const char *encoded, uint8_t *result, int bufSize);
|
2019-07-17 21:53:33 +00:00
|
|
|
|
2019-08-08 03:35:17 +00:00
|
|
|
static inline unsigned int b64MaxEncodedSize(const unsigned int s) { return ((((s + 2) / 3) * 4) + 1); }
|
|
|
|
static unsigned int b64e(const uint8_t *in,unsigned int inlen,char *out,unsigned int outlen);
|
|
|
|
static unsigned int b64d(const char *in,uint8_t *out,unsigned int outlen);
|
|
|
|
|
2019-08-14 19:48:45 +00:00
|
|
|
/**
|
|
|
|
* Get a non-cryptographic random integer
|
|
|
|
*/
|
|
|
|
static uint64_t random();
|
|
|
|
|
|
|
|
static inline float normalize(float value, int64_t bigMin, int64_t bigMax, int32_t targetMin, int32_t targetMax)
|
|
|
|
{
|
|
|
|
int64_t bigSpan = bigMax - bigMin;
|
|
|
|
int64_t smallSpan = targetMax - targetMin;
|
|
|
|
float valueScaled = (value - (float)bigMin) / (float)bigSpan;
|
|
|
|
return (float)targetMin + valueScaled * (float)smallSpan;
|
|
|
|
}
|
|
|
|
|
2013-08-28 19:09:49 +00:00
|
|
|
/**
|
2013-09-16 17:02:10 +00:00
|
|
|
* Tokenize a string (alias for strtok_r or strtok_s depending on platform)
|
2013-08-28 19:09:49 +00:00
|
|
|
*
|
|
|
|
* @param str String to split
|
|
|
|
* @param delim Delimiters
|
|
|
|
* @param saveptr Pointer to a char * for temporary reentrant storage
|
|
|
|
*/
|
|
|
|
static inline char *stok(char *str,const char *delim,char **saveptr)
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return strtok_s(str,delim,saveptr);
|
|
|
|
#else
|
|
|
|
return strtok_r(str,delim,saveptr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
|
|
|
|
static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
|
|
|
|
static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
|
|
|
|
static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long long strToU64(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
{
|
2013-09-04 13:27:56 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-09-07 16:23:53 +00:00
|
|
|
return (unsigned long long)_strtoui64(s,(char **)0,10);
|
2013-09-04 13:27:56 +00:00
|
|
|
#else
|
2013-08-28 20:01:27 +00:00
|
|
|
return strtoull(s,(char **)0,10);
|
2013-09-07 16:23:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline long long strTo64(const char *s)
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return (long long)_strtoi64(s,(char **)0,10);
|
|
|
|
#else
|
|
|
|
return strtoll(s,(char **)0,10);
|
2013-09-04 13:27:56 +00:00
|
|
|
#endif
|
2013-08-28 20:01:27 +00:00
|
|
|
}
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
|
|
|
|
static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
|
|
|
|
static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
|
|
|
|
static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
|
2013-09-04 13:27:56 +00:00
|
|
|
static inline unsigned long long hexStrToU64(const char *s)
|
2013-08-28 20:01:27 +00:00
|
|
|
{
|
2013-09-04 13:27:56 +00:00
|
|
|
#ifdef __WINDOWS__
|
2013-09-07 16:23:53 +00:00
|
|
|
return (unsigned long long)_strtoui64(s,(char **)0,16);
|
2013-09-04 13:27:56 +00:00
|
|
|
#else
|
2013-08-28 20:01:27 +00:00
|
|
|
return strtoull(s,(char **)0,16);
|
2013-09-07 16:23:53 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
static inline long long hexStrTo64(const char *s)
|
|
|
|
{
|
|
|
|
#ifdef __WINDOWS__
|
|
|
|
return (long long)_strtoi64(s,(char **)0,16);
|
|
|
|
#else
|
|
|
|
return strtoll(s,(char **)0,16);
|
2013-09-04 13:27:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2013-08-28 20:01:27 +00:00
|
|
|
|
2013-08-28 19:09:49 +00:00
|
|
|
/**
|
2016-06-15 22:02:40 +00:00
|
|
|
* Perform a safe C string copy, ALWAYS null-terminating the result
|
2013-08-28 19:09:49 +00:00
|
|
|
*
|
2016-06-15 22:02:40 +00:00
|
|
|
* This will never ever EVER result in dest[] not being null-terminated
|
|
|
|
* regardless of any input parameter (other than len==0 which is invalid).
|
|
|
|
*
|
|
|
|
* @param dest Destination buffer (must not be NULL)
|
|
|
|
* @param len Length of dest[] (if zero, false is returned and nothing happens)
|
|
|
|
* @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
|
2013-08-28 19:09:49 +00:00
|
|
|
* @return True on success, false on overflow (buffer will still be 0-terminated)
|
|
|
|
*/
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline bool scopy(char *dest,unsigned int len,const char *src)
|
|
|
|
{
|
|
|
|
if (!len)
|
|
|
|
return false; // sanity check
|
|
|
|
if (!src) {
|
|
|
|
*dest = (char)0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
char *end = dest + len;
|
|
|
|
while ((*dest++ = *src++)) {
|
|
|
|
if (dest == end) {
|
|
|
|
*(--dest) = (char)0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-28 19:09:49 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
/**
|
|
|
|
* Count the number of bits set in an integer
|
|
|
|
*
|
2019-08-14 17:35:57 +00:00
|
|
|
* @param v Unsigned integer
|
|
|
|
* @return Number of bits set in this integer (0-bits in integer)
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-08-14 17:35:57 +00:00
|
|
|
template<typename T>
|
|
|
|
static inline uint64_t countBits(T v)
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2019-08-14 17:35:57 +00:00
|
|
|
v = v - ((v >> 1) & (T)~(T)0/3);
|
|
|
|
v = (v & (T)~(T)0/15*3) + ((v >> 2) & (T)~(T)0/15*3);
|
|
|
|
v = (v + (v >> 4)) & (T)~(T)0/255*15;
|
|
|
|
return (T)(v * ((~((T)0))/((T)255))) >> ((sizeof(T) - 1) * 8);
|
2017-03-01 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2013-11-06 15:38:19 +00:00
|
|
|
// Byte swappers for big/little endian conversion
|
2019-08-14 19:48:45 +00:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline uint8_t hton(uint8_t n) { return n; }
|
|
|
|
static inline int8_t hton(int8_t n) { return n; }
|
|
|
|
static inline uint16_t hton(uint16_t n) { return htons(n); }
|
|
|
|
static inline int16_t hton(int16_t n) { return (int16_t)htons((uint16_t)n); }
|
|
|
|
static inline uint32_t hton(uint32_t n) { return htonl(n); }
|
|
|
|
static inline int32_t hton(int32_t n) { return (int32_t)htonl((uint32_t)n); }
|
2013-07-04 20:56:19 +00:00
|
|
|
static inline uint64_t hton(uint64_t n)
|
|
|
|
{
|
2018-06-22 21:05:53 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
return bswap64(n);
|
|
|
|
#elif (!defined(__OpenBSD__))
|
2013-07-04 20:56:19 +00:00
|
|
|
return __builtin_bswap64(n);
|
2018-06-22 21:05:53 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
#else
|
|
|
|
return (
|
2016-01-12 22:04:55 +00:00
|
|
|
((n & 0x00000000000000FFULL) << 56) |
|
|
|
|
((n & 0x000000000000FF00ULL) << 40) |
|
|
|
|
((n & 0x0000000000FF0000ULL) << 24) |
|
|
|
|
((n & 0x00000000FF000000ULL) << 8) |
|
|
|
|
((n & 0x000000FF00000000ULL) >> 8) |
|
|
|
|
((n & 0x0000FF0000000000ULL) >> 24) |
|
|
|
|
((n & 0x00FF000000000000ULL) >> 40) |
|
2013-07-04 20:56:19 +00:00
|
|
|
((n & 0xFF00000000000000ULL) >> 56)
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
}
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); }
|
2019-08-14 19:48:45 +00:00
|
|
|
#else
|
|
|
|
template<typename T>
|
|
|
|
static inline T hton(T n) { return n; }
|
|
|
|
#endif
|
2017-07-07 00:32:41 +00:00
|
|
|
|
2019-08-14 19:48:45 +00:00
|
|
|
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline uint8_t ntoh(uint8_t n) { return n; }
|
|
|
|
static inline int8_t ntoh(int8_t n) { return n; }
|
|
|
|
static inline uint16_t ntoh(uint16_t n) { return ntohs(n); }
|
|
|
|
static inline int16_t ntoh(int16_t n) { return (int16_t)ntohs((uint16_t)n); }
|
|
|
|
static inline uint32_t ntoh(uint32_t n) { return ntohl(n); }
|
|
|
|
static inline int32_t ntoh(int32_t n) { return (int32_t)ntohl((uint32_t)n); }
|
2013-07-04 20:56:19 +00:00
|
|
|
static inline uint64_t ntoh(uint64_t n)
|
|
|
|
{
|
2018-06-22 21:05:53 +00:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
return bswap64(n);
|
|
|
|
#elif (!defined(__OpenBSD__))
|
2013-07-04 20:56:19 +00:00
|
|
|
return __builtin_bswap64(n);
|
2018-06-22 21:05:53 +00:00
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
#else
|
|
|
|
return (
|
2016-01-12 22:04:55 +00:00
|
|
|
((n & 0x00000000000000FFULL) << 56) |
|
|
|
|
((n & 0x000000000000FF00ULL) << 40) |
|
|
|
|
((n & 0x0000000000FF0000ULL) << 24) |
|
|
|
|
((n & 0x00000000FF000000ULL) << 8) |
|
|
|
|
((n & 0x000000FF00000000ULL) >> 8) |
|
|
|
|
((n & 0x0000FF0000000000ULL) >> 24) |
|
|
|
|
((n & 0x00FF000000000000ULL) >> 40) |
|
2013-07-04 20:56:19 +00:00
|
|
|
((n & 0xFF00000000000000ULL) >> 56)
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
}
|
2017-07-07 00:32:41 +00:00
|
|
|
static inline int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); }
|
2019-08-14 19:48:45 +00:00
|
|
|
#else
|
|
|
|
template<typename T>
|
|
|
|
static inline T ntoh(T n) { return n; }
|
|
|
|
#endif
|
2013-07-04 20:56:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|
|
|
|
|
|
|
|
#endif
|