Dictionary.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_DICTIONARY_HPP
  19. #define ZT_DICTIONARY_HPP
  20. #include "Constants.hpp"
  21. #include "Utils.hpp"
  22. #include "Buffer.hpp"
  23. #include "Address.hpp"
  24. #include <stdint.h>
  25. // Can be increased if it's ever needed, but not too much.
  26. #define ZT_DICTIONARY_MAX_SIZE 8194
  27. namespace ZeroTier {
  28. /**
  29. * A small key=value store
  30. *
  31. * This stores data in the form of a blob of max size ZT_DICTIONARY_MAX_SIZE.
  32. * It's *technically* human-readable to be backward compatible with old format
  33. * netconfs, but it can store binary data and doing this will negatively impact
  34. * its human-readability.
  35. *
  36. * In any case nulls are always escaped, making the serialized form of this
  37. * object a valid null-terminated C-string. Appending it to a buffer appends
  38. * it as such.
  39. *
  40. * Keys cannot contain binary data, CR/LF, nulls, or the equals (=) sign.
  41. * Adding such a key will result in an invalid entry (but isn't dangerous).
  42. *
  43. * There is code to test and fuzz this in selftest.cpp. Fuzzing a blob of
  44. * pointer tricks like this is important after any modifications.
  45. */
  46. class Dictionary
  47. {
  48. public:
  49. Dictionary()
  50. {
  51. _d[0] = (char)0;
  52. }
  53. Dictionary(const char *s)
  54. {
  55. Utils::scopy(_d,sizeof(_d),s);
  56. }
  57. Dictionary(const char *s,unsigned int len)
  58. {
  59. memcpy(_d,s,(len > ZT_DICTIONARY_MAX_SIZE) ? (unsigned int)ZT_DICTIONARY_MAX_SIZE : len);
  60. _d[ZT_DICTIONARY_MAX_SIZE-1] = (char)0;
  61. }
  62. Dictionary(const Dictionary &d)
  63. {
  64. Utils::scopy(_d,sizeof(_d),d._d);
  65. }
  66. inline Dictionary &operator=(const Dictionary &d)
  67. {
  68. Utils::scopy(_d,sizeof(_d),d._d);
  69. return *this;
  70. }
  71. /**
  72. * Load a dictionary from a C-string
  73. *
  74. * @param s Dictionary in string form
  75. * @return False if 's' was longer than ZT_DICTIONARY_MAX_SIZE
  76. */
  77. inline bool load(const char *s)
  78. {
  79. return Utils::scopy(_d,sizeof(_d),s);
  80. }
  81. /**
  82. * Delete all entries
  83. */
  84. inline void clear()
  85. {
  86. _d[0] = (char)0;
  87. }
  88. /**
  89. * @return Size of dictionary in bytes not including terminating NULL
  90. */
  91. inline unsigned int sizeBytes() const
  92. {
  93. for(unsigned int i=0;i<ZT_DICTIONARY_MAX_SIZE;++i) {
  94. if (!_d[i])
  95. return i;
  96. }
  97. return ZT_DICTIONARY_MAX_SIZE;
  98. }
  99. /**
  100. * Get an entry
  101. *
  102. * Note that to get binary values, dest[] should be at least one more than
  103. * the maximum size of the value being retrieved. That's because even if
  104. * the data is binary a terminating 0 is appended to dest[].
  105. *
  106. * If the key is not found, dest[0] is set to 0 to make dest[] an empty
  107. * C string in that case. The dest[] array will *never* be unterminated.
  108. *
  109. * @param key Key to look up
  110. * @param dest Destination buffer
  111. * @param destlen Size of destination buffer
  112. * @return -1 if not found, or actual number of bytes stored in dest[] minus trailing 0
  113. */
  114. inline int get(const char *key,char *dest,unsigned int destlen) const
  115. {
  116. const char *p = _d;
  117. const char *k;
  118. bool esc;
  119. int j;
  120. if (!destlen) // sanity check
  121. return -1;
  122. while (*p) {
  123. k = key;
  124. while (*k) {
  125. if (*p != *k)
  126. break;
  127. ++k;
  128. ++p;
  129. }
  130. if ((!*k)&&(*p == '=')) {
  131. j = 0;
  132. esc = false;
  133. ++p;
  134. while ((*p)&&(*p != '\r')&&(*p != '\n')) {
  135. if (esc) {
  136. esc = false;
  137. switch(*p) {
  138. case 'r': dest[j++] = '\r'; break;
  139. case 'n': dest[j++] = '\n'; break;
  140. case '0': dest[j++] = (char)0; break;
  141. case 'e': dest[j++] = '='; break;
  142. default: dest[j++] = *p; break;
  143. }
  144. if (j == (int)destlen) {
  145. dest[j-1] = (char)0;
  146. return j-1;
  147. }
  148. } else if (*p == '\\') {
  149. esc = true;
  150. } else {
  151. dest[j++] = *p;
  152. if (j == (int)destlen) {
  153. dest[j-1] = (char)0;
  154. return j-1;
  155. }
  156. }
  157. ++p;
  158. }
  159. dest[j] = (char)0;
  160. return j;
  161. } else {
  162. while ((*p)&&(*p != '\r')&&(*p != '\n'))
  163. ++p;
  164. if (*p)
  165. ++p;
  166. else break;
  167. }
  168. }
  169. dest[0] = (char)0;
  170. return -1;
  171. }
  172. /**
  173. * Get the contents of a key into a buffer
  174. *
  175. * @param key Key to get
  176. * @param dest Destination buffer
  177. * @return True if key was found (if false, dest will be empty)
  178. */
  179. template<unsigned int C>
  180. inline bool get(const char *key,Buffer<C> &dest) const
  181. {
  182. const int r = this->get(key,const_cast<char *>(reinterpret_cast<const char *>(dest.data())),C);
  183. if (r >= 0) {
  184. dest.setSize((unsigned int)r);
  185. return true;
  186. } else {
  187. dest.clear();
  188. return false;
  189. }
  190. }
  191. /**
  192. * Get a boolean value
  193. *
  194. * @param key Key to look up
  195. * @param dfl Default value if not found in dictionary
  196. * @return Boolean value of key or 'dfl' if not found
  197. */
  198. bool getB(const char *key,bool dfl = false) const
  199. {
  200. char tmp[4];
  201. if (this->get(key,tmp,sizeof(tmp)) >= 0)
  202. return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T'));
  203. return dfl;
  204. }
  205. /**
  206. * Get an unsigned int64 stored as hex in the dictionary
  207. *
  208. * @param key Key to look up
  209. * @param dfl Default value or 0 if unspecified
  210. * @return Decoded hex UInt value or 'dfl' if not found
  211. */
  212. inline uint64_t getUI(const char *key,uint64_t dfl = 0) const
  213. {
  214. char tmp[128];
  215. if (this->get(key,tmp,sizeof(tmp)) >= 1)
  216. return Utils::hexStrToU64(tmp);
  217. return dfl;
  218. }
  219. /**
  220. * Add a new key=value pair
  221. *
  222. * If the key is already present this will append another, but the first
  223. * will always be returned by get(). This is not checked. If you want to
  224. * ensure a key is not present use erase() first.
  225. *
  226. * Use the vlen parameter to add binary values. Nulls will be escaped.
  227. *
  228. * @param key Key -- nulls, CR/LF, and equals (=) are illegal characters
  229. * @param value Value to set
  230. * @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0
  231. * @return True if there was enough room to add this key=value pair
  232. */
  233. inline bool add(const char *key,const char *value,int vlen = -1)
  234. {
  235. for(unsigned int i=0;i<ZT_DICTIONARY_MAX_SIZE;++i) {
  236. if (!_d[i]) {
  237. unsigned int j = i;
  238. if (j > 0) {
  239. _d[j++] = '\n';
  240. if (j == ZT_DICTIONARY_MAX_SIZE) {
  241. _d[i] = (char)0;
  242. return false;
  243. }
  244. }
  245. const char *p = key;
  246. while (*p) {
  247. _d[j++] = *(p++);
  248. if (j == ZT_DICTIONARY_MAX_SIZE) {
  249. _d[i] = (char)0;
  250. return false;
  251. }
  252. }
  253. _d[j++] = '=';
  254. if (j == ZT_DICTIONARY_MAX_SIZE) {
  255. _d[i] = (char)0;
  256. return false;
  257. }
  258. p = value;
  259. int k = 0;
  260. while ( ((*p)&&(vlen < 0)) || (k < vlen) ) {
  261. switch(*p) {
  262. case 0:
  263. case '\r':
  264. case '\n':
  265. case '\\':
  266. case '=':
  267. _d[j++] = '\\';
  268. if (j == ZT_DICTIONARY_MAX_SIZE) {
  269. _d[i] = (char)0;
  270. return false;
  271. }
  272. switch(*p) {
  273. case 0: _d[j++] = '0'; break;
  274. case '\r': _d[j++] = 'r'; break;
  275. case '\n': _d[j++] = 'n'; break;
  276. case '\\': _d[j++] = '\\'; break;
  277. case '=': _d[j++] = 'e'; break;
  278. }
  279. if (j == ZT_DICTIONARY_MAX_SIZE) {
  280. _d[i] = (char)0;
  281. return false;
  282. }
  283. break;
  284. default:
  285. _d[j++] = *p;
  286. if (j == ZT_DICTIONARY_MAX_SIZE) {
  287. _d[i] = (char)0;
  288. return false;
  289. }
  290. break;
  291. }
  292. ++p;
  293. ++k;
  294. }
  295. _d[j] = (char)0;
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. /**
  302. * Add a boolean as a '1' or a '0'
  303. */
  304. inline bool add(const char *key,bool value)
  305. {
  306. return this->add(key,(value) ? "1" : "0",1);
  307. }
  308. /**
  309. * Add a 64-bit integer (unsigned) as a hex value
  310. */
  311. inline bool add(const char *key,uint64_t value)
  312. {
  313. char tmp[32];
  314. Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value);
  315. return this->add(key,tmp,-1);
  316. }
  317. /**
  318. * Add a 64-bit integer (unsigned) as a hex value
  319. */
  320. inline bool add(const char *key,const Address &a)
  321. {
  322. char tmp[32];
  323. Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt());
  324. return this->add(key,tmp,-1);
  325. }
  326. /**
  327. * Add a binary buffer
  328. */
  329. template<unsigned int C>
  330. inline bool add(const char *key,const Buffer<C> &value)
  331. {
  332. return this->add(key,(const char *)value.data(),(int)value.size());
  333. }
  334. /**
  335. * @param key Key to check
  336. * @return True if key is present
  337. */
  338. inline bool contains(const char *key) const
  339. {
  340. char tmp[2];
  341. return (this->get(key,tmp,2) >= 0);
  342. }
  343. /**
  344. * Erase a key from this dictionary
  345. *
  346. * Use this before add() to ensure that a key is replaced if it might
  347. * already be present.
  348. *
  349. * @param key Key to erase
  350. * @return True if key was found and erased
  351. */
  352. inline bool erase(const char *key)
  353. {
  354. char d2[ZT_DICTIONARY_MAX_SIZE];
  355. char *saveptr = (char *)0;
  356. unsigned int d2ptr = 0;
  357. bool found = false;
  358. for(char *f=Utils::stok(_d,"\r\n",&saveptr);(f);f=Utils::stok((char *)0,"\r\n",&saveptr)) {
  359. if (*f) {
  360. const char *p = f;
  361. const char *k = key;
  362. while ((*k)&&(*p)) {
  363. if (*k != *p)
  364. break;
  365. ++k;
  366. ++p;
  367. }
  368. if (*k) {
  369. p = f;
  370. while (*p)
  371. d2[d2ptr++] = *(p++);
  372. d2[d2ptr++] = '\n';
  373. } else {
  374. found = true;
  375. }
  376. }
  377. }
  378. d2[d2ptr++] = (char)0;
  379. memcpy(_d,d2,d2ptr);
  380. return found;
  381. }
  382. /**
  383. * @return Dictionary data as a 0-terminated C-string
  384. */
  385. inline const char *data() const { return _d; }
  386. private:
  387. char _d[ZT_DICTIONARY_MAX_SIZE];
  388. };
  389. } // namespace ZeroTier
  390. #endif