LFDB.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 myId This controller's identity
  42. * @param path Base path for ZeroTier node itself
  43. * @param lfOwnerPrivate LF owner private in PEM format
  44. * @param lfOwnerPublic LF owner public in @base62 format
  45. * @param lfNodeHost LF node host
  46. * @param lfNodePort LF node http (not https) port
  47. * @param storeOnlineState If true, store online/offline state and IP info in LF (a lot of data, only for private networks!)
  48. */
  49. LFDB(const Identity &myId,const char *path,const char *lfOwnerPrivate,const char *lfOwnerPublic,const char *lfNodeHost,int lfNodePort,bool storeOnlineState);
  50. virtual ~LFDB();
  51. virtual bool waitForReady();
  52. virtual bool isReady();
  53. virtual bool save(nlohmann::json &record,bool notifyListeners);
  54. virtual void eraseNetwork(const uint64_t networkId);
  55. virtual void eraseMember(const uint64_t networkId,const uint64_t memberId);
  56. virtual void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress);
  57. protected:
  58. const Identity _myId;
  59. std::string _lfOwnerPrivate,_lfOwnerPublic;
  60. std::string _lfNodeHost;
  61. int _lfNodePort;
  62. struct _MemberState
  63. {
  64. _MemberState() :
  65. lastOnlineAddress(),
  66. lastOnlineTime(0),
  67. dirty(false),
  68. lastOnlineDirty(false) {}
  69. InetAddress lastOnlineAddress;
  70. int64_t lastOnlineTime;
  71. bool dirty;
  72. bool lastOnlineDirty;
  73. };
  74. struct _NetworkState
  75. {
  76. _NetworkState() :
  77. members(),
  78. dirty(false) {}
  79. std::unordered_map<uint64_t,_MemberState> members;
  80. bool dirty;
  81. };
  82. std::unordered_map<uint64_t,_NetworkState> _state;
  83. std::mutex _state_l;
  84. std::atomic_bool _running;
  85. std::atomic_bool _ready;
  86. std::thread _syncThread;
  87. bool _storeOnlineState;
  88. };
  89. } // namespace ZeroTier
  90. #endif