PostgreSQL.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifdef ZT_CONTROLLER_USE_LIBPQ
  27. #ifndef ZT_CONTROLLER_LIBPQ_HPP
  28. #define ZT_CONTROLLER_LIBPQ_HPP
  29. #include "DB.hpp"
  30. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  31. extern "C" {
  32. typedef struct pg_conn PGconn;
  33. }
  34. namespace ZeroTier
  35. {
  36. struct mq_config
  37. {
  38. const char *host;
  39. int port;
  40. const char *username;
  41. const char *password;
  42. };
  43. /**
  44. * A controller database driver that talks to PostgreSQL
  45. *
  46. * This is for use with ZeroTier Central. Others are free to build and use it
  47. * but be aware taht we might change it at any time.
  48. */
  49. class PostgreSQL : public DB
  50. {
  51. public:
  52. PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId, const char *path, int listenPort, mq_config *mqc = NULL);
  53. virtual ~PostgreSQL();
  54. virtual bool waitForReady();
  55. virtual bool isReady();
  56. virtual void save(nlohmann::json *orig, nlohmann::json &record);
  57. virtual void eraseNetwork(const uint64_t networkId);
  58. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  59. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  60. protected:
  61. struct _PairHasher
  62. {
  63. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  64. };
  65. private:
  66. void initializeNetworks(PGconn *conn);
  67. void initializeMembers(PGconn *conn);
  68. void heartbeat();
  69. void membersDbWatcher();
  70. void _membersWatcher_Postgres(PGconn *conn);
  71. void _membersWatcher_RabbitMQ();
  72. void networksDbWatcher();
  73. void _networksWatcher_Postgres(PGconn *conn);
  74. void _networksWatcher_RabbitMQ();
  75. void commitThread();
  76. void onlineNotificationThread();
  77. enum OverrideMode {
  78. ALLOW_PGBOUNCER_OVERRIDE = 0,
  79. NO_OVERRIDE = 1
  80. };
  81. PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE );
  82. std::string _connString;
  83. BlockingQueue<nlohmann::json *> _commitQueue;
  84. std::thread _heartbeatThread;
  85. std::thread _membersDbWatcher;
  86. std::thread _networksDbWatcher;
  87. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  88. std::thread _onlineNotificationThread;
  89. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  90. mutable std::mutex _lastOnline_l;
  91. mutable std::mutex _readyLock;
  92. std::atomic<int> _ready, _connected, _run;
  93. mutable volatile bool _waitNoticePrinted;
  94. int _listenPort;
  95. mq_config *_mqc;
  96. };
  97. }
  98. #endif // ZT_CONTROLLER_LIBPQ_HPP
  99. #endif // ZT_CONTROLLER_USE_LIBPQ