Dictionary.hpp 11 KB

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