PostgreSQL.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2018 ZeroTier, Inc.
  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. #ifdef ZT_CONTROLLER_USE_LIBPQ
  19. #ifndef ZT_CONTROLLER_LIBPQ_HPP
  20. #define ZT_CONTROLLER_LIBPQ_HPP
  21. #include "DB.hpp"
  22. #define ZT_CENTRAL_CONTROLLER_COMMIT_THREADS 4
  23. extern "C" {
  24. typedef struct pg_conn PGconn;
  25. }
  26. namespace ZeroTier
  27. {
  28. /**
  29. * A controller database driver that talks to PostgreSQL
  30. *
  31. * This is for use with ZeroTier Central. Others are free to build and use it
  32. * but be aware taht we might change it at any time.
  33. */
  34. class PostgreSQL : public DB
  35. {
  36. public:
  37. PostgreSQL(EmbeddedNetworkController *const nc, const Identity &myId, const char *path);
  38. virtual ~PostgreSQL();
  39. virtual bool waitForReady();
  40. virtual bool isReady();
  41. virtual void save(nlohmann::json *orig, nlohmann::json &record);
  42. virtual void eraseNetwork(const uint64_t networkId);
  43. virtual void eraseMember(const uint64_t networkId, const uint64_t memberId);
  44. virtual void nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress);
  45. protected:
  46. struct _PairHasher
  47. {
  48. inline std::size_t operator()(const std::pair<uint64_t,uint64_t> &p) const { return (std::size_t)(p.first ^ p.second); }
  49. };
  50. private:
  51. void initializeNetworks(PGconn *conn);
  52. void initializeMembers(PGconn *conn);
  53. void heartbeat();
  54. void membersDbWatcher();
  55. void networksDbWatcher();
  56. void commitThread();
  57. void onlineNotificationThread();
  58. enum OverrideMode {
  59. ALLOW_PGBOUNCER_OVERRIDE = 0,
  60. NO_OVERRIDE = 1
  61. };
  62. PGconn * getPgConn( OverrideMode m = ALLOW_PGBOUNCER_OVERRIDE );
  63. std::string _connString;
  64. BlockingQueue<nlohmann::json *> _commitQueue;
  65. std::thread _heartbeatThread;
  66. std::thread _membersDbWatcher;
  67. std::thread _networksDbWatcher;
  68. std::thread _commitThread[ZT_CENTRAL_CONTROLLER_COMMIT_THREADS];
  69. std::thread _onlineNotificationThread;
  70. std::unordered_map< std::pair<uint64_t,uint64_t>,std::pair<int64_t,InetAddress>,_PairHasher > _lastOnline;
  71. mutable std::mutex _lastOnline_l;
  72. mutable std::mutex _readyLock;
  73. std::atomic<int> _ready, _connected, _run;
  74. mutable volatile bool _waitNoticePrinted;
  75. };
  76. }
  77. #endif // ZT_CONTROLLER_LIBPQ_HPP
  78. #endif // ZT_CONTROLLER_USE_LIBPQ