Utils.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 s[17])
  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 s[11])
  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(uint32_t i,char s[9])
  107. {
  108. s[0] = HEXCHARS[(i >> 28) & 0xf];
  109. s[1] = HEXCHARS[(i >> 24) & 0xf];
  110. s[2] = HEXCHARS[(i >> 20) & 0xf];
  111. s[3] = HEXCHARS[(i >> 16) & 0xf];
  112. s[4] = HEXCHARS[(i >> 12) & 0xf];
  113. s[5] = HEXCHARS[(i >> 8) & 0xf];
  114. s[6] = HEXCHARS[(i >> 4) & 0xf];
  115. s[7] = HEXCHARS[i & 0xf];
  116. s[8] = (char)0;
  117. return s;
  118. }
  119. static inline char *hex(uint16_t i,char s[5])
  120. {
  121. s[0] = HEXCHARS[(i >> 12) & 0xf];
  122. s[1] = HEXCHARS[(i >> 8) & 0xf];
  123. s[2] = HEXCHARS[(i >> 4) & 0xf];
  124. s[3] = HEXCHARS[i & 0xf];
  125. s[4] = (char)0;
  126. return s;
  127. }
  128. static inline char *hex(uint8_t i,char s[3])
  129. {
  130. s[0] = HEXCHARS[(i >> 4) & 0xf];
  131. s[1] = HEXCHARS[i & 0xf];
  132. s[2] = (char)0;
  133. return s;
  134. }
  135. static inline char *hex(const void *d,unsigned int l,char *s)
  136. {
  137. char *const save = s;
  138. for(unsigned int i=0;i<l;++i) {
  139. const unsigned int b = reinterpret_cast<const uint8_t *>(d)[i];
  140. *(s++) = HEXCHARS[b >> 4];
  141. *(s++) = HEXCHARS[b & 0xf];
  142. }
  143. *s = (char)0;
  144. return save;
  145. }
  146. static inline unsigned int unhex(const char *h,void *buf,unsigned int buflen)
  147. {
  148. unsigned int l = 0;
  149. while (l < buflen) {
  150. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  151. if (!hc) break;
  152. uint8_t c = 0;
  153. if ((hc >= 48)&&(hc <= 57)) // 0..9
  154. c = hc - 48;
  155. else if ((hc >= 97)&&(hc <= 102)) // a..f
  156. c = hc - 87;
  157. else if ((hc >= 65)&&(hc <= 70)) // A..F
  158. c = hc - 55;
  159. hc = *(reinterpret_cast<const uint8_t *>(h++));
  160. if (!hc) break;
  161. c <<= 4;
  162. if ((hc >= 48)&&(hc <= 57))
  163. c |= hc - 48;
  164. else if ((hc >= 97)&&(hc <= 102))
  165. c |= hc - 87;
  166. else if ((hc >= 65)&&(hc <= 70))
  167. c |= hc - 55;
  168. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  169. }
  170. return l;
  171. }
  172. static inline unsigned int unhex(const char *h,unsigned int hlen,void *buf,unsigned int buflen)
  173. {
  174. unsigned int l = 0;
  175. const char *hend = h + hlen;
  176. while (l < buflen) {
  177. if (h == hend) break;
  178. uint8_t hc = *(reinterpret_cast<const uint8_t *>(h++));
  179. if (!hc) break;
  180. uint8_t c = 0;
  181. if ((hc >= 48)&&(hc <= 57))
  182. c = hc - 48;
  183. else if ((hc >= 97)&&(hc <= 102))
  184. c = hc - 87;
  185. else if ((hc >= 65)&&(hc <= 70))
  186. c = hc - 55;
  187. if (h == hend) break;
  188. hc = *(reinterpret_cast<const uint8_t *>(h++));
  189. if (!hc) break;
  190. c <<= 4;
  191. if ((hc >= 48)&&(hc <= 57))
  192. c |= hc - 48;
  193. else if ((hc >= 97)&&(hc <= 102))
  194. c |= hc - 87;
  195. else if ((hc >= 65)&&(hc <= 70))
  196. c |= hc - 55;
  197. reinterpret_cast<uint8_t *>(buf)[l++] = c;
  198. }
  199. return l;
  200. }
  201. /**
  202. * Generate secure random bytes
  203. *
  204. * This will try to use whatever OS sources of entropy are available. It's
  205. * guarded by an internal mutex so it's thread-safe.
  206. *
  207. * @param buf Buffer to fill
  208. * @param bytes Number of random bytes to generate
  209. */
  210. static void getSecureRandom(void *buf,unsigned int bytes);
  211. /**
  212. * Tokenize a string (alias for strtok_r or strtok_s depending on platform)
  213. *
  214. * @param str String to split
  215. * @param delim Delimiters
  216. * @param saveptr Pointer to a char * for temporary reentrant storage
  217. */
  218. static inline char *stok(char *str,const char *delim,char **saveptr)
  219. {
  220. #ifdef __WINDOWS__
  221. return strtok_s(str,delim,saveptr);
  222. #else
  223. return strtok_r(str,delim,saveptr);
  224. #endif
  225. }
  226. static inline unsigned int strToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,10); }
  227. static inline int strToInt(const char *s) { return (int)strtol(s,(char **)0,10); }
  228. static inline unsigned long strToULong(const char *s) { return strtoul(s,(char **)0,10); }
  229. static inline long strToLong(const char *s) { return strtol(s,(char **)0,10); }
  230. static inline unsigned long long strToU64(const char *s)
  231. {
  232. #ifdef __WINDOWS__
  233. return (unsigned long long)_strtoui64(s,(char **)0,10);
  234. #else
  235. return strtoull(s,(char **)0,10);
  236. #endif
  237. }
  238. static inline long long strTo64(const char *s)
  239. {
  240. #ifdef __WINDOWS__
  241. return (long long)_strtoi64(s,(char **)0,10);
  242. #else
  243. return strtoll(s,(char **)0,10);
  244. #endif
  245. }
  246. static inline unsigned int hexStrToUInt(const char *s) { return (unsigned int)strtoul(s,(char **)0,16); }
  247. static inline int hexStrToInt(const char *s) { return (int)strtol(s,(char **)0,16); }
  248. static inline unsigned long hexStrToULong(const char *s) { return strtoul(s,(char **)0,16); }
  249. static inline long hexStrToLong(const char *s) { return strtol(s,(char **)0,16); }
  250. static inline unsigned long long hexStrToU64(const char *s)
  251. {
  252. #ifdef __WINDOWS__
  253. return (unsigned long long)_strtoui64(s,(char **)0,16);
  254. #else
  255. return strtoull(s,(char **)0,16);
  256. #endif
  257. }
  258. static inline long long hexStrTo64(const char *s)
  259. {
  260. #ifdef __WINDOWS__
  261. return (long long)_strtoi64(s,(char **)0,16);
  262. #else
  263. return strtoll(s,(char **)0,16);
  264. #endif
  265. }
  266. /**
  267. * Perform a safe C string copy, ALWAYS null-terminating the result
  268. *
  269. * This will never ever EVER result in dest[] not being null-terminated
  270. * regardless of any input parameter (other than len==0 which is invalid).
  271. *
  272. * @param dest Destination buffer (must not be NULL)
  273. * @param len Length of dest[] (if zero, false is returned and nothing happens)
  274. * @param src Source string (if NULL, dest will receive a zero-length string and true is returned)
  275. * @return True on success, false on overflow (buffer will still be 0-terminated)
  276. */
  277. static inline bool scopy(char *dest,unsigned int len,const char *src)
  278. {
  279. if (!len)
  280. return false; // sanity check
  281. if (!src) {
  282. *dest = (char)0;
  283. return true;
  284. }
  285. char *end = dest + len;
  286. while ((*dest++ = *src++)) {
  287. if (dest == end) {
  288. *(--dest) = (char)0;
  289. return false;
  290. }
  291. }
  292. return true;
  293. }
  294. /**
  295. * Count the number of bits set in an integer
  296. *
  297. * @param v 32-bit integer
  298. * @return Number of bits set in this integer (0-32)
  299. */
  300. static inline uint32_t countBits(uint32_t v)
  301. {
  302. v = v - ((v >> 1) & (uint32_t)0x55555555);
  303. v = (v & (uint32_t)0x33333333) + ((v >> 2) & (uint32_t)0x33333333);
  304. return ((((v + (v >> 4)) & (uint32_t)0xF0F0F0F) * (uint32_t)0x1010101) >> 24);
  305. }
  306. /**
  307. * Count the number of bits set in an integer
  308. *
  309. * @param v 64-bit integer
  310. * @return Number of bits set in this integer (0-64)
  311. */
  312. static inline uint64_t countBits(uint64_t v)
  313. {
  314. v = v - ((v >> 1) & (uint64_t)~(uint64_t)0/3);
  315. v = (v & (uint64_t)~(uint64_t)0/15*3) + ((v >> 2) & (uint64_t)~(uint64_t)0/15*3);
  316. v = (v + (v >> 4)) & (uint64_t)~(uint64_t)0/255*15;
  317. return (uint64_t)(v * ((uint64_t)~(uint64_t)0/255)) >> 56;
  318. }
  319. /**
  320. * Check if a memory buffer is all-zero
  321. *
  322. * @param p Memory to scan
  323. * @param len Length of memory
  324. * @return True if memory is all zero
  325. */
  326. static inline bool isZero(const void *p,unsigned int len)
  327. {
  328. for(unsigned int i=0;i<len;++i) {
  329. if (((const unsigned char *)p)[i])
  330. return false;
  331. }
  332. return true;
  333. }
  334. // Byte swappers for big/little endian conversion
  335. static inline uint8_t hton(uint8_t n) { return n; }
  336. static inline int8_t hton(int8_t n) { return n; }
  337. static inline uint16_t hton(uint16_t n) { return htons(n); }
  338. static inline int16_t hton(int16_t n) { return (int16_t)htons((uint16_t)n); }
  339. static inline uint32_t hton(uint32_t n) { return htonl(n); }
  340. static inline int32_t hton(int32_t n) { return (int32_t)htonl((uint32_t)n); }
  341. static inline uint64_t hton(uint64_t n)
  342. {
  343. #if __BYTE_ORDER == __LITTLE_ENDIAN
  344. #if defined(__GNUC__) && (!defined(__OpenBSD__))
  345. return __builtin_bswap64(n);
  346. #else
  347. return (
  348. ((n & 0x00000000000000FFULL) << 56) |
  349. ((n & 0x000000000000FF00ULL) << 40) |
  350. ((n & 0x0000000000FF0000ULL) << 24) |
  351. ((n & 0x00000000FF000000ULL) << 8) |
  352. ((n & 0x000000FF00000000ULL) >> 8) |
  353. ((n & 0x0000FF0000000000ULL) >> 24) |
  354. ((n & 0x00FF000000000000ULL) >> 40) |
  355. ((n & 0xFF00000000000000ULL) >> 56)
  356. );
  357. #endif
  358. #else
  359. return n;
  360. #endif
  361. }
  362. static inline int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); }
  363. static inline uint8_t ntoh(uint8_t n) { return n; }
  364. static inline int8_t ntoh(int8_t n) { return n; }
  365. static inline uint16_t ntoh(uint16_t n) { return ntohs(n); }
  366. static inline int16_t ntoh(int16_t n) { return (int16_t)ntohs((uint16_t)n); }
  367. static inline uint32_t ntoh(uint32_t n) { return ntohl(n); }
  368. static inline int32_t ntoh(int32_t n) { return (int32_t)ntohl((uint32_t)n); }
  369. static inline uint64_t ntoh(uint64_t n)
  370. {
  371. #if __BYTE_ORDER == __LITTLE_ENDIAN
  372. #if defined(__GNUC__) && !defined(__OpenBSD__)
  373. return __builtin_bswap64(n);
  374. #else
  375. return (
  376. ((n & 0x00000000000000FFULL) << 56) |
  377. ((n & 0x000000000000FF00ULL) << 40) |
  378. ((n & 0x0000000000FF0000ULL) << 24) |
  379. ((n & 0x00000000FF000000ULL) << 8) |
  380. ((n & 0x000000FF00000000ULL) >> 8) |
  381. ((n & 0x0000FF0000000000ULL) >> 24) |
  382. ((n & 0x00FF000000000000ULL) >> 40) |
  383. ((n & 0xFF00000000000000ULL) >> 56)
  384. );
  385. #endif
  386. #else
  387. return n;
  388. #endif
  389. }
  390. static inline int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); }
  391. /**
  392. * Hexadecimal characters 0-f
  393. */
  394. static const char HEXCHARS[16];
  395. };
  396. } // namespace ZeroTier
  397. #endif