LFDB.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. _MemberState() :
  68. lastOnlineAddress(),
  69. lastOnlineTime(0),
  70. recordTimestamp(0),
  71. dirty(false),
  72. lastOnlineDirty(false) {}
  73. InetAddress lastOnlineAddress;
  74. int64_t lastOnlineTime;
  75. int64_t recordTimestamp;
  76. bool dirty;
  77. bool lastOnlineDirty;
  78. };
  79. struct _NetworkState
  80. {
  81. _NetworkState() :
  82. members(),
  83. recordTimestamp(0),
  84. dirty(false) {}
  85. std::unordered_map<uint64_t,_MemberState> members;
  86. int64_t recordTimestamp;
  87. bool dirty;
  88. };
  89. std::unordered_map<uint64_t,_NetworkState> _state;
  90. std::mutex _state_l;
  91. std::atomic_bool _running;
  92. std::atomic_bool _ready;
  93. std::thread _syncThread;
  94. bool _storeOnlineState;
  95. };
  96. } // namespace ZeroTier
  97. #endif