Utils.hpp 12 KB

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