Utils.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_UTILS_HPP
  27. #define ZT_UTILS_HPP
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <string>
  34. #include <stdexcept>
  35. #include <vector>
  36. #include <map>
  37. #include "Constants.hpp"
  38. #ifdef __LINUX__
  39. //#if (defined(_MSC_VER) || defined(__GNUC__)) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  40. #if 0
  41. #include <emmintrin.h>
  42. static inline void ZT_FAST_MEMCPY(void *a,const void *b,unsigned long k)
  43. {
  44. char *aa = reinterpret_cast<char *>(a);
  45. const char *bb = reinterpret_cast<const char *>(b);
  46. while (k >= 64) {
  47. __m128 t1 = _mm_loadu_ps(reinterpret_cast<const float *>(bb));
  48. __m128 t2 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 16));
  49. __m128 t3 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 32));
  50. __m128 t4 = _mm_loadu_ps(reinterpret_cast<const float *>(bb + 48));
  51. _mm_storeu_ps(reinterpret_cast<float *>(aa),t1);
  52. _mm_storeu_ps(reinterpret_cast<float *>(aa + 16),t2);
  53. _mm_storeu_ps(reinterpret_cast<float *>(aa + 32),t3);
  54. _mm_storeu_ps(reinterpret_cast<float *>(aa + 48),t4);
  55. bb += 64;
  56. aa += 64;
  57. k -= 64;
  58. }
  59. while (k >= 16) {
  60. __m128 t1 = _mm_loadu_ps(reinterpret_cast<const float *>(bb));
  61. _mm_storeu_ps(reinterpret_cast<float *>(aa),t1);
  62. bb += 16;
  63. aa += 16;
  64. k -= 16;
  65. }
  66. for(unsigned long i=0;i<k;++i)
  67. aa[i] = bb[i];
  68. }
  69. #else
  70. #define ZT_FAST_MEMCPY(a,b,c) memcpy(a,b,c)
  71. #endif
  72. #else
  73. #define ZT_FAST_MEMCPY(a,b,c) memcpy(a,b,c)
  74. #endif
  75. namespace ZeroTier {
  76. /**
  77. * Miscellaneous utility functions and global constants
  78. */
  79. class Utils
  80. {
  81. public:
  82. /**
  83. * Perform a time-invariant binary comparison
  84. *
  85. * @param a First binary string
  86. * @param b Second binary string
  87. * @param len Length of strings
  88. * @return True if strings are equal
  89. */
  90. static inline bool secureEq(const void *a,const void *b,unsigned int len)
  91. {
  92. uint8_t diff = 0;
  93. for(unsigned int i=0;i<len;++i)
  94. diff |= ( (reinterpret_cast<const uint8_t *>(a))[i] ^ (reinterpret_cast<const uint8_t *>(b))[i] );
  95. return (diff == 0);
  96. }
  97. /**
  98. * Securely zero memory, avoiding compiler optimizations and such
  99. */
  100. static void burn(void *ptr,unsigned int len);
  101. /**
  102. * @param n Number to convert
  103. * @param s Buffer, at least 24 bytes in size
  104. * @return String containing 'n' in base 10 form
  105. */
  106. static char *decimal(unsigned long n,char s[24]);
  107. static inline char *hex(uint64_t i,char s[17])
  108. {
  109. s[0] = HEXCHARS[(i >> 60) & 0xf];
  110. s[1] = HEXCHARS[(i >> 56) & 0xf];
  111. s[2] = HEXCHARS[(i >> 52) & 0xf];
  112. s[3] = HEXCHARS[(i >> 48) & 0xf];
  113. s[4] = HEXCHARS[(i >> 44) & 0xf];
  114. s[5] = HEXCHARS[(i >> 40) & 0xf];
  115. s[6] = HEXCHARS[(i >> 36) & 0xf];
  116. s[7] = HEXCHARS[(i >> 32) & 0xf];
  117. s[8] = HEXCHARS[(i >> 28) & 0xf];
  118. s[9] = HEXCHARS[(i >> 24) & 0xf];
  119. s[10] = HEXCHARS[(i >> 20) & 0xf];
  120. s[11] = HEXCHARS[(i >> 16) & 0xf];
  121. s[12] = HEXCHARS[(i >> 12) & 0xf];
  122. s[13] = HEXCHARS[(i >> 8) & 0xf];
  123. s[14] = HEXCHARS[(i >> 4) & 0xf];
  124. s[15] = HEXCHARS[i & 0xf];
  125. s[16] = (char)0;
  126. return s;
  127. }
  128. static inline char *hex10(uint64_t i,char s[11])
  129. {
  130. s[0] = HEXCHARS[(i >> 36) & 0xf];
  131. s[1] = HEXCHARS[(i >> 32) & 0xf];
  132. s[2] = HEXCHARS[(i >> 28) & 0xf];
  133. s[3] = HEXCHARS[(i >> 24) & 0xf];
  134. s[4] = HEXCHARS[(i >> 20) & 0xf];
  135. s[5] = HEXCHARS[(i >> 16) & 0xf];
  136. s[6] = HEXCHARS[(i >> 12) & 0xf];
  137. s[7] = HEXCHARS[(i >> 8) & 0xf];
  138. s[8] = HEXCHARS[(i >> 4) & 0xf];
  139. s[9] = HEXCHARS[i & 0xf];
  140. s[10] = (char)0;
  141. return s;
  142. }
  143. static inline char *hex(uint32_t i,char s[9])
  144. {
  145. s[0] = HEXCHARS[(i >> 28) & 0xf];
  146. s[1] = HEXCHARS[(i >> 24) & 0xf];
  147. s[2] = HEXCHARS[(i >> 20) & 0xf];
  148. s[3] = HEXCHARS[(i >> 16) & 0xf];
  149. s[4] = HEXCHARS[(i >> 12) & 0xf];
  150. s[5] = HEXCHARS[(i >> 8) & 0xf];
  151. s[6] = HEXCHARS[(i >> 4) & 0xf];
  152. s[7] = HEXCHARS[i & 0xf];
  153. s[8] = (char)0;
  154. return s;
  155. }
  156. static inline char *hex(uint16_t i,char s[5])
  157. {
  158. s[0] = HEXCHARS[(i >> 12) & 0xf];
  159. s[1] = HEXCHARS[(i >> 8) & 0xf];
  160. s[2] = HEXCHARS[(i >> 4) & 0xf];
  161. s[3] = HEXCHARS[i & 0xf];
  162. s[4] = (char)0;
  163. return s;
  164. }
  165. static inline char *hex(uint8_t i,char s[3])
  166. {
  167. s[0] = HEXCHARS[(i >> 4) & 0xf];
  168. s[1] = HEXCHARS[i & 0xf];
  169. s[2] = (char)0;
  170. return s;
  171. }
  172. static inline char *hex(const void *d,unsigned int l,char *s)
  173. {
  174. char *const save = s;
  175. for(unsigned int i=0;i<l;++i) {
  176. const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
  177. *(s++) = HEXCHARS[b >> 4];
  178. *(s++) = HEXCHARS[b & 0xf];
  179. }
  180. *s = (char)0;
  181. return save;
  182. }
  183. static inline unsigned int unhex(const char *h,void *buf,unsigned int buflen)
  184. {
  185. unsigned int l = 0;
  186. while (l < buflen) {
  187. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  188. if (!hc) break;
  189. uint8_t c = 0;
  190. if ((hc >= 48)&&(hc <= 57)) // 0..9
  191. c = hc - 48;
  192. else if ((hc >= 97)&&(hc <= 102)) // a..f
  193. c = hc - 87;
  194. else if ((hc >= 65)&&(hc <= 70)) // A..F
  195. c = hc - 55;
  196. hc = *(reinterpret_cast<const uint8_t *>(h++));
  197. if (!hc) break;
  198. c <<= 4;
  199. if ((hc >= 48)&&(hc <= 57))
  200. c |= hc - 48;
  201. else if ((hc >= 97)&&(hc <= 102))
  202. c |= hc - 87;
  203. else if ((hc >= 65)&&(hc <= 70))
  204. c |= hc - 55;
  205. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  206. }
  207. return l;
  208. }
  209. static inline unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen)
  210. {
  211. unsigned int l = 0;
  212. const char *hend = h + hlen;
  213. while (l < buflen) {
  214. if (h == hend) break;
  215. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  216. if (!hc) break;
  217. uint8_t c = 0;
  218. if ((hc >= 48)&&(hc <= 57))
  219. c = hc - 48;
  220. else if ((hc >= 97)&&(hc <= 102))
  221. c = hc - 87;
  222. else if ((hc >= 65)&&(hc <= 70))
  223. c = hc - 55;
  224. if (h == hend) break;
  225. hc = *(reinterpret_cast<const uint8_t *>(h++));
  226. if (!hc) break;
  227. c <<= 4;
  228. if ((hc >= 48)&&(hc <= 57))
  229. c |= hc - 48;
  230. else if ((hc >= 97)&&(hc <= 102))
  231. c |= hc - 87;
  232. else if ((hc >= 65)&&(hc <= 70))
  233. c |= hc - 55;
  234. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  235. }
  236. return l;
  237. }
  238. /**
  239. * Generate secure random bytes
  240. *
  241. * This will try to use whatever OS sources of entropy are available. It's
  242. * guarded by an internal mutex so it's thread-safe.
  243. *
  244. * @param buf Buffer to fill
  245. * @param bytes Number of random bytes to generate
  246. */
  247. static void getSecureRandom(void *buf,unsigned int bytes);
  248. /**
  249. * Tokenize a string (alias for strtok_r or strtok_s depending on platform)
  250. *
  251. * @param str String to split
  252. * @param delim Delimiters
  253. * @param saveptr Pointer to a char * for temporary reentrant storage
  254. */
  255. static inline char *stok(char *str,const char *delim,char **saveptr)
  256. {
  257. #ifdef __WINDOWS__
  258. return strtok_s(str,delim,saveptr);
  259. #else
  260. return strtok_r(str,delim,saveptr);
  261. #endif
  262. }
  263. static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
  264. static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
  265. static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
  266. static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
  267. static inline unsigned long long strToU64(const char *s)
  268. {
  269. #ifdef __WINDOWS__
  270. return (unsigned long long)_strtoui64(s,(char **)0,10);
  271. #else
  272. return strtoull(s,(char **)0,10);
  273. #endif
  274. }
  275. static inline long long strTo64(const char *s)
  276. {
  277. #ifdef __WINDOWS__
  278. return (long long)_strtoi64(s,(char **)0,10);
  279. #else
  280. return strtoll(s,(char **)0,10);
  281. #endif
  282. }
  283. static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
  284. static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
  285. static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
  286. static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
  287. static inline unsigned long long hexStrToU64(const char *s)
  288. {
  289. #ifdef __WINDOWS__
  290. return (unsigned long long)_strtoui64(s,(char **)0,16);
  291. #else
  292. return strtoull(s,(char **)0,16);
  293. #endif
  294. }
  295. static inline long long hexStrTo64(const char *s)
  296. {
  297. #ifdef __WINDOWS__
  298. return (long long)_strtoi64(s,(char **)0,16);
  299. #else
  300. return strtoll(s,(char **)0,16);
  301. #endif
  302. }
  303. /**
  304. * Perform a safe C string copy, ALWAYS null-terminating the result
  305. *
  306. * This will never ever EVER result in dest[] not being null-terminated
  307. * regardless of any input parameter (other than len==0 which is invalid).
  308. *
  309. * @param dest Destination buffer (must not be NULL)
  310. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  311. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  312. * @return True on success, false on overflow (buffer will still be 0-terminated)
  313. */
  314. static inline bool scopy(char *dest,unsigned int len,const char *src)
  315. {
  316. if (!len)
  317. return false; // sanity check
  318. if (!src) {
  319. *dest = (char)0;
  320. return true;
  321. }
  322. char *end = dest + len;
  323. while ((*dest++ = *src++)) {
  324. if (dest == end) {
  325. *(--dest) = (char)0;
  326. return false;
  327. }
  328. }
  329. return true;
  330. }
  331. /**
  332. * Count the number of bits set in an integer
  333. *
  334. * @param v 32-bit integer
  335. * @return Number of bits set in this integer (0-32)
  336. */
  337. static inline uint32_t countBits(uint32_t v)
  338. {
  339. v = v - ((v >> 1) & (uint32_t)0x55555555);
  340. v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333);
  341. return ((((v + (v >> 4)) & (uint32_t)0xF0F0F0F) * (uint32_t)0x1010101) >> 24);
  342. }
  343. /**
  344. * Count the number of bits set in an integer
  345. *
  346. * @param v 64-bit integer
  347. * @return Number of bits set in this integer (0-64)
  348. */
  349. static inline uint64_t countBits(uint64_t v)
  350. {
  351. v = v - ((v >> 1) & (uint64_t)~(uint64_t)0/3);
  352. v = (v & (uint64_t)~(uint64_t)0/15*3) + ((v >> 2) & (uint64_t)~(uint64_t)0/15*3);
  353. v = (v + (v >> 4)) & (uint64_t)~(uint64_t)0/255*15;
  354. return (uint64_t)(v * ((uint64_t)~(uint64_t)0/255)) >> 56;
  355. }
  356. /**
  357. * Check if a memory buffer is all-zero
  358. *
  359. * @param p Memory to scan
  360. * @param len Length of memory
  361. * @return True if memory is all zero
  362. */
  363. static inline bool isZero(const void *p,unsigned int len)
  364. {
  365. for(unsigned int i=0;i<len;++i) {
  366. if (((const unsigned char *)p)[i])
  367. return false;
  368. }
  369. return true;
  370. }
  371. // Byte swappers for big/little endian conversion
  372. static inline uint8_t hton(uint8_t n) { return n; }
  373. static inline int8_t hton(int8_t n) { return n; }
  374. static inline uint16_t hton(uint16_t n) { return htons(n); }
  375. static inline int16_t hton(int16_t n) { return (int16_t)htons((uint16_t)n); }
  376. static inline uint32_t hton(uint32_t n) { return htonl(n); }
  377. static inline int32_t hton(int32_t n) { return (int32_t)htonl((uint32_t)n); }
  378. static inline uint64_t hton(uint64_t n)
  379. {
  380. #if __BYTE_ORDER == __LITTLE_ENDIAN
  381. #if defined(__GNUC__) && (!defined(__OpenBSD__))
  382. return __builtin_bswap64(n);
  383. #else
  384. return (
  385. ((n & 0x00000000000000FFULL) << 56) |
  386. ((n & 0x000000000000FF00ULL) << 40) |
  387. ((n & 0x0000000000FF0000ULL) << 24) |
  388. ((n & 0x00000000FF000000ULL) << 8) |
  389. ((n & 0x000000FF00000000ULL) >> 8) |
  390. ((n & 0x0000FF0000000000ULL) >> 24) |
  391. ((n & 0x00FF000000000000ULL) >> 40) |
  392. ((n & 0xFF00000000000000ULL) >> 56)
  393. );
  394. #endif
  395. #else
  396. return n;
  397. #endif
  398. }
  399. static inline int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); }
  400. static inline uint8_t ntoh(uint8_t n) { return n; }
  401. static inline int8_t ntoh(int8_t n) { return n; }
  402. static inline uint16_t ntoh(uint16_t n) { return ntohs(n); }
  403. static inline int16_t ntoh(int16_t n) { return (int16_t)ntohs((uint16_t)n); }
  404. static inline uint32_t ntoh(uint32_t n) { return ntohl(n); }
  405. static inline int32_t ntoh(int32_t n) { return (int32_t)ntohl((uint32_t)n); }
  406. static inline uint64_t ntoh(uint64_t n)
  407. {
  408. #if __BYTE_ORDER == __LITTLE_ENDIAN
  409. #if defined(__GNUC__) && !defined(__OpenBSD__)
  410. return __builtin_bswap64(n);
  411. #else
  412. return (
  413. ((n & 0x00000000000000FFULL) << 56) |
  414. ((n & 0x000000000000FF00ULL) << 40) |
  415. ((n & 0x0000000000FF0000ULL) << 24) |
  416. ((n & 0x00000000FF000000ULL) << 8) |
  417. ((n & 0x000000FF00000000ULL) >> 8) |
  418. ((n & 0x0000FF0000000000ULL) >> 24) |
  419. ((n & 0x00FF000000000000ULL) >> 40) |
  420. ((n & 0xFF00000000000000ULL) >> 56)
  421. );
  422. #endif
  423. #else
  424. return n;
  425. #endif
  426. }
  427. static inline int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); }
  428. /**
  429. * Hexadecimal characters 0-f
  430. */
  431. static const char HEXCHARS[16];
  432. };
  433. } // namespace ZeroTier
  434. #endif