Dictionary.hpp 11 KB

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