Dictionary.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_DICTIONARY_HPP
  14. #define ZT_DICTIONARY_HPP
  15. #include "Constants.hpp"
  16. #include "Utils.hpp"
  17. #include "Address.hpp"
  18. #include "Buf.hpp"
  19. #include "Containers.hpp"
  20. namespace ZeroTier {
  21. class Identity;
  22. /**
  23. * A simple key-value store for short keys
  24. *
  25. * This data structure is used for network configurations, node meta-data,
  26. * and other open-definition protocol objects.
  27. *
  28. * If this seems a little odd, it is. It dates back to the very first alpha
  29. * versions of ZeroTier and if it were redesigned today we'd use some kind
  30. * of simple or standardized binary encoding. Nevertheless it is efficient
  31. * and it works so there is no need to change it and break backward
  32. * compatibility.
  33. *
  34. * Use of the append functions is faster than building and then encoding a
  35. * dictionary for creating outbound packets.
  36. */
  37. class Dictionary
  38. {
  39. public:
  40. typedef SortedMap< String, Vector < uint8_t > >::const_iterator const_iterator;
  41. Dictionary();
  42. ~Dictionary();
  43. /**
  44. * Get a reference to a value
  45. *
  46. * @param k Key to look up
  47. * @return Reference to value
  48. */
  49. Vector <uint8_t> &operator[](const char *k);
  50. /**
  51. * Get a const reference to a value
  52. *
  53. * @param k Key to look up
  54. * @return Reference to value or to empty vector if not found
  55. */
  56. const Vector <uint8_t> &operator[](const char *k) const;
  57. /**
  58. * @return Start of key->value pairs
  59. */
  60. ZT_INLINE const_iterator begin() const noexcept
  61. { return m_entries.begin(); }
  62. /**
  63. * @return End of key->value pairs
  64. */
  65. ZT_INLINE const_iterator end() const noexcept
  66. { return m_entries.end(); }
  67. /**
  68. * Add a boolean as '1' or '0'
  69. */
  70. void add(const char *k, bool v);
  71. /**
  72. * Add an integer as a hexadecimal string value
  73. *
  74. * @param k Key to set
  75. * @param v Integer to set, will be cast to uint64_t and stored as hex
  76. */
  77. ZT_INLINE void add(const char *const k, const uint64_t v)
  78. { char buf[17]; add(k, Utils::hex((uint64_t)(v), buf)); }
  79. /**
  80. * Add an integer as a hexadecimal string value
  81. *
  82. * @param k Key to set
  83. * @param v Integer to set, will be cast to uint64_t and stored as hex
  84. */
  85. ZT_INLINE void add(const char *const k, const int64_t v)
  86. { char buf[17]; add(k, Utils::hex((uint64_t)(v), buf)); }
  87. /**
  88. * Add an address in 10-digit hex string format
  89. */
  90. void add(const char *k, const Address &v);
  91. /**
  92. * Add a C string as a value
  93. */
  94. void add(const char *k, const char *v);
  95. /**
  96. * Add a binary blob as a value
  97. */
  98. void add(const char *k, const void *data, unsigned int len);
  99. /**
  100. * Get a boolean
  101. *
  102. * @param k Key to look up
  103. * @param dfl Default value (default: false)
  104. * @return Value of key or default if not found
  105. */
  106. bool getB(const char *k, bool dfl = false) const;
  107. /**
  108. * Get an integer
  109. *
  110. * @param k Key to look up
  111. * @param dfl Default value (default: 0)
  112. * @return Value of key or default if not found
  113. */
  114. uint64_t getUI(const char *k, uint64_t dfl = 0) const;
  115. /**
  116. * Get a C string
  117. *
  118. * If the buffer is too small the string will be truncated, but the
  119. * buffer will always end in a terminating null no matter what.
  120. *
  121. * @param k Key to look up
  122. * @param v Buffer to hold string
  123. * @param cap Maximum size of string (including terminating null)
  124. */
  125. char *getS(const char *k, char *v, unsigned int cap) const;
  126. /**
  127. * Get an object supporting the marshal/unmarshal interface pattern
  128. *
  129. * @tparam T Object type (inferred)
  130. * @param k Key to look up
  131. * @param obj Object to unmarshal() into
  132. * @return True if unmarshal was successful
  133. */
  134. template< typename T >
  135. ZT_INLINE bool getO(const char *k, T &obj) const
  136. {
  137. const Vector< uint8_t > &d = (*this)[k];
  138. if (d.empty())
  139. return false;
  140. return (obj.unmarshal(d.data(), (unsigned int)d.size()) > 0);
  141. }
  142. /**
  143. * Add an object supporting the marshal/unmarshal interface pattern
  144. *
  145. * @tparam T Object type (inferred)
  146. * @param k Key to add
  147. * @param obj Object to marshal() into vector
  148. * @return True if successful
  149. */
  150. template< typename T >
  151. ZT_INLINE bool addO(const char *k, T &obj)
  152. {
  153. uint8_t tmp[4096];
  154. static_assert(sizeof(tmp) >= T::marshalSizeMax(),"buffer too small");
  155. int l = obj.marshal(tmp);
  156. if (l > 0) {
  157. (*this)[k].assign(tmp, tmp + l);
  158. return true;
  159. }
  160. return false;
  161. }
  162. /**
  163. * Erase all entries in dictionary
  164. */
  165. void clear();
  166. /**
  167. * @return Number of entries
  168. */
  169. ZT_INLINE unsigned int size() const noexcept
  170. { return m_entries.size(); }
  171. /**
  172. * @return True if dictionary is not empty
  173. */
  174. ZT_INLINE bool empty() const noexcept
  175. { return m_entries.empty(); }
  176. /**
  177. * Encode to a string in the supplied vector
  178. *
  179. * @param out String encoded dictionary
  180. */
  181. void encode(Vector <uint8_t> &out) const;
  182. /**
  183. * Decode a string encoded dictionary
  184. *
  185. * This will decode up to 'len' but will also abort if it finds a
  186. * null/zero as this could be a C string.
  187. *
  188. * @param data Data to decode
  189. * @param len Length of data
  190. * @return True if dictionary was formatted correctly and valid, false on error
  191. */
  192. bool decode(const void *data, unsigned int len);
  193. /**
  194. * Append a key=value pair to a buffer (vector or FCV)
  195. *
  196. * @param out Buffer
  197. * @param k Key (must be <= 8 characters)
  198. * @param v Value
  199. */
  200. template< typename V >
  201. ZT_INLINE static void append(V &out, const char *const k, const bool v)
  202. {
  203. s_appendKey(out, k);
  204. out.push_back((uint8_t)(v ? '1' : '0'));
  205. out.push_back((uint8_t)'\n');
  206. }
  207. /**
  208. * Append a key=value pair to a buffer (vector or FCV)
  209. *
  210. * @param out Buffer
  211. * @param k Key (must be <= 8 characters)
  212. * @param v Value
  213. */
  214. template< typename V >
  215. ZT_INLINE static void append(V &out, const char *const k, const Address v)
  216. {
  217. s_appendKey(out, k);
  218. const uint64_t a = v.toInt();
  219. static_assert(ZT_ADDRESS_LENGTH_HEX == 10, "this must be rewritten for any change in address length");
  220. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 36U) & 0xfU]);
  221. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 32U) & 0xfU]);
  222. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 28U) & 0xfU]);
  223. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 24U) & 0xfU]);
  224. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 20U) & 0xfU]);
  225. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 16U) & 0xfU]);
  226. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 12U) & 0xfU]);
  227. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 8U) & 0xfU]);
  228. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 4U) & 0xfU]);
  229. out.push_back((uint8_t)Utils::HEXCHARS[a & 0xfU]);
  230. out.push_back((uint8_t)'\n');
  231. }
  232. /**
  233. * Append a key=value pair to a buffer (vector or FCV)
  234. *
  235. * @param out Buffer
  236. * @param k Key (must be <= 8 characters)
  237. * @param v Value
  238. */
  239. template< typename V >
  240. ZT_INLINE static void append(V &out, const char *const k, const uint64_t v)
  241. {
  242. char buf[17];
  243. Utils::hex(v, buf);
  244. unsigned int i = 0;
  245. while (buf[i])
  246. out.push_back((uint8_t)buf[i++]);
  247. out.push_back((uint8_t)'\n');
  248. }
  249. template< typename V >
  250. ZT_INLINE static void append(V &out, const char *const k, const int64_t v)
  251. { append(out, k, (uint64_t)v); }
  252. template< typename V >
  253. ZT_INLINE static void append(V &out, const char *const k, const uint32_t v)
  254. { append(out, k, (uint64_t)v); }
  255. template< typename V >
  256. ZT_INLINE static void append(V &out, const char *const k, const int32_t v)
  257. { append(out, k, (uint64_t)v); }
  258. template< typename V >
  259. ZT_INLINE static void append(V &out, const char *const k, const uint16_t v)
  260. { append(out, k, (uint64_t)v); }
  261. template< typename V >
  262. ZT_INLINE static void append(V &out, const char *const k, const int16_t v)
  263. { append(out, k, (uint64_t)v); }
  264. template< typename V >
  265. ZT_INLINE static void append(V &out, const char *const k, const uint8_t v)
  266. { append(out, k, (uint64_t)v); }
  267. template< typename V >
  268. ZT_INLINE static void append(V &out, const char *const k, const int8_t v)
  269. { append(out, k, (uint64_t)v); }
  270. /**
  271. * Append a key=value pair to a buffer (vector or FCV)
  272. *
  273. * @param out Buffer
  274. * @param k Key (must be <= 8 characters)
  275. * @param v Value
  276. */
  277. template< typename V >
  278. ZT_INLINE static void append(V &out, const char *const k, const char *v)
  279. {
  280. if ((v) && (*v)) {
  281. s_appendKey(out, k);
  282. while (*v)
  283. s_appendValueByte(out, (uint8_t)*(v++));
  284. out.push_back((uint8_t)'\n');
  285. }
  286. }
  287. /**
  288. * Append a key=value pair to a buffer (vector or FCV)
  289. *
  290. * @param out Buffer
  291. * @param k Key (must be <= 8 characters)
  292. * @param v Value
  293. * @param vlen Value length in bytes
  294. */
  295. template< typename V >
  296. ZT_INLINE static void append(V &out, const char *const k, const void *const v, const unsigned int vlen)
  297. {
  298. s_appendKey(out, k);
  299. for (unsigned int i = 0; i < vlen; ++i)
  300. s_appendValueByte(out, reinterpret_cast<const uint8_t *>(v)[i]);
  301. out.push_back((uint8_t)'\n');
  302. }
  303. /**
  304. * Append a packet ID as raw bytes in the provided byte order
  305. *
  306. * @param out Buffer
  307. * @param k Key (must be <= 8 characters)
  308. * @param pid Packet ID
  309. */
  310. template< typename V >
  311. static ZT_INLINE void appendPacketId(V &out, const char *const k, const uint64_t pid)
  312. { append(out, k, &pid, 8); }
  313. /**
  314. * Append key=value with any object implementing the correct marshal interface
  315. *
  316. * @param out Buffer
  317. * @param k Key (must be <= 8 characters)
  318. * @param v Marshal-able object
  319. * @return Bytes appended or negative on error (return value of marshal())
  320. */
  321. template< typename V, typename T >
  322. static ZT_INLINE int appendObject(V &out, const char *const k, const T &v)
  323. {
  324. uint8_t tmp[4096]; // large enough for any current object
  325. if (T::marshalSizeMax() > sizeof(tmp))
  326. return -1;
  327. const int mlen = v.marshal(tmp);
  328. if (mlen > 0)
  329. append(out, k, tmp, (unsigned int)mlen);
  330. return mlen;
  331. }
  332. static ZT_INLINE char *arraySubscript(char buf[256],const char *name,const unsigned long sub) noexcept
  333. {
  334. for(unsigned int i=0;i<(256 - 17);++i) {
  335. if ((buf[i] = name[i]) == 0) {
  336. buf[i++] = '#';
  337. Utils::hex(sub, buf + i);
  338. return buf;
  339. }
  340. }
  341. buf[0] = 0;
  342. return buf;
  343. }
  344. private:
  345. template< typename V >
  346. ZT_INLINE static void s_appendValueByte(V &out, const uint8_t c)
  347. {
  348. switch (c) {
  349. case 0:
  350. out.push_back(92); // backslash
  351. out.push_back(48);
  352. break;
  353. case 10:
  354. out.push_back(92);
  355. out.push_back(110);
  356. break;
  357. case 13:
  358. out.push_back(92);
  359. out.push_back(114);
  360. break;
  361. case 61:
  362. out.push_back(92);
  363. out.push_back(101);
  364. break;
  365. case 92:
  366. out.push_back(92);
  367. out.push_back(92);
  368. break;
  369. default:
  370. out.push_back(c);
  371. break;
  372. }
  373. }
  374. template< typename V >
  375. ZT_INLINE static void s_appendKey(V &out, const char *const k)
  376. {
  377. for (unsigned int i = 0; i < 7; ++i) {
  378. const char kc = k[i];
  379. if (kc == 0)
  380. break;
  381. if ((kc >= 33) && (kc <= 126) && (kc != 61) && (kc != 92)) // printable ASCII with no spaces, equals, or backslash
  382. out.push_back((uint8_t)kc);
  383. }
  384. out.push_back((uint8_t)'=');
  385. }
  386. ZT_INLINE static String s_key(const char *k) noexcept
  387. {
  388. String buf;
  389. buf.clear();
  390. for (unsigned int i = 0; i < 7; ++i) {
  391. const char kc = k[i];
  392. if (kc == 0)
  393. break;
  394. if ((kc >= 33) && (kc <= 126) && (kc != 61) && (kc != 92)) // printable ASCII with no spaces, equals, or backslash
  395. buf.push_back(kc);
  396. }
  397. buf.push_back(0);
  398. return buf;
  399. }
  400. SortedMap <String, Vector< uint8_t >> m_entries;
  401. };
  402. } // namespace ZeroTier
  403. #endif