Utils.hpp 13 KB

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