CV2.hpp 2.7 KB

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