LFDB.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CONTROLLER_LFDB_HPP
  27. #define ZT_CONTROLLER_LFDB_HPP
  28. #include "DB.hpp"
  29. #include <mutex>
  30. #include <string>
  31. #include <unordered_map>
  32. #include <atomic>
  33. namespace ZeroTier {
  34. /**
  35. * DB implementation for controller that stores data in LF
  36. */
  37. class LFDB : public DB
  38. {
  39. public:
  40. /**
  41. * @param nc Network controller
  42. * @param myId Identity of controller node (with secret)
  43. * @param path Base path for ZeroTier node itself
  44. * @param lfOwnerPrivate LF owner private in PEM format
  45. * @param lfOwnerPublic LF owner public in @base62 format
  46. * @param lfNodeHost LF node host
  47. * @param lfNodePort LF node http (not https) port
  48. * @param storeOnlineState If true, store online/offline state and IP info in LF (a lot of data, only for private networks!)
  49. */
  50. LFDB(EmbeddedNetworkController *const nc,const Identity &myId,const char *path,const char *lfOwnerPrivate,const char *lfOwnerPublic,const char *lfNodeHost,int lfNodePort,bool storeOnlineState);
  51. virtual ~LFDB();
  52. virtual bool waitForReady();
  53. virtual bool isReady();
  54. virtual void save(nlohmann::json *orig,nlohmann::json &record);
  55. virtual void eraseNetwork(const uint64_t networkId);
  56. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
  57. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  58. protected:
  59. EmbeddedNetworkController *const _nc;
  60. const Identity _myId;
  61. std::string _lfOwnerPrivate;
  62. std::string _lfOwnerPublic;
  63. std::string _lfNodeHost;
  64. int _lfNodePort;
  65. struct _MemberState
  66. {
  67. InetAddress lastOnlineAddress;
  68. int64_t lastOnlineTime;
  69. bool dirty;
  70. bool lastOnlineDirty;
  71. };
  72. struct _NetworkState
  73. {
  74. std::unordered_map<uint64_t,_MemberState> members;
  75. bool dirty;
  76. };
  77. std::unordered_map<uint64_t,_NetworkState> _state;
  78. std::mutex _state_l;
  79. std::atomic_bool _running;
  80. std::atomic_bool _ready;
  81. std::thread _syncThread;
  82. bool _storeOnlineState;
  83. };
  84. } // namespace ZeroTier
  85. #endif