PostgreSQL.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #ifdef ZT_CONTROLLER_USE_LIBPQ
  14. #ifndef ZT_CONTROLLER_POSTGRESQL_HPP
  15. #define ZT_CONTROLLER_POSTGRESQL_HPP
  16. #include "ConnectionPool.hpp"
  17. #include "DB.hpp"
  18. #include "opentelemetry/trace/provider.h"
  19. #include <memory>
  20. #include <nlohmann/json.hpp>
  21. #include <pqxx/pqxx>
  22. namespace ZeroTier {
  23. extern "C" {
  24. typedef struct pg_conn PGconn;
  25. }
  26. class PostgresConnection : public Connection {
  27. public:
  28. virtual ~PostgresConnection()
  29. {
  30. }
  31. std::shared_ptr<pqxx::connection> c;
  32. int a;
  33. };
  34. class PostgresConnFactory : public ConnectionFactory {
  35. public:
  36. PostgresConnFactory(std::string& connString) : m_connString(connString)
  37. {
  38. }
  39. virtual std::shared_ptr<Connection> create()
  40. {
  41. Metrics::conn_counter++;
  42. auto c = std::shared_ptr<PostgresConnection>(new PostgresConnection());
  43. c->c = std::make_shared<pqxx::connection>(m_connString);
  44. return std::static_pointer_cast<Connection>(c);
  45. }
  46. private:
  47. std::string m_connString;
  48. };
  49. template <typename T> class MemberNotificationReceiver : public pqxx::notification_receiver {
  50. public:
  51. MemberNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  52. {
  53. fprintf(stderr, "initialize MemberNotificationReceiver\n");
  54. }
  55. virtual ~MemberNotificationReceiver()
  56. {
  57. fprintf(stderr, "MemberNotificationReceiver destroyed\n");
  58. }
  59. virtual void operator()(const std::string& payload, int backendPid)
  60. {
  61. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  62. auto tracer = provider->GetTracer("db_member_notification");
  63. auto span = tracer->StartSpan("db_member_notification::operator()");
  64. auto scope = tracer->WithActiveSpan(span);
  65. span->SetAttribute("payload", payload);
  66. span->SetAttribute("psqlReady", _psql->isReady());
  67. fprintf(stderr, "Member Notification received: %s\n", payload.c_str());
  68. Metrics::pgsql_mem_notification++;
  69. nlohmann::json tmp(nlohmann::json::parse(payload));
  70. nlohmann::json& ov = tmp["old_val"];
  71. nlohmann::json& nv = tmp["new_val"];
  72. nlohmann::json oldConfig, newConfig;
  73. if (ov.is_object())
  74. oldConfig = ov;
  75. if (nv.is_object())
  76. newConfig = nv;
  77. if (oldConfig.is_object() && newConfig.is_object()) {
  78. _psql->save(newConfig, _psql->isReady());
  79. fprintf(stderr, "payload sent\n");
  80. }
  81. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  82. // new member
  83. Metrics::member_count++;
  84. _psql->save(newConfig, _psql->isReady());
  85. fprintf(stderr, "new member payload sent\n");
  86. }
  87. else if (! newConfig.is_object() && oldConfig.is_object()) {
  88. // member delete
  89. uint64_t networkId = OSUtils::jsonIntHex(oldConfig["nwid"], 0ULL);
  90. uint64_t memberId = OSUtils::jsonIntHex(oldConfig["id"], 0ULL);
  91. if (memberId && networkId) {
  92. _psql->eraseMember(networkId, memberId);
  93. fprintf(stderr, "member delete payload sent\n");
  94. }
  95. }
  96. }
  97. private:
  98. T* _psql;
  99. };
  100. template <typename T> class NetworkNotificationReceiver : public pqxx::notification_receiver {
  101. public:
  102. NetworkNotificationReceiver(T* p, pqxx::connection& c, const std::string& channel) : pqxx::notification_receiver(c, channel), _psql(p)
  103. {
  104. fprintf(stderr, "initialize NetworkrNotificationReceiver\n");
  105. }
  106. virtual ~NetworkNotificationReceiver()
  107. {
  108. fprintf(stderr, "NetworkNotificationReceiver destroyed\n");
  109. };
  110. virtual void operator()(const std::string& payload, int packend_pid)
  111. {
  112. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  113. auto tracer = provider->GetTracer("db_network_notification");
  114. auto span = tracer->StartSpan("db_network_notification::operator()");
  115. auto scope = tracer->WithActiveSpan(span);
  116. span->SetAttribute("payload", payload);
  117. span->SetAttribute("psqlReady", _psql->isReady());
  118. fprintf(stderr, "Network Notification received: %s\n", payload.c_str());
  119. Metrics::pgsql_net_notification++;
  120. nlohmann::json tmp(nlohmann::json::parse(payload));
  121. nlohmann::json& ov = tmp["old_val"];
  122. nlohmann::json& nv = tmp["new_val"];
  123. nlohmann::json oldConfig, newConfig;
  124. if (ov.is_object())
  125. oldConfig = ov;
  126. if (nv.is_object())
  127. newConfig = nv;
  128. if (oldConfig.is_object() && newConfig.is_object()) {
  129. std::string nwid = oldConfig["id"];
  130. span->SetAttribute("action", "network_change");
  131. span->SetAttribute("network_id", nwid);
  132. _psql->save(newConfig, _psql->isReady());
  133. fprintf(stderr, "payload sent\n");
  134. }
  135. else if (newConfig.is_object() && ! oldConfig.is_object()) {
  136. std::string nwid = newConfig["id"];
  137. span->SetAttribute("network_id", nwid);
  138. span->SetAttribute("action", "new_network");
  139. // new network
  140. _psql->save(newConfig, _psql->isReady());
  141. fprintf(stderr, "new network payload sent\n");
  142. }
  143. else if (! newConfig.is_object() && oldConfig.is_object()) {
  144. // network delete
  145. span->SetAttribute("action", "delete_network");
  146. std::string nwid = oldConfig["id"];
  147. span->SetAttribute("network_id", nwid);
  148. uint64_t networkId = Utils::hexStrToU64(nwid.c_str());
  149. span->SetAttribute("network_id_int", networkId);
  150. if (networkId) {
  151. _psql->eraseNetwork(networkId);
  152. fprintf(stderr, "network delete payload sent\n");
  153. }
  154. }
  155. }
  156. private:
  157. T* _psql;
  158. };
  159. struct NodeOnlineRecord {
  160. uint64_t lastSeen;
  161. InetAddress physicalAddress;
  162. std::string osArch;
  163. };
  164. } // namespace ZeroTier
  165. #endif // ZT_CONTROLLER_POSTGRESQL_HPP
  166. #endif // ZT_CONTROLLER_USE_LIBPQ