CV2.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c)2025 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. #include "DB.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #ifndef ZT_CONTROLLER_CV2_HPP
  16. #define ZT_CONTROLLER_CV2_HPP
  17. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  18. #include "ConnectionPool.hpp"
  19. #include <pqxx/pqxx>
  20. #include <memory>
  21. #include <redis++/redis++.h>
  22. #include "../node/Metrics.hpp"
  23. #include "PostgreSQL.hpp"
  24. namespace ZeroTier {
  25. class CV2 : public DB
  26. {
  27. public:
  28. CV2(const Identity &myId, const char *path, int listenPort);
  29. virtual ~CV2();
  30. virtual bool waitForReady();
  31. virtual bool isReady();
  32. virtual bool save(nlohmann::json &record,bool notifyListeners);
  33. virtual void eraseNetwork(const uint64_t networkId);
  34. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  35. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  36. virtual AuthInfo getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL);
  37. virtual bool ready() {
  38. return _ready == 2;
  39. }
  40. protected:
  41. struct _PairHasher
  42. {
  43. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  44. };
  45. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) {
  46. DB::_memberChanged(old, memberConfig, notifyListeners);
  47. }
  48. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) {
  49. DB::_networkChanged(old, networkConfig, notifyListeners);
  50. }
  51. private:
  52. void initializeNetworks();
  53. void initializeMembers();
  54. void heartbeat();
  55. void membersDbWatcher();
  56. void networksDbWatcher();
  57. void commitThread();
  58. void onlineNotificationThread();
  59. // void notifyNewMember(const std::string &networkID, const std::string &memberID);
  60. enum OverrideMode {
  61. ALLOW_PGBOUNCER_OVERRIDE = 0,
  62. NO_OVERRIDE = 1
  63. };
  64. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  65. const Identity _myId;
  66. const Address _myAddress;
  67. std::string _myAddressStr;
  68. std::string _connString;
  69. BlockingQueue< std::pair<nlohmann::json,bool> > _commitQueue;
  70. std::thread _heartbeatThread;
  71. std::thread _membersDbWatcher;
  72. std::thread _networksDbWatcher;
  73. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  74. std::thread _onlineNotificationThread;
  75. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  76. mutable std::mutex _lastOnline_l;
  77. mutable std::mutex _readyLock;
  78. std::atomic<int> _ready, _connected, _run;
  79. mutable volatile bool _waitNoticePrinted;
  80. int _listenPort;
  81. uint8_t _ssoPsk[48];
  82. };
  83. } // namespace Zerotier
  84. #endif // ZT_CONTROLLER_CV2_HPP
  85. #endif // ZT_CONTROLLER_USE_LIBPQ