LFDB.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: 2025-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 <mutex>
  17. #include <string>
  18. #include <unordered_map>
  19. #include <atomic>
  20. namespace ZeroTier {
  21. /**
  22. * DB implementation for controller that stores data in LF
  23. */
  24. class LFDB : public DB
  25. {
  26. public:
  27. /**
  28. * @param myId This controller's identity
  29. * @param path Base path for ZeroTier node itself
  30. * @param lfOwnerPrivate LF owner private in PEM format
  31. * @param lfOwnerPublic LF owner public in @base62 format
  32. * @param lfNodeHost LF node host
  33. * @param lfNodePort LF node http (not https) port
  34. * @param storeOnlineState If true, store online/offline state and IP info in LF (a lot of data, only for private networks!)
  35. */
  36. LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,const char *lfOwnerPublic,const char *lfNodeHost,int lfNodePort,bool storeOnlineState);
  37. virtual ~LFDB();
  38. virtual bool waitForReady();
  39. virtual bool isReady();
  40. virtual bool save(nlohmann::json &record,bool notifyListeners);
  41. virtual void eraseNetwork(const uint64_t networkId);
  42. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
  43. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  44. protected:
  45. const Identity _myId;
  46. std::string _lfOwnerPrivate,_lfOwnerPublic;
  47. std::string _lfNodeHost;
  48. int _lfNodePort;
  49. struct _MemberState
  50. {
  51. _MemberState() :
  52. lastOnlineAddress(),
  53. lastOnlineTime(0),
  54. dirty(false),
  55. lastOnlineDirty(false) {}
  56. InetAddress lastOnlineAddress;
  57. int64_t lastOnlineTime;
  58. bool dirty;
  59. bool lastOnlineDirty;
  60. };
  61. struct _NetworkState
  62. {
  63. _NetworkState() :
  64. members(),
  65. dirty(false) {}
  66. std::unordered_map<uint64_t,_MemberState> members;
  67. bool dirty;
  68. };
  69. std::unordered_map<uint64_t,_NetworkState> _state;
  70. std::mutex _state_l;
  71. std::atomic_bool _running;
  72. std::atomic_bool _ready;
  73. std::thread _syncThread;
  74. bool _storeOnlineState;
  75. };
  76. } // namespace ZeroTier
  77. #endif