LFDB.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_CONTROLLER_LFDB_HPP
  14. #define ZT_CONTROLLER_LFDB_HPP
  15. #include "DB.hpp"
  16. #include <atomic>
  17. #include <mutex>
  18. #include <string>
  19. #include <unordered_map>
  20. namespace ZeroTier {
  21. /**
  22. * DB implementation for controller that stores data in LF
  23. */
  24. class LFDB : public DB {
  25. public:
  26. /**
  27. * @param myId This controller's identity
  28. * @param path Base path for ZeroTier node itself
  29. * @param lfOwnerPrivate LF owner private in PEM format
  30. * @param lfOwnerPublic LF owner public in @base62 format
  31. * @param lfNodeHost LF node host
  32. * @param lfNodePort LF node http (not https) port
  33. * @param storeOnlineState If true, store online/offline state and IP info in LF (a lot of data, only for private networks!)
  34. */
  35. LFDB(const Identity& myId, const char* path, const char* lfOwnerPrivate, const char* lfOwnerPublic, const char* lfNodeHost, int lfNodePort, bool storeOnlineState);
  36. virtual ~LFDB();
  37. virtual bool waitForReady();
  38. virtual bool isReady();
  39. virtual bool save(nlohmann::json& record, bool notifyListeners);
  40. virtual void eraseNetwork(const uint64_t networkId);
  41. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  42. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress);
  43. protected:
  44. const Identity _myId;
  45. std::string _lfOwnerPrivate, _lfOwnerPublic;
  46. std::string _lfNodeHost;
  47. int _lfNodePort;
  48. struct _MemberState {
  49. _MemberState() : lastOnlineAddress(), lastOnlineTime(0), dirty(false), lastOnlineDirty(false)
  50. {
  51. }
  52. InetAddress lastOnlineAddress;
  53. int64_t lastOnlineTime;
  54. bool dirty;
  55. bool lastOnlineDirty;
  56. };
  57. struct _NetworkState {
  58. _NetworkState() : members(), dirty(false)
  59. {
  60. }
  61. std::unordered_map<uint64_t, _MemberState> members;
  62. bool dirty;
  63. };
  64. std::unordered_map<uint64_t, _NetworkState> _state;
  65. std::mutex _state_l;
  66. std::atomic_bool _running;
  67. std::atomic_bool _ready;
  68. std::thread _syncThread;
  69. bool _storeOnlineState;
  70. };
  71. } // namespace ZeroTier
  72. #endif