/* * ZeroTier One - Network Virtualization Everywhere * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * -- * * You can be released from the requirements of the license by purchasing * a commercial license. Buying such a license is mandatory as soon as you * develop commercial closed-source software that incorporates or links * directly against ZeroTier software without disclosing the source code * of your own application. */ #ifndef ZT_DICTIONARY_HPP #define ZT_DICTIONARY_HPP #include "Constants.hpp" #include "Utils.hpp" #include "Buffer.hpp" #include "Address.hpp" #include namespace ZeroTier { /** * A small (in code and data) packed key=value store * * This stores data in the form of a compact blob that is sort of human * readable (depending on whether you put binary data in it) and is backward * compatible with older versions. Binary data is escaped such that the * serialized form of a Dictionary is always a valid null-terminated C string. * * Keys are restricted: no binary data, no CR/LF, and no equals (=). If a key * contains these characters it may not be retrievable. This is not checked. * * Lookup is via linear search and will be slow with a lot of keys. It's * designed for small things. * * There is code to test and fuzz this in selftest.cpp. Fuzzing a blob of * pointer tricks like this is important after any modifications. * * This is used for network configurations and for saving some things on disk * in the ZeroTier One service code. * * @tparam C Dictionary max capacity in bytes */ template class Dictionary { public: Dictionary() { _d[0] = (char)0; } Dictionary(const char *s) { if (s) { Utils::scopy(_d,sizeof(_d),s); } else { _d[0] = (char)0; } } Dictionary(const char *s,unsigned int len) { if (s) { if (len > (C-1)) len = C-1; memcpy(_d,s,len); _d[len] = (char)0; } else { _d[0] = (char)0; } } Dictionary(const Dictionary &d) { Utils::scopy(_d,sizeof(_d),d._d); } inline Dictionary &operator=(const Dictionary &d) { Utils::scopy(_d,sizeof(_d),d._d); return *this; } inline operator bool() const { return (_d[0] != 0); } /** * Load a dictionary from a C-string * * @param s Dictionary in string form * @return False if 's' was longer than our capacity */ inline bool load(const char *s) { if (s) { return Utils::scopy(_d,sizeof(_d),s); } else { _d[0] = (char)0; return true; } } /** * Delete all entries */ inline void clear() { _d[0] = (char)0; } /** * @return Size of dictionary in bytes not including terminating NULL */ inline unsigned int sizeBytes() const { for(unsigned int i=0;i inline bool get(const char *key,Buffer &dest) const { const int r = this->get(key,const_cast(reinterpret_cast(dest.data())),BC); if (r >= 0) { dest.setSize((unsigned int)r); return true; } else { dest.clear(); return false; } } /** * Get a boolean value * * @param key Key to look up * @param dfl Default value if not found in dictionary * @return Boolean value of key or 'dfl' if not found */ bool getB(const char *key,bool dfl = false) const { char tmp[4]; if (this->get(key,tmp,sizeof(tmp)) >= 0) return ((*tmp == '1')||(*tmp == 't')||(*tmp == 'T')); return dfl; } /** * Get an unsigned int64 stored as hex in the dictionary * * @param key Key to look up * @param dfl Default value or 0 if unspecified * @return Decoded hex UInt value or 'dfl' if not found */ inline uint64_t getUI(const char *key,uint64_t dfl = 0) const { char tmp[128]; if (this->get(key,tmp,sizeof(tmp)) >= 1) return Utils::hexStrToU64(tmp); return dfl; } /** * Add a new key=value pair * * If the key is already present this will append another, but the first * will always be returned by get(). This is not checked. If you want to * ensure a key is not present use erase() first. * * Use the vlen parameter to add binary values. Nulls will be escaped. * * @param key Key -- nulls, CR/LF, and equals (=) are illegal characters * @param value Value to set * @param vlen Length of value in bytes or -1 to treat value[] as a C-string and look for terminating 0 * @return True if there was enough room to add this key=value pair */ inline bool add(const char *key,const char *value,int vlen = -1) { for(unsigned int i=0;i 0) { _d[j++] = (char)10; if (j == C) { _d[i] = (char)0; return false; } } const char *p = key; while (*p) { _d[j++] = *(p++); if (j == C) { _d[i] = (char)0; return false; } } _d[j++] = '='; if (j == C) { _d[i] = (char)0; return false; } p = value; int k = 0; while ( ((vlen < 0)&&(*p)) || (k < vlen) ) { switch(*p) { case 0: case 13: case 10: case '\\': case '=': _d[j++] = '\\'; if (j == C) { _d[i] = (char)0; return false; } switch(*p) { case 0: _d[j++] = '0'; break; case 13: _d[j++] = 'r'; break; case 10: _d[j++] = 'n'; break; case '\\': _d[j++] = '\\'; break; case '=': _d[j++] = 'e'; break; } if (j == C) { _d[i] = (char)0; return false; } break; default: _d[j++] = *p; if (j == C) { _d[i] = (char)0; return false; } break; } ++p; ++k; } _d[j] = (char)0; return true; } } return false; } /** * Add a boolean as a '1' or a '0' */ inline bool add(const char *key,bool value) { return this->add(key,(value) ? "1" : "0",1); } /** * Add a 64-bit integer (unsigned) as a hex value */ inline bool add(const char *key,uint64_t value) { char tmp[32]; Utils::snprintf(tmp,sizeof(tmp),"%llx",(unsigned long long)value); return this->add(key,tmp,-1); } /** * Add a 64-bit integer (unsigned) as a hex value */ inline bool add(const char *key,const Address &a) { char tmp[32]; Utils::snprintf(tmp,sizeof(tmp),"%.10llx",(unsigned long long)a.toInt()); return this->add(key,tmp,-1); } /** * Add a binary buffer's contents as a value * * @tparam BC Buffer capacity (usually inferred) */ template inline bool add(const char *key,const Buffer &value) { return this->add(key,(const char *)value.data(),(int)value.size()); } /** * @param key Key to check * @return True if key is present */ inline bool contains(const char *key) const { char tmp[2]; return (this->get(key,tmp,2) >= 0); } /** * @return Value of C template parameter */ inline unsigned int capacity() const { return C; } inline const char *data() const { return _d; } inline char *unsafeData() { return _d; } private: char _d[C]; }; } // namespace ZeroTier #endif