CV2.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 void nodeIsOnline(const uint64_t networkId,const uint64_t memberId,const InetAddress &physicalAddress, const char *osArch);
  37. virtual AuthInfo getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL);
  38. virtual bool ready() {
  39. return _ready == 2;
  40. }
  41. protected:
  42. struct _PairHasher
  43. {
  44. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  45. };
  46. virtual void _memberChanged(nlohmann::json &old,nlohmann::json &memberConfig,bool notifyListeners) {
  47. DB::_memberChanged(old, memberConfig, notifyListeners);
  48. }
  49. virtual void _networkChanged(nlohmann::json &old,nlohmann::json &networkConfig,bool notifyListeners) {
  50. DB::_networkChanged(old, networkConfig, notifyListeners);
  51. }
  52. private:
  53. void initializeNetworks();
  54. void initializeMembers();
  55. void heartbeat();
  56. void membersDbWatcher();
  57. void networksDbWatcher();
  58. void commitThread();
  59. void onlineNotificationThread();
  60. // void notifyNewMember(const std::string &networkID, const std::string &memberID);
  61. enum OverrideMode {
  62. ALLOW_PGBOUNCER_OVERRIDE = 0,
  63. NO_OVERRIDE = 1
  64. };
  65. std::shared_ptr<ConnectionPool<PostgresConnection> > _pool;
  66. const Identity _myId;
  67. const Address _myAddress;
  68. std::string _myAddressStr;
  69. std::string _connString;
  70. BlockingQueue< std::pair<nlohmann::json,bool> > _commitQueue;
  71. std::thread _heartbeatThread;
  72. std::thread _membersDbWatcher;
  73. std::thread _networksDbWatcher;
  74. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  75. std::thread _onlineNotificationThread;
  76. std::unordered_map< std::pair<uint64_t,uint64_t>,NodeOnlineRecord,_PairHasher > _lastOnline;
  77. mutable std::mutex _lastOnline_l;
  78. mutable std::mutex _readyLock;
  79. std::atomic<int> _ready, _connected, _run;
  80. mutable volatile bool _waitNoticePrinted;
  81. int _listenPort;
  82. uint8_t _ssoPsk[48];
  83. };
  84. } // namespace Zerotier
  85. #endif // ZT_CONTROLLER_CV2_HPP
  86. #endif // ZT_CONTROLLER_USE_LIBPQ