Dictionary.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. /**
  22. * A simple key-value store for short keys
  23. *
  24. * This data structure is used for network configurations, node meta-data,
  25. * and other open-definition protocol objects. It consists of a key-value
  26. * store with short (max: 8 characters) keys that map to strings, blobs,
  27. * or integers with the latter being by convention in hex format.
  28. *
  29. * If this seems a little odd, it is. It dates back to the very first alpha
  30. * versions of ZeroTier and if it were redesigned today we'd use some kind
  31. * of simple or standardized binary encoding. Nevertheless it is efficient
  32. * and it works so there is no need to change it and break backward
  33. * compatibility.
  34. *
  35. * Use of the append functions is faster than building and then encoding a
  36. * dictionary for creating outbound packets.
  37. */
  38. class Dictionary
  39. {
  40. public:
  41. Dictionary();
  42. /**
  43. * Get a reference to a value
  44. *
  45. * @param k Key to look up
  46. * @return Reference to value
  47. */
  48. Vector<uint8_t> &operator[](const char *k);
  49. /**
  50. * Get a const reference to a value
  51. *
  52. * @param k Key to look up
  53. * @return Reference to value or to empty vector if not found
  54. */
  55. const Vector<uint8_t> &operator[](const char *k) const;
  56. /**
  57. * Add a boolean as '1' or '0'
  58. */
  59. void add(const char *k,bool v);
  60. /**
  61. * Add an integer as a hexadecimal string value
  62. */
  63. ZT_INLINE void add(const char *const k,const uint64_t v) { char buf[17]; add(k,Utils::hex(v,buf)); }
  64. ZT_INLINE void add(const char *const k,const int64_t v) { char buf[17]; add(k,Utils::hex((uint64_t)v,buf)); }
  65. ZT_INLINE void add(const char *const k,const uint32_t v) { char buf[17]; add(k,Utils::hex((uint64_t)v,buf)); }
  66. ZT_INLINE void add(const char *const k,const int32_t v) { char buf[17]; add(k,Utils::hex((uint64_t)v,buf)); }
  67. ZT_INLINE void add(const char *const k,const uint16_t v) { char buf[17]; add(k,Utils::hex((uint64_t)v,buf)); }
  68. ZT_INLINE void add(const char *const k,const int16_t v) { char buf[17]; add(k,Utils::hex((uint64_t)v,buf)); }
  69. /**
  70. * Add an address in 10-digit hex string format
  71. */
  72. void add(const char *k,const Address &v);
  73. /**
  74. * Add a C string as a value
  75. */
  76. void add(const char *k,const char *v);
  77. /**
  78. * Add a binary blob as a value
  79. */
  80. void add(const char *k,const void *data,unsigned int len);
  81. /**
  82. * Get a boolean
  83. *
  84. * @param k Key to look up
  85. * @param dfl Default value (default: false)
  86. * @return Value of key or default if not found
  87. */
  88. bool getB(const char *k,bool dfl = false) const;
  89. /**
  90. * Get an integer
  91. *
  92. * @param k Key to look up
  93. * @param dfl Default value (default: 0)
  94. * @return Value of key or default if not found
  95. */
  96. uint64_t getUI(const char *k,uint64_t dfl = 0) const;
  97. /**
  98. * Get a C string
  99. *
  100. * If the buffer is too small the string will be truncated, but the
  101. * buffer will always end in a terminating null no matter what.
  102. *
  103. * @param k Key to look up
  104. * @param v Buffer to hold string
  105. * @param cap Maximum size of string (including terminating null)
  106. */
  107. void getS(const char *k,char *v,unsigned int cap) const;
  108. /**
  109. * Get an object supporting the marshal/unmarshal interface pattern
  110. *
  111. * @param k Key to look up
  112. * @param obj Object to unmarshal() into
  113. * @return True if unmarshal was successful
  114. */
  115. template<typename T>
  116. ZT_INLINE bool getO(const char *k,T &obj) const
  117. {
  118. const Vector<uint8_t> &d = (*this)[k];
  119. if (d.empty())
  120. return false;
  121. if (obj.unmarshal(d.data(),(unsigned int)d.size()) <= 0)
  122. return false;
  123. return true;
  124. }
  125. /**
  126. * Erase all entries in dictionary
  127. */
  128. void clear();
  129. /**
  130. * @return Number of entries
  131. */
  132. ZT_INLINE unsigned int size() const noexcept { return m_entries.size(); }
  133. /**
  134. * @return True if dictionary is not empty
  135. */
  136. ZT_INLINE bool empty() const noexcept { return m_entries.empty(); }
  137. /**
  138. * Encode to a string in the supplied vector
  139. *
  140. * This does not add a terminating zero. This must be pushed afterwords
  141. * if the result is to be handled as a C string.
  142. *
  143. * @param out String encoded dictionary
  144. */
  145. void encode(Vector<uint8_t> &out) const;
  146. /**
  147. * Decode a string encoded dictionary
  148. *
  149. * This will decode up to 'len' but will also abort if it finds a
  150. * null/zero as this could be a C string.
  151. *
  152. * @param data Data to decode
  153. * @param len Length of data
  154. * @return True if dictionary was formatted correctly and valid, false on error
  155. */
  156. bool decode(const void *data,unsigned int len);
  157. /**
  158. * Append a key=value pair to a buffer (vector or FCV)
  159. *
  160. * @param out Buffer
  161. * @param k Key (must be <= 8 characters)
  162. * @param v Value
  163. */
  164. template<typename V>
  165. ZT_INLINE static void append(V &out,const char *const k,const bool v)
  166. {
  167. s_appendKey(out,k);
  168. out.push_back((uint8_t)(v ? '1' : '0'));
  169. out.push_back((uint8_t)'\n');
  170. }
  171. /**
  172. * Append a key=value pair to a buffer (vector or FCV)
  173. *
  174. * @param out Buffer
  175. * @param k Key (must be <= 8 characters)
  176. * @param v Value
  177. */
  178. template<typename V>
  179. ZT_INLINE static void append(V &out,const char *const k,const Address v)
  180. {
  181. s_appendKey(out,k);
  182. const uint64_t a = v.toInt();
  183. static_assert(ZT_ADDRESS_LENGTH_HEX == 10,"this must be rewritten for any change in address length");
  184. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 36U) & 0xfU]);
  185. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 32U) & 0xfU]);
  186. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 28U) & 0xfU]);
  187. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 24U) & 0xfU]);
  188. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 20U) & 0xfU]);
  189. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 16U) & 0xfU]);
  190. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 12U) & 0xfU]);
  191. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 8U) & 0xfU]);
  192. out.push_back((uint8_t)Utils::HEXCHARS[(a >> 4U) & 0xfU]);
  193. out.push_back((uint8_t)Utils::HEXCHARS[a & 0xfU]);
  194. out.push_back((uint8_t)'\n');
  195. }
  196. /**
  197. * Append a key=value pair to a buffer (vector or FCV)
  198. *
  199. * @param out Buffer
  200. * @param k Key (must be <= 8 characters)
  201. * @param v Value
  202. */
  203. template<typename V>
  204. ZT_INLINE static void append(V &out,const char *const k,const uint64_t v)
  205. {
  206. char buf[17];
  207. Utils::hex(v,buf);
  208. unsigned int i = 0;
  209. while (buf[i])
  210. out.push_back((uint8_t)buf[i++]);
  211. out.push_back((uint8_t)'\n');
  212. }
  213. template<typename V>
  214. ZT_INLINE static void append(V &out,const char *const k,const int64_t v) { append(out,k,(uint64_t)v); }
  215. template<typename V>
  216. ZT_INLINE static void append(V &out,const char *const k,const uint32_t v) { append(out,k,(uint64_t)v); }
  217. template<typename V>
  218. ZT_INLINE static void append(V &out,const char *const k,const int32_t v) { append(out,k,(uint64_t)v); }
  219. template<typename V>
  220. ZT_INLINE static void append(V &out,const char *const k,const uint16_t v) { append(out,k,(uint64_t)v); }
  221. template<typename V>
  222. ZT_INLINE static void append(V &out,const char *const k,const int16_t v) { append(out,k,(uint64_t)v); }
  223. template<typename V>
  224. ZT_INLINE static void append(V &out,const char *const k,const uint8_t v) { append(out,k,(uint64_t)v); }
  225. template<typename V>
  226. ZT_INLINE static void append(V &out,const char *const k,const int8_t v) { append(out,k,(uint64_t)v); }
  227. /**
  228. * Append a key=value pair to a buffer (vector or FCV)
  229. *
  230. * @param out Buffer
  231. * @param k Key (must be <= 8 characters)
  232. * @param v Value
  233. */
  234. template<typename V>
  235. ZT_INLINE static void append(V &out,const char *const k,const char *v)
  236. {
  237. if ((v)&&(*v)) {
  238. s_appendKey(out,k);
  239. while (*v)
  240. s_appendValueByte(out,(uint8_t)*(v++));
  241. out.push_back((uint8_t)'\n');
  242. }
  243. }
  244. /**
  245. * Append a key=value pair to a buffer (vector or FCV)
  246. *
  247. * @param out Buffer
  248. * @param k Key (must be <= 8 characters)
  249. * @param v Value
  250. * @param vlen Value length in bytes
  251. */
  252. template<typename V>
  253. ZT_INLINE static void append(V &out,const char *const k,const void *const v,const unsigned int vlen)
  254. {
  255. s_appendKey(out,k);
  256. for(unsigned int i=0;i<vlen;++i)
  257. s_appendValueByte(out,reinterpret_cast<const uint8_t *>(v)[i]);
  258. out.push_back((uint8_t)'\n');
  259. }
  260. /**
  261. * Append a packet ID as raw bytes in the provided byte order
  262. *
  263. * @param out Buffer
  264. * @param k Key (must be <= 8 characters)
  265. * @param pid Packet ID
  266. */
  267. template<typename V>
  268. static ZT_INLINE void appendPacketId(V &out,const char *const k,const uint64_t pid)
  269. {
  270. append(out,k,&pid,8);
  271. }
  272. /**
  273. * Append key=value with any object implementing the correct marshal interface
  274. *
  275. * @param out Buffer
  276. * @param k Key (must be <= 8 characters)
  277. * @param v Marshal-able object
  278. * @return Bytes appended or negative on error (return value of marshal())
  279. */
  280. template<typename V,typename T>
  281. static ZT_INLINE int appendObject(V &out,const char *const k,const T &v)
  282. {
  283. uint8_t tmp[4096]; // large enough for any current object
  284. if (T::marshalSizeMax() > sizeof(tmp))
  285. return -1;
  286. const int mlen = v.marshal(tmp);
  287. if (mlen > 0)
  288. append(out,k,tmp,(unsigned int)mlen);
  289. return mlen;
  290. }
  291. private:
  292. template<typename V>
  293. ZT_INLINE static void s_appendValueByte(V &out,const uint8_t c)
  294. {
  295. switch(c) {
  296. case 0:
  297. out.push_back(92); // backslash
  298. out.push_back(48);
  299. break;
  300. case 10:
  301. out.push_back(92);
  302. out.push_back(110);
  303. break;
  304. case 13:
  305. out.push_back(92);
  306. out.push_back(114);
  307. break;
  308. case 61:
  309. out.push_back(92);
  310. out.push_back(101);
  311. break;
  312. case 92:
  313. out.push_back(92);
  314. out.push_back(92);
  315. break;
  316. default:
  317. out.push_back(c);
  318. break;
  319. }
  320. }
  321. template<typename V>
  322. ZT_INLINE static void s_appendKey(V &out,const char *const k)
  323. {
  324. for(unsigned int i=0;i<8;++i) {
  325. const char kc = k[i];
  326. if (!kc) break;
  327. if ((kc >= 33)&&(kc <= 126)&&(kc != 61)&&(kc != 92)) // printable ASCII with no spaces, equals, or backslash
  328. out.push_back((uint8_t)kc);
  329. }
  330. out.push_back((uint8_t)'=');
  331. }
  332. // This just packs up to 8 character bytes into a 64-bit word. There is no need
  333. // for this to be portable in terms of endian-ness. It's just for fast key lookup.
  334. static ZT_INLINE uint64_t s_toKey(const char *k)
  335. {
  336. uint64_t key = 0;
  337. for(int i=0;i<8;++i) {
  338. if ((reinterpret_cast<uint8_t *>(&key)[i] = *(k++)) == 0)
  339. break;
  340. }
  341. return key;
  342. }
  343. Map< uint64_t,Vector<uint8_t> > m_entries;
  344. };
  345. } // namespace ZeroTier
  346. #endif