Dictionary.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 16384
  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,*s;
  118. bool esc;
  119. int j;
  120. for(;;) {
  121. s = p;
  122. for(;;) {
  123. if ((*p == '\r')||(*p == '\n')||(*p == '=')||(!*p)) {
  124. k = key;
  125. while ((*k)&&(s != p)) {
  126. if (*(k++) != *(s++))
  127. break;
  128. }
  129. if (*k) {
  130. esc = false;
  131. for(;;) {
  132. if (!*p) {
  133. dest[0] = (char)0;
  134. return -1;
  135. } else if (esc) {
  136. esc = false;
  137. } else if (*p == '\\') {
  138. esc = true;
  139. } else if ((*p == '\r')||(*p == '\n')) {
  140. ++p;
  141. break;
  142. }
  143. ++p;
  144. }
  145. break;
  146. } else {
  147. if (*p == '=') ++p;
  148. esc = false;
  149. j = 0;
  150. for(;;) {
  151. if (esc) {
  152. esc = false;
  153. if (j >= destlen) {
  154. dest[destlen-1] = (char)0;
  155. return (int)(destlen-1);
  156. }
  157. switch(*p) {
  158. case 'r':
  159. dest[j++] = '\r';
  160. break;
  161. case 'n':
  162. dest[j++] = '\n';
  163. break;
  164. case 't':
  165. dest[j++] = '\t';
  166. break;
  167. case '0':
  168. dest[j++] = (char)0;
  169. break;
  170. case 'e':
  171. dest[j++] = '=';
  172. break;
  173. default:
  174. dest[j++] = *p;
  175. }
  176. } else if (*p == '\\') {
  177. esc = true;
  178. } else if ((*p == '\r')||(*p == '\n')||(!*p)) {
  179. dest[j] = (char)0;
  180. return j;
  181. } else {
  182. if (j >= destlen) {
  183. dest[destlen-1] = (char)0;
  184. return (int)(destlen-1);
  185. }
  186. dest[j++] = *p;
  187. }
  188. ++p;
  189. }
  190. }
  191. } else {
  192. ++p;
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Get the contents of a key into a buffer
  199. *
  200. * @param key Key to get
  201. * @param dest Destination buffer
  202. * @return True if key was found (if false, dest will be empty)
  203. */
  204. template<unsigned int C>
  205. inline bool get(const char *key,Buffer<C> &dest) const
  206. {
  207. const int r = this->get(key,const_cast<char *>(reinterpret_cast<const char *>(dest.data())),C);
  208. if (r >= 0) {
  209. dest.setSize((unsigned int)r);
  210. return true;
  211. } else {
  212. dest.clear();
  213. return false;
  214. }
  215. }
  216. /**
  217. * Get a boolean value
  218. *
  219. * @param key Key to look up
  220. * @param dfl Default value if not found in dictionary
  221. * @return Boolean value of key or 'dfl' if not found
  222. */
  223. bool getB(const char *key,bool dfl = false) const
  224. {
  225. char tmp[4];
  226. if (this->get(key,tmp,sizeof(tmp)) >= 0)
  227. return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T'));
  228. return dfl;
  229. }
  230. /**
  231. * Get an unsigned int64 stored as hex in the dictionary
  232. *
  233. * @param key Key to look up
  234. * @param dfl Default value or 0 if unspecified
  235. * @return Decoded hex UInt value or 'dfl' if not found
  236. */
  237. inline uint64_t getUI(const char *key,uint64_t dfl = 0) const
  238. {
  239. char tmp[128];
  240. if (this->get(key,tmp,sizeof(tmp)) >= 1)
  241. return Utils::hexStrToU64(tmp);
  242. return dfl;
  243. }
  244. /**
  245. * Add a new key=value pair
  246. *
  247. * If the key is already present this will append another, but the first
  248. * will always be returned by get(). This is not checked. If you want to
  249. * ensure a key is not present use erase() first.
  250. *
  251. * Use the vlen parameter to add binary values. Nulls will be escaped.
  252. *
  253. * @param key Key -- nulls, CR/LF, and equals (=) are illegal characters
  254. * @param value Value to set
  255. * @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0
  256. * @return True if there was enough room to add this key=value pair
  257. */
  258. inline bool add(const char *key,const char *value,int vlen = -1)
  259. {
  260. for(unsigned int i=0;i<ZT_DICTIONARY_MAX_SIZE;++i) {
  261. if (!_d[i]) {
  262. unsigned int j = i;
  263. const char *p = key;
  264. while (*p) {
  265. _d[j++] = *(p++);
  266. if (j == ZT_DICTIONARY_MAX_SIZE) {
  267. _d[i] = (char)0;
  268. return false;
  269. }
  270. }
  271. p = value;
  272. int k = 0;
  273. while ((*p)&&((vlen < 0)||(k < vlen))) {
  274. switch(*p) {
  275. case 0:
  276. case '\r':
  277. case '\n':
  278. case '\t':
  279. case '\\':
  280. case '=':
  281. _d[j++] = '\\';
  282. if (j == ZT_DICTIONARY_MAX_SIZE) {
  283. _d[i] = (char)0;
  284. return false;
  285. }
  286. switch(*p) {
  287. case 0: _d[j++] = '0'; break;
  288. case '\r': _d[j++] = 'r'; break;
  289. case '\n': _d[j++] = 'n'; break;
  290. case '\t': _d[j++] = 't'; break;
  291. case '\\': _d[j++] = '\\'; break;
  292. case '=': _d[j++] = 'e'; break;
  293. }
  294. if (j == ZT_DICTIONARY_MAX_SIZE) {
  295. _d[i] = (char)0;
  296. return false;
  297. }
  298. break;
  299. default:
  300. _d[j++] = *p;
  301. if (j == ZT_DICTIONARY_MAX_SIZE) {
  302. _d[i] = (char)0;
  303. return false;
  304. }
  305. break;
  306. }
  307. ++p;
  308. ++k;
  309. }
  310. _d[j++] = (char)0;
  311. return true;
  312. }
  313. }
  314. return false;
  315. }
  316. /**
  317. * Add a boolean as a '1' or a '0'
  318. */
  319. inline bool add(const char *key,bool value)
  320. {
  321. return this->add(key,(value) ? "1" : "0",1);
  322. }
  323. /**
  324. * Add a 64-bit integer (unsigned) as a hex value
  325. */
  326. inline bool add(const char *key,uint64_t value)
  327. {
  328. char tmp[32];
  329. Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value);
  330. return this->add(key,tmp,-1);
  331. }
  332. /**
  333. * Add a 64-bit integer (unsigned) as a hex value
  334. */
  335. inline bool add(const char *key,const Address &a)
  336. {
  337. char tmp[32];
  338. Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt());
  339. return this->add(key,tmp,-1);
  340. }
  341. /**
  342. * Add a binary buffer
  343. */
  344. template<unsigned int C>
  345. inline bool add(const char *key,const Buffer<C> &value)
  346. {
  347. return this->add(key,(const char *)value.data(),(int)value.size());
  348. }
  349. /**
  350. * @param key Key to check
  351. * @return True if key is present
  352. */
  353. inline bool contains(const char *key) const
  354. {
  355. char tmp[2];
  356. return (this->get(key,tmp,2) >= 0);
  357. }
  358. /**
  359. * Erase a key from this dictionary
  360. *
  361. * Use this before add() to ensure that a key is replaced if it might
  362. * already be present.
  363. *
  364. * @param key Key to erase
  365. * @return True if key was found and erased
  366. */
  367. inline bool erase(const char *key)
  368. {
  369. char d2[ZT_DICTIONARY_MAX_SIZE];
  370. char *saveptr = (char *)0;
  371. unsigned int d2ptr = 0;
  372. bool found = false;
  373. for(char *f=Utils::stok(_d,"\r\n",&saveptr);(f);f=Utils::stok((char *)0,"\r\n",&saveptr)) {
  374. if (*f) {
  375. const char *p = f;
  376. const char *k = key;
  377. while ((*k)&&(*p)) {
  378. if (*k != *p)
  379. break;
  380. ++k;
  381. ++p;
  382. }
  383. if (*k) {
  384. p = f;
  385. while (*p)
  386. d2[d2ptr++] = *(p++);
  387. d2[d2ptr++] = '\n';
  388. } else {
  389. found = true;
  390. }
  391. }
  392. }
  393. d2[d2ptr++] = (char)0;
  394. memcpy(_d,d2,d2ptr);
  395. return found;
  396. }
  397. /**
  398. * @return Dictionary data as a 0-terminated C-string
  399. */
  400. inline const char *data() const { return _d; }
  401. private:
  402. char _d[ZT_DICTIONARY_MAX_SIZE];
  403. };
  404. } // namespace ZeroTier
  405. #endif