CV2.hpp 3.1 KB

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