CV2.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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 "CV2.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #include "../node/Constants.hpp"
  16. #include "../node/SHA512.hpp"
  17. #include "../version.h"
  18. #include "CtlUtil.hpp"
  19. #include "EmbeddedNetworkController.hpp"
  20. #include "opentelemetry/trace/provider.h"
  21. #include <chrono>
  22. #include <climits>
  23. #include <iomanip>
  24. #include <libpq-fe.h>
  25. #include <sstream>
  26. using json = nlohmann::json;
  27. namespace {
  28. }
  29. using namespace ZeroTier;
  30. CV2::CV2(const Identity& myId, const char* path, int listenPort) : DB(), _pool(), _myId(myId), _myAddress(myId.address()), _ready(0), _connected(1), _run(1), _waitNoticePrinted(false), _listenPort(listenPort)
  31. {
  32. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  33. auto tracer = provider->GetTracer("cv2");
  34. auto span = tracer->StartSpan("cv2::CV2");
  35. auto scope = tracer->WithActiveSpan(span);
  36. fprintf(stderr, "CV2::CV2\n");
  37. char myAddress[64];
  38. _myAddressStr = myId.address().toString(myAddress);
  39. _connString = std::string(path);
  40. auto f = std::make_shared<PostgresConnFactory>(_connString);
  41. _pool = std::make_shared<ConnectionPool<PostgresConnection> >(15, 5, std::static_pointer_cast<ConnectionFactory>(f));
  42. memset(_ssoPsk, 0, sizeof(_ssoPsk));
  43. char* const ssoPskHex = getenv("ZT_SSO_PSK");
  44. #ifdef ZT_TRACE
  45. fprintf(stderr, "ZT_SSO_PSK: %s\n", ssoPskHex);
  46. #endif
  47. if (ssoPskHex) {
  48. // SECURITY: note that ssoPskHex will always be null-terminated if libc actually
  49. // returns something non-NULL. If the hex encodes something shorter than 48 bytes,
  50. // it will be padded at the end with zeroes. If longer, it'll be truncated.
  51. Utils::unhex(ssoPskHex, _ssoPsk, sizeof(_ssoPsk));
  52. }
  53. _readyLock.lock();
  54. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL waiting for initial data download..." ZT_EOL_S, ::_timestr(), (unsigned long long)_myAddress.toInt());
  55. _waitNoticePrinted = true;
  56. initializeNetworks();
  57. initializeMembers();
  58. _heartbeatThread = std::thread(&CV2::heartbeat, this);
  59. _membersDbWatcher = std::thread(&CV2::membersDbWatcher, this);
  60. _networksDbWatcher = std::thread(&CV2::networksDbWatcher, this);
  61. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  62. _commitThread[i] = std::thread(&CV2::commitThread, this);
  63. }
  64. _onlineNotificationThread = std::thread(&CV2::onlineNotificationThread, this);
  65. }
  66. CV2::~CV2()
  67. {
  68. _run = 0;
  69. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  70. _heartbeatThread.join();
  71. _membersDbWatcher.join();
  72. _networksDbWatcher.join();
  73. _commitQueue.stop();
  74. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  75. _commitThread[i].join();
  76. }
  77. _onlineNotificationThread.join();
  78. }
  79. bool CV2::waitForReady()
  80. {
  81. while (_ready < 2) {
  82. _readyLock.lock();
  83. _readyLock.unlock();
  84. }
  85. return true;
  86. }
  87. bool CV2::isReady()
  88. {
  89. return (_ready == 2) && _connected;
  90. }
  91. void CV2::_memberChanged(nlohmann::json& old, nlohmann::json& memberConfig, bool notifyListeners)
  92. {
  93. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  94. auto tracer = provider->GetTracer("cv2");
  95. auto span = tracer->StartSpan("cv2::_memberChanged");
  96. auto scope = tracer->WithActiveSpan(span);
  97. if (memberConfig.is_object()) {
  98. // member config change
  99. const std::string ids = memberConfig["id"];
  100. const uint64_t networkId = OSUtils::jsonIntHex(memberConfig["nwid"], 0ULL);
  101. const uint64_t memberId = Utils::hexStrToU64(ids.c_str());
  102. if ((networkId) && (memberId)) {
  103. save(memberConfig, notifyListeners);
  104. }
  105. }
  106. else if (old.is_object()) {
  107. // member delete
  108. const std::string ids = old["id"];
  109. const uint64_t networkId = OSUtils::jsonIntHex(old["nwid"], 0ULL);
  110. const uint64_t memberId = Utils::hexStrToU64(ids.c_str());
  111. if ((networkId) && (memberId)) {
  112. eraseMember(networkId, memberId);
  113. }
  114. }
  115. if (notifyListeners) {
  116. // fprintf(stderr, "CV2::_memberChanged\n");
  117. DB::_memberChanged(old, memberConfig, notifyListeners);
  118. }
  119. }
  120. void CV2::_networkChanged(nlohmann::json& old, nlohmann::json& networkConfig, bool notifyListeners)
  121. {
  122. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  123. auto tracer = provider->GetTracer("cv2");
  124. auto span = tracer->StartSpan("cv2::_networkChanged");
  125. auto scope = tracer->WithActiveSpan(span);
  126. if (networkConfig.is_object()) {
  127. // network config change
  128. const std::string ids = networkConfig["id"];
  129. const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
  130. if (networkId) {
  131. save(networkConfig, notifyListeners);
  132. }
  133. }
  134. else if (old.is_object()) {
  135. // network delete
  136. const std::string ids = networkConfig["id"];
  137. const uint64_t networkId = Utils::hexStrToU64(ids.c_str());
  138. if (networkId) {
  139. eraseNetwork(networkId);
  140. }
  141. }
  142. if (notifyListeners) {
  143. // fprintf(stderr, "CV2::_networkChanged\n");
  144. DB::_networkChanged(old, networkConfig, false);
  145. }
  146. }
  147. bool CV2::save(nlohmann::json& record, bool notifyListeners)
  148. {
  149. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  150. auto tracer = provider->GetTracer("cv2");
  151. auto span = tracer->StartSpan("cv2::save");
  152. auto scope = tracer->WithActiveSpan(span);
  153. bool modified = false;
  154. try {
  155. if (! record.is_object()) {
  156. fprintf(stderr, "record is not an object?!?\n");
  157. return false;
  158. }
  159. const std::string objtype = record["objtype"];
  160. if (objtype == "network") {
  161. auto nspan = tracer->StartSpan("cv2::save::network");
  162. auto nscope = tracer->WithActiveSpan(nspan);
  163. // fprintf(stderr, "network save\n");
  164. const uint64_t nwid = OSUtils::jsonIntHex(record["id"], 0ULL);
  165. if (nwid) {
  166. nlohmann::json old;
  167. get(nwid, old);
  168. if ((! old.is_object()) || (! _compareRecords(old, record))) {
  169. record["revision"] = OSUtils::jsonInt(record["revision"], 0ULL) + 1ULL;
  170. _commitQueue.post(std::pair<nlohmann::json, bool>(record, notifyListeners));
  171. modified = true;
  172. }
  173. }
  174. }
  175. else if (objtype == "member") {
  176. auto mspan = tracer->StartSpan("cv2::save::member");
  177. auto mscope = tracer->WithActiveSpan(mspan);
  178. std::string networkId = record["nwid"];
  179. std::string memberId = record["id"];
  180. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"], 0ULL);
  181. const uint64_t id = OSUtils::jsonIntHex(record["id"], 0ULL);
  182. // fprintf(stderr, "member save %s-%s\n", networkId.c_str(), memberId.c_str());
  183. if ((id) && (nwid)) {
  184. nlohmann::json network, old;
  185. get(nwid, network, id, old);
  186. if ((! old.is_object()) || (! _compareRecords(old, record))) {
  187. // fprintf(stderr, "commit queue post\n");
  188. record["revision"] = OSUtils::jsonInt(record["revision"], 0ULL) + 1ULL;
  189. _commitQueue.post(std::pair<nlohmann::json, bool>(record, notifyListeners));
  190. modified = true;
  191. }
  192. else {
  193. // fprintf(stderr, "no change\n");
  194. }
  195. }
  196. }
  197. else {
  198. fprintf(stderr, "uhh waaat\n");
  199. }
  200. }
  201. catch (std::exception& e) {
  202. fprintf(stderr, "Error on PostgreSQL::save: %s\n", e.what());
  203. }
  204. catch (...) {
  205. fprintf(stderr, "Unknown error on PostgreSQL::save\n");
  206. }
  207. return modified;
  208. }
  209. void CV2::eraseNetwork(const uint64_t networkId)
  210. {
  211. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  212. auto tracer = provider->GetTracer("cv2");
  213. auto span = tracer->StartSpan("cv2::eraseNetwork");
  214. auto scope = tracer->WithActiveSpan(span);
  215. char networkIdStr[17];
  216. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  217. fprintf(stderr, "PostgreSQL::eraseNetwork\n");
  218. char tmp2[24];
  219. waitForReady();
  220. Utils::hex(networkId, tmp2);
  221. std::pair<nlohmann::json, bool> tmp;
  222. tmp.first["id"] = tmp2;
  223. tmp.first["objtype"] = "_delete_network";
  224. tmp.second = true;
  225. _commitQueue.post(tmp);
  226. nlohmann::json nullJson;
  227. _networkChanged(tmp.first, nullJson, true);
  228. }
  229. void CV2::eraseMember(const uint64_t networkId, const uint64_t memberId)
  230. {
  231. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  232. auto tracer = provider->GetTracer("cv2");
  233. auto span = tracer->StartSpan("cv2::eraseMember");
  234. auto scope = tracer->WithActiveSpan(span);
  235. char networkIdStr[17];
  236. char memberIdStr[11];
  237. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  238. span->SetAttribute("member_id", Utils::hex10(memberId, memberIdStr));
  239. fprintf(stderr, "PostgreSQL::eraseMember\n");
  240. char tmp2[24];
  241. waitForReady();
  242. std::pair<nlohmann::json, bool> tmp, nw;
  243. Utils::hex(networkId, tmp2);
  244. tmp.first["nwid"] = tmp2;
  245. Utils::hex(memberId, tmp2);
  246. tmp.first["id"] = tmp2;
  247. tmp.first["objtype"] = "_delete_member";
  248. tmp.second = true;
  249. _commitQueue.post(tmp);
  250. nlohmann::json nullJson;
  251. _memberChanged(tmp.first, nullJson, true);
  252. }
  253. void CV2::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress, const char* osArch)
  254. {
  255. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  256. auto tracer = provider->GetTracer("cv2");
  257. auto span = tracer->StartSpan("cv2::nodeIsOnline");
  258. auto scope = tracer->WithActiveSpan(span);
  259. char networkIdStr[17];
  260. char memberIdStr[11];
  261. char ipAddressStr[INET6_ADDRSTRLEN];
  262. span->SetAttribute("network_id", Utils::hex(networkId, networkIdStr));
  263. span->SetAttribute("member_id", Utils::hex10(memberId, memberIdStr));
  264. span->SetAttribute("physical_address", ipAddressStr);
  265. span->SetAttribute("os_arch", osArch);
  266. std::lock_guard<std::mutex> l(_lastOnline_l);
  267. NodeOnlineRecord& i = _lastOnline[std::pair<uint64_t, uint64_t>(networkId, memberId)];
  268. i.lastSeen = OSUtils::now();
  269. if (physicalAddress) {
  270. i.physicalAddress = physicalAddress;
  271. }
  272. i.osArch = std::string(osArch);
  273. }
  274. void CV2::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress& physicalAddress)
  275. {
  276. this->nodeIsOnline(networkId, memberId, physicalAddress, "unknown/unknown");
  277. }
  278. AuthInfo CV2::getSSOAuthInfo(const nlohmann::json& member, const std::string& redirectURL)
  279. {
  280. // TODO: Redo this for CV2
  281. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  282. auto tracer = provider->GetTracer("cv2");
  283. auto span = tracer->StartSpan("cv2::getSSOAuthInfo");
  284. auto scope = tracer->WithActiveSpan(span);
  285. Metrics::db_get_sso_info++;
  286. // NONCE is just a random character string. no semantic meaning
  287. // state = HMAC SHA384 of Nonce based on shared sso key
  288. //
  289. // need nonce timeout in database? make sure it's used within X time
  290. // X is 5 minutes for now. Make configurable later?
  291. //
  292. // how do we tell when a nonce is used? if auth_expiration_time is set
  293. std::string networkId = member["nwid"];
  294. std::string memberId = member["id"];
  295. char authenticationURL[4096] = { 0 };
  296. AuthInfo info;
  297. info.enabled = true;
  298. // if (memberId == "a10dccea52" && networkId == "8056c2e21c24673d") {
  299. // fprintf(stderr, "invalid authinfo for grant's machine\n");
  300. // info.version=1;
  301. // return info;
  302. // }
  303. // fprintf(stderr, "PostgreSQL::updateMemberOnLoad: %s-%s\n", networkId.c_str(), memberId.c_str());
  304. std::shared_ptr<PostgresConnection> c;
  305. try {
  306. // c = _pool->borrow();
  307. // pqxx::work w(*c->c);
  308. // char nonceBytes[16] = {0};
  309. // std::string nonce = "";
  310. // // check if the member exists first.
  311. // pqxx::row count = w.exec_params1("SELECT count(id) FROM ztc_member WHERE id = $1 AND network_id = $2 AND deleted = false", memberId, networkId);
  312. // if (count[0].as<int>() == 1) {
  313. // // get active nonce, if exists.
  314. // pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  315. // "WHERE network_id = $1 AND member_id = $2 "
  316. // "AND ((NOW() AT TIME ZONE 'UTC') <= authentication_expiry_time) AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  317. // networkId, memberId);
  318. // if (r.size() == 0) {
  319. // // no active nonce.
  320. // // find an unused nonce, if one exists.
  321. // pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  322. // "WHERE network_id = $1 AND member_id = $2 "
  323. // "AND authentication_expiry_time IS NULL AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  324. // networkId, memberId);
  325. // if (r.size() == 1) {
  326. // // we have an existing nonce. Use it
  327. // nonce = r.at(0)[0].as<std::string>();
  328. // Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  329. // } else if (r.empty()) {
  330. // // create a nonce
  331. // Utils::getSecureRandom(nonceBytes, 16);
  332. // char nonceBuf[64] = {0};
  333. // Utils::hex(nonceBytes, sizeof(nonceBytes), nonceBuf);
  334. // nonce = std::string(nonceBuf);
  335. // pqxx::result ir = w.exec_params0("INSERT INTO ztc_sso_expiry "
  336. // "(nonce, nonce_expiration, network_id, member_id) VALUES "
  337. // "($1, TO_TIMESTAMP($2::double precision/1000), $3, $4)",
  338. // nonce, OSUtils::now() + 300000, networkId, memberId);
  339. // w.commit();
  340. // } else {
  341. // // > 1 ?!? Thats an error!
  342. // fprintf(stderr, "> 1 unused nonce!\n");
  343. // exit(6);
  344. // }
  345. // } else if (r.size() == 1) {
  346. // nonce = r.at(0)[0].as<std::string>();
  347. // Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  348. // } else {
  349. // // more than 1 nonce in use? Uhhh...
  350. // fprintf(stderr, "> 1 nonce in use for network member?!?\n");
  351. // exit(7);
  352. // }
  353. // r = w.exec_params(
  354. // "SELECT oc.client_id, oc.authorization_endpoint, oc.issuer, oc.provider, oc.sso_impl_version "
  355. // "FROM ztc_network AS n "
  356. // "INNER JOIN ztc_org o "
  357. // " ON o.owner_id = n.owner_id "
  358. // "LEFT OUTER JOIN ztc_network_oidc_config noc "
  359. // " ON noc.network_id = n.id "
  360. // "LEFT OUTER JOIN ztc_oidc_config oc "
  361. // " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  362. // "WHERE n.id = $1 AND n.sso_enabled = true", networkId);
  363. // std::string client_id = "";
  364. // std::string authorization_endpoint = "";
  365. // std::string issuer = "";
  366. // std::string provider = "";
  367. // uint64_t sso_version = 0;
  368. // if (r.size() == 1) {
  369. // client_id = r.at(0)[0].as<std::optional<std::string>>().value_or("");
  370. // authorization_endpoint = r.at(0)[1].as<std::optional<std::string>>().value_or("");
  371. // issuer = r.at(0)[2].as<std::optional<std::string>>().value_or("");
  372. // provider = r.at(0)[3].as<std::optional<std::string>>().value_or("");
  373. // sso_version = r.at(0)[4].as<std::optional<uint64_t>>().value_or(1);
  374. // } else if (r.size() > 1) {
  375. // fprintf(stderr, "ERROR: More than one auth endpoint for an organization?!?!? NetworkID: %s\n", networkId.c_str());
  376. // } else {
  377. // fprintf(stderr, "No client or auth endpoint?!?\n");
  378. // }
  379. // info.version = sso_version;
  380. // // no catch all else because we don't actually care if no records exist here. just continue as normal.
  381. // if ((!client_id.empty())&&(!authorization_endpoint.empty())) {
  382. // uint8_t state[48];
  383. // HMACSHA384(_ssoPsk, nonceBytes, sizeof(nonceBytes), state);
  384. // char state_hex[256];
  385. // Utils::hex(state, 48, state_hex);
  386. // if (info.version == 0) {
  387. // char url[2048] = {0};
  388. // OSUtils::ztsnprintf(url, sizeof(authenticationURL),
  389. // "%s?response_type=id_token&response_mode=form_post&scope=openid+email+profile&redirect_uri=%s&nonce=%s&state=%s&client_id=%s",
  390. // authorization_endpoint.c_str(),
  391. // url_encode(redirectURL).c_str(),
  392. // nonce.c_str(),
  393. // state_hex,
  394. // client_id.c_str());
  395. // info.authenticationURL = std::string(url);
  396. // } else if (info.version == 1) {
  397. // info.ssoClientID = client_id;
  398. // info.issuerURL = issuer;
  399. // info.ssoProvider = provider;
  400. // info.ssoNonce = nonce;
  401. // info.ssoState = std::string(state_hex) + "_" +networkId;
  402. // info.centralAuthURL = redirectURL;
  403. // #ifdef ZT_DEBUG
  404. // fprintf(
  405. // stderr,
  406. // "ssoClientID: %s\nissuerURL: %s\nssoNonce: %s\nssoState: %s\ncentralAuthURL: %s\nprovider: %s\n",
  407. // info.ssoClientID.c_str(),
  408. // info.issuerURL.c_str(),
  409. // info.ssoNonce.c_str(),
  410. // info.ssoState.c_str(),
  411. // info.centralAuthURL.c_str(),
  412. // provider.c_str());
  413. // #endif
  414. // }
  415. // } else {
  416. // fprintf(stderr, "client_id: %s\nauthorization_endpoint: %s\n", client_id.c_str(), authorization_endpoint.c_str());
  417. // }
  418. // }
  419. // _pool->unborrow(c);
  420. }
  421. catch (std::exception& e) {
  422. fprintf(stderr, "ERROR: Error updating member on load for network %s: %s\n", networkId.c_str(), e.what());
  423. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  424. }
  425. return info; // std::string(authenticationURL);
  426. }
  427. void CV2::initializeNetworks()
  428. {
  429. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  430. auto tracer = provider->GetTracer("cv2");
  431. auto span = tracer->StartSpan("cv2::initializeNetworks");
  432. auto scope = tracer->WithActiveSpan(span);
  433. fprintf(stderr, "Initializing networks...\n");
  434. try {
  435. char qbuf[2048];
  436. sprintf(
  437. qbuf,
  438. "SELECT id, name, configuration , (EXTRACT(EPOCH FROM creation_time AT TIME ZONE 'UTC')*1000)::bigint, "
  439. "(EXTRACT(EPOCH FROM last_modified AT TIME ZONE 'UTC')*1000)::bigint, revision "
  440. "FROM networks_ctl WHERE controller_id = '%s'",
  441. _myAddressStr.c_str());
  442. auto c = _pool->borrow();
  443. pqxx::work w(*c->c);
  444. fprintf(stderr, "Load networks from psql...\n");
  445. auto stream = pqxx::stream_from::query(w, qbuf);
  446. std::tuple<
  447. std::string // network ID
  448. ,
  449. std::optional<std::string> // name
  450. ,
  451. std::string // configuration
  452. ,
  453. std::optional<uint64_t> // creation_time
  454. ,
  455. std::optional<uint64_t> // last_modified
  456. ,
  457. std::optional<uint64_t> // revision
  458. >
  459. row;
  460. uint64_t count = 0;
  461. uint64_t total = 0;
  462. while (stream >> row) {
  463. auto start = std::chrono::high_resolution_clock::now();
  464. json empty;
  465. json config;
  466. initNetwork(config);
  467. std::string nwid = std::get<0>(row);
  468. std::string name = std::get<1>(row).value_or("");
  469. json cfgtmp = json::parse(std::get<2>(row));
  470. std::optional<uint64_t> created_at = std::get<3>(row);
  471. std::optional<uint64_t> last_modified = std::get<4>(row);
  472. std::optional<uint64_t> revision = std::get<5>(row);
  473. config["id"] = nwid;
  474. config["name"] = name;
  475. config["creationTime"] = created_at.value_or(0);
  476. config["lastModified"] = last_modified.value_or(0);
  477. config["revision"] = revision.value_or(0);
  478. config["capabilities"] = cfgtmp["capabilities"].is_array() ? cfgtmp["capabilities"] : json::array();
  479. config["enableBroadcast"] = cfgtmp["enableBroadcast"].is_boolean() ? cfgtmp["enableBroadcast"].get<bool>() : false;
  480. config["mtu"] = cfgtmp["mtu"].is_number() ? cfgtmp["mtu"].get<int32_t>() : 2800;
  481. config["multicastLimit"] = cfgtmp["multicastLimit"].is_number() ? cfgtmp["multicastLimit"].get<int32_t>() : 64;
  482. config["private"] = cfgtmp["private"].is_boolean() ? cfgtmp["private"].get<bool>() : true;
  483. config["remoteTraceLevel"] = cfgtmp["remoteTraceLevel"].is_number() ? cfgtmp["remoteTraceLevel"].get<int32_t>() : 0;
  484. config["remoteTraceTarget"] = cfgtmp["remoteTraceTarget"].is_string() ? cfgtmp["remoteTraceTarget"].get<std::string>() : "";
  485. config["revision"] = revision.value_or(0);
  486. config["rules"] = cfgtmp["rules"].is_array() ? cfgtmp["rules"] : json::array();
  487. config["tags"] = cfgtmp["tags"].is_array() ? cfgtmp["tags"] : json::array();
  488. if (cfgtmp["v4AssignMode"].is_object()) {
  489. config["v4AssignMode"] = cfgtmp["v4AssignMode"];
  490. }
  491. else {
  492. config["v4AssignMode"] = json::object();
  493. config["v4AssignMode"]["zt"] = true;
  494. }
  495. if (cfgtmp["v6AssignMode"].is_object()) {
  496. config["v6AssignMode"] = cfgtmp["v6AssignMode"];
  497. }
  498. else {
  499. config["v6AssignMode"] = json::object();
  500. config["v6AssignMode"]["zt"] = true;
  501. config["v6AssignMode"]["6plane"] = true;
  502. config["v6AssignMode"]["rfc4193"] = false;
  503. }
  504. config["ssoEnabled"] = cfgtmp["ssoEnabled"].is_boolean() ? cfgtmp["ssoEnabled"].get<bool>() : false;
  505. config["objtype"] = "network";
  506. config["routes"] = cfgtmp["routes"].is_array() ? cfgtmp["routes"] : json::array();
  507. config["clientId"] = cfgtmp["clientId"].is_string() ? cfgtmp["clientId"].get<std::string>() : "";
  508. config["authorizationEndpoint"] = cfgtmp["authorizationEndpoint"].is_string() ? cfgtmp["authorizationEndpoint"].get<std::string>() : nullptr;
  509. config["provider"] = cfgtmp["ssoProvider"].is_string() ? cfgtmp["ssoProvider"].get<std::string>() : "";
  510. if (! cfgtmp["dns"].is_object()) {
  511. cfgtmp["dns"] = json::object();
  512. cfgtmp["dns"]["domain"] = "";
  513. cfgtmp["dns"]["servers"] = json::array();
  514. }
  515. else {
  516. config["dns"] = cfgtmp["dns"];
  517. }
  518. config["ipAssignmentPools"] = cfgtmp["ipAssignmentPools"].is_array() ? cfgtmp["ipAssignmentPools"] : json::array();
  519. Metrics::network_count++;
  520. _networkChanged(empty, config, false);
  521. auto end = std::chrono::high_resolution_clock::now();
  522. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  523. ;
  524. total += dur.count();
  525. ++count;
  526. if (count > 0 && count % 10000 == 0) {
  527. fprintf(stderr, "Averaging %lu us per network\n", (total / count));
  528. }
  529. }
  530. w.commit();
  531. _pool->unborrow(c);
  532. fprintf(stderr, "done.\n");
  533. if (++this->_ready == 2) {
  534. if (_waitNoticePrinted) {
  535. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S, _timestr(), (unsigned long long)_myAddress.toInt());
  536. }
  537. _readyLock.unlock();
  538. }
  539. fprintf(stderr, "network init done\n");
  540. }
  541. catch (std::exception& e) {
  542. fprintf(stderr, "ERROR: Error initializing networks: %s\n", e.what());
  543. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  544. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  545. exit(-1);
  546. }
  547. }
  548. void CV2::initializeMembers()
  549. {
  550. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  551. auto tracer = provider->GetTracer("cv2");
  552. auto span = tracer->StartSpan("cv2::initializeMembers");
  553. auto scope = tracer->WithActiveSpan(span);
  554. std::string memberId;
  555. std::string networkId;
  556. try {
  557. char qbuf[2048];
  558. sprintf(
  559. qbuf,
  560. "SELECT nm.device_id, nm.network_id, nm.authorized, nm.active_bridge, nm.ip_assignments, nm.no_auto_assign_ips, "
  561. "nm.sso_exempt, (EXTRACT(EPOCH FROM nm.authentication_expiry_time AT TIME ZONE 'UTC')*1000)::bigint, "
  562. "(EXTRACT(EPOCH FROM nm.creation_time AT TIME ZONE 'UTC')*1000)::bigint, nm.identity, "
  563. "(EXTRACT(EPOCH FROM nm.last_authorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  564. "(EXTRACT(EPOCH FROM nm.last_deauthorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  565. "nm.remote_trace_level, nm.remote_trace_target, nm.revision, nm.capabilities, nm.tags "
  566. "FROM network_memberships_ctl nm "
  567. "INNER JOIN networks_ctl n "
  568. " ON nm.network_id = n.id "
  569. "WHERE n.controller_id = '%s'",
  570. _myAddressStr.c_str());
  571. auto c = _pool->borrow();
  572. pqxx::work w(*c->c);
  573. fprintf(stderr, "Load members from psql...\n");
  574. auto stream = pqxx::stream_from::query(w, qbuf);
  575. std::tuple<
  576. std::string // device ID
  577. ,
  578. std::string // network ID
  579. ,
  580. bool // authorized
  581. ,
  582. std::optional<bool> // active_bridge
  583. ,
  584. std::optional<std::string> // ip_assignments
  585. ,
  586. std::optional<bool> // no_auto_assign_ips
  587. ,
  588. std::optional<bool> // sso_exempt
  589. ,
  590. std::optional<uint64_t> // authentication_expiry_time
  591. ,
  592. std::optional<uint64_t> // creation_time
  593. ,
  594. std::optional<std::string> // identity
  595. ,
  596. std::optional<uint64_t> // last_authorized_time
  597. ,
  598. std::optional<uint64_t> // last_deauthorized_time
  599. ,
  600. std::optional<int32_t> // remote_trace_level
  601. ,
  602. std::optional<std::string> // remote_trace_target
  603. ,
  604. std::optional<uint64_t> // revision
  605. ,
  606. std::optional<std::string> // capabilities
  607. ,
  608. std::optional<std::string> // tags
  609. >
  610. row;
  611. uint64_t count = 0;
  612. uint64_t total = 0;
  613. while (stream >> row) {
  614. auto start = std::chrono::high_resolution_clock::now();
  615. json empty;
  616. json config;
  617. initMember(config);
  618. memberId = std::get<0>(row);
  619. networkId = std::get<1>(row);
  620. bool authorized = std::get<2>(row);
  621. std::optional<bool> active_bridge = std::get<3>(row);
  622. std::string ip_assignments = std::get<4>(row).value_or("");
  623. std::optional<bool> no_auto_assign_ips = std::get<5>(row);
  624. std::optional<bool> sso_exempt = std::get<6>(row);
  625. std::optional<uint64_t> authentication_expiry_time = std::get<7>(row);
  626. std::optional<uint64_t> creation_time = std::get<8>(row);
  627. std::optional<std::string> identity = std::get<9>(row);
  628. std::optional<uint64_t> last_authorized_time = std::get<10>(row);
  629. std::optional<uint64_t> last_deauthorized_time = std::get<11>(row);
  630. std::optional<int32_t> remote_trace_level = std::get<12>(row);
  631. std::optional<std::string> remote_trace_target = std::get<13>(row);
  632. std::optional<uint64_t> revision = std::get<14>(row);
  633. std::optional<std::string> capabilities = std::get<15>(row);
  634. std::optional<std::string> tags = std::get<16>(row);
  635. config["objtype"] = "member";
  636. config["id"] = memberId;
  637. config["address"] = identity.value_or("");
  638. config["nwid"] = networkId;
  639. config["authorized"] = authorized;
  640. config["activeBridge"] = active_bridge.value_or(false);
  641. config["ipAssignments"] = json::array();
  642. if (ip_assignments != "{}") {
  643. std::string tmp = ip_assignments.substr(1, ip_assignments.length() - 2);
  644. std::vector<std::string> addrs = split(tmp, ',');
  645. for (auto it = addrs.begin(); it != addrs.end(); ++it) {
  646. config["ipAssignments"].push_back(*it);
  647. }
  648. }
  649. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  650. config["creationTime"] = creation_time.value_or(0);
  651. config["lastAuthorizedTime"] = last_authorized_time.value_or(0);
  652. config["lastDeauthorizedTime"] = last_deauthorized_time.value_or(0);
  653. config["noAutoAssignIPs"] = no_auto_assign_ips.value_or(false);
  654. config["remoteTraceLevel"] = remote_trace_level.value_or(0);
  655. config["remoteTraceTarget"] = remote_trace_target.value_or(nullptr);
  656. config["revision"] = revision.value_or(0);
  657. config["ssoExempt"] = sso_exempt.value_or(false);
  658. config["authenticationExpiryTime"] = authentication_expiry_time.value_or(0);
  659. config["tags"] = json::parse(tags.value_or("[]"));
  660. Metrics::member_count++;
  661. _memberChanged(empty, config, false);
  662. memberId = "";
  663. networkId = "";
  664. auto end = std::chrono::high_resolution_clock::now();
  665. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  666. total += dur.count();
  667. ++count;
  668. if (count > 0 && count % 10000 == 0) {
  669. fprintf(stderr, "Averaging %lu us per member\n", (total / count));
  670. }
  671. }
  672. if (count > 0) {
  673. fprintf(stderr, "Took %lu us per member to load\n", (total / count));
  674. }
  675. stream.complete();
  676. w.commit();
  677. _pool->unborrow(c);
  678. fprintf(stderr, "done.\n");
  679. if (++this->_ready == 2) {
  680. if (_waitNoticePrinted) {
  681. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S, _timestr(), (unsigned long long)_myAddress.toInt());
  682. }
  683. _readyLock.unlock();
  684. }
  685. fprintf(stderr, "member init done\n");
  686. }
  687. catch (std::exception& e) {
  688. fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what());
  689. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  690. exit(-1);
  691. }
  692. }
  693. void CV2::heartbeat()
  694. {
  695. char publicId[1024];
  696. char hostnameTmp[1024];
  697. _myId.toString(false, publicId);
  698. if (gethostname(hostnameTmp, sizeof(hostnameTmp)) != 0) {
  699. hostnameTmp[0] = (char)0;
  700. }
  701. else {
  702. for (int i = 0; i < (int)sizeof(hostnameTmp); ++i) {
  703. if ((hostnameTmp[i] == '.') || (hostnameTmp[i] == 0)) {
  704. hostnameTmp[i] = (char)0;
  705. break;
  706. }
  707. }
  708. }
  709. const char* controllerId = _myAddressStr.c_str();
  710. const char* publicIdentity = publicId;
  711. const char* hostname = hostnameTmp;
  712. while (_run == 1) {
  713. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  714. auto tracer = provider->GetTracer("cv2");
  715. auto span = tracer->StartSpan("cv2::heartbeat");
  716. auto scope = tracer->WithActiveSpan(span);
  717. auto c = _pool->borrow();
  718. int64_t ts = OSUtils::now();
  719. if (c->c) {
  720. std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
  721. std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
  722. std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
  723. std::string version = major + "." + minor + "." + rev;
  724. std::string versionStr = "v" + version;
  725. try {
  726. pqxx::work w { *c->c };
  727. w.exec_params0(
  728. "INSERT INTO controllers_ctl (id, hostname, last_heartbeat, public_identity, version) VALUES "
  729. "($1, $2, TO_TIMESTAMP($3::double precision/1000), $4, $5) "
  730. "ON CONFLICT (id) DO UPDATE SET hostname = EXCLUDED.hostname, last_heartbeat = EXCLUDED.last_heartbeat, "
  731. "public_identity = EXCLUDED.public_identity, version = EXCLUDED.version",
  732. controllerId,
  733. hostname,
  734. ts,
  735. publicIdentity,
  736. versionStr);
  737. w.commit();
  738. }
  739. catch (std::exception& e) {
  740. fprintf(stderr, "ERROR: Error in heartbeat: %s\n", e.what());
  741. span->SetStatus(opentelemetry::trace::StatusCode::kError, e.what());
  742. continue;
  743. }
  744. catch (...) {
  745. fprintf(stderr, "ERROR: Unknown error in heartbeat\n");
  746. span->SetStatus(opentelemetry::trace::StatusCode::kError, "Unknown error in heartbeat");
  747. continue;
  748. }
  749. }
  750. _pool->unborrow(c);
  751. span->End();
  752. std::this_thread::sleep_for(std::chrono::seconds(1));
  753. }
  754. fprintf(stderr, "Exited heartbeat thread\n");
  755. }
  756. void CV2::membersDbWatcher()
  757. {
  758. auto c = _pool->borrow();
  759. std::string stream = "member_" + _myAddressStr;
  760. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  761. MemberNotificationReceiver<CV2> m(this, *c->c, stream);
  762. while (_run == 1) {
  763. c->c->await_notification(5, 0);
  764. }
  765. _pool->unborrow(c);
  766. fprintf(stderr, "Exited membersDbWatcher\n");
  767. }
  768. void CV2::networksDbWatcher()
  769. {
  770. std::string stream = "network_" + _myAddressStr;
  771. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  772. auto c = _pool->borrow();
  773. NetworkNotificationReceiver<CV2> n(this, *c->c, stream);
  774. while (_run == 1) {
  775. c->c->await_notification(5, 0);
  776. }
  777. _pool->unborrow(c);
  778. fprintf(stderr, "Exited networksDbWatcher\n");
  779. }
  780. void CV2::commitThread()
  781. {
  782. fprintf(stderr, "%s: commitThread start\n", _myAddressStr.c_str());
  783. std::pair<nlohmann::json, bool> qitem;
  784. while (_commitQueue.get(qitem) && (_run == 1)) {
  785. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  786. auto tracer = provider->GetTracer("cv2");
  787. auto span = tracer->StartSpan("cv2::commitThread");
  788. auto scope = tracer->WithActiveSpan(span);
  789. // fprintf(stderr, "commitThread tick\n");
  790. if (! qitem.first.is_object()) {
  791. fprintf(stderr, "not an object\n");
  792. continue;
  793. }
  794. std::shared_ptr<PostgresConnection> c;
  795. try {
  796. c = _pool->borrow();
  797. }
  798. catch (std::exception& e) {
  799. fprintf(stderr, "ERROR: %s\n", e.what());
  800. continue;
  801. }
  802. if (! c) {
  803. fprintf(stderr, "Error getting database connection\n");
  804. continue;
  805. }
  806. Metrics::pgsql_commit_ticks++;
  807. try {
  808. nlohmann::json& config = (qitem.first);
  809. const std::string objtype = config["objtype"];
  810. if (objtype == "member") {
  811. auto mspan = tracer->StartSpan("cv2::commitThread::member");
  812. auto mscope = tracer->WithActiveSpan(span);
  813. // fprintf(stderr, "%s: commitThread: member\n", _myAddressStr.c_str());
  814. std::string memberId;
  815. std::string networkId;
  816. try {
  817. pqxx::work w(*c->c);
  818. memberId = config["id"];
  819. networkId = config["nwid"];
  820. std::string target = "NULL";
  821. if (! config["remoteTraceTarget"].is_null()) {
  822. target = config["remoteTraceTarget"];
  823. }
  824. pqxx::row nwrow = w.exec_params1("SELECT COUNT(id) FROM networks WHERE id = $1", networkId);
  825. int nwcount = nwrow[0].as<int>();
  826. if (nwcount != 1) {
  827. fprintf(stderr, "network %s does not exist. skipping member upsert\n", networkId.c_str());
  828. w.abort();
  829. _pool->unborrow(c);
  830. continue;
  831. }
  832. // only needed for hooks, and no hooks for now
  833. // pqxx::row mrow = w.exec_params1("SELECT COUNT(id) FROM device_networks WHERE device_id = $1 AND network_id = $2", memberId, networkId);
  834. // int membercount = mrow[0].as<int>();
  835. // bool isNewMember = (membercount == 0);
  836. pqxx::result res = w.exec_params0(
  837. "INSERT INTO network_memberships_ctl (device_id, network_id, authorized, active_bridge, ip_assignments, "
  838. "no_auto_assign_ips, sso_exempt, authentication_expiry_time, capabilities, creation_time, "
  839. "identity, last_authorized_time, last_deauthorized_time, "
  840. "remote_trace_level, remote_trace_target, revision, tags, version_major, version_minor, "
  841. "version_revision, version_protocol) "
  842. "VALUES ($1, $2, $3, $4, $5, $6, $7, TO_TIMESTAMP($8::double precision/1000), $9, "
  843. "TO_TIMESTAMP($10::double precision/1000), $11, TO_TIMESTAMP($12::double precision/1000), "
  844. "TO_TIMESTAMP($13::double precision/1000), $14, $15, $16, $17, $18, $19, $20, $21) "
  845. "ON CONFLICT (device_id, network_id) DO UPDATE SET "
  846. "authorized = EXCLUDED.authorized, active_bridge = EXCLUDED.active_bridge, "
  847. "ip_assignments = EXCLUDED.ip_assignments, no_auto_assign_ips = EXCLUDED.no_auto_assign_ips, "
  848. "sso_exempt = EXCLUDED.sso_exempt, authentication_expiry_time = EXCLUDED.authentication_expiry_time, "
  849. "capabilities = EXCLUDED.capabilities, creation_time = EXCLUDED.creation_time, "
  850. "identity = EXCLUDED.identity, last_authorized_time = EXCLUDED.last_authorized_time, "
  851. "last_deauthorized_time = EXCLUDED.last_deauthorized_time, "
  852. "remote_trace_level = EXCLUDED.remote_trace_level, remote_trace_target = EXCLUDED.remote_trace_target, "
  853. "revision = EXCLUDED.revision, tags = EXCLUDED.tags, version_major = EXCLUDED.version_major, "
  854. "version_minor = EXCLUDED.version_minor, version_revision = EXCLUDED.version_revision, "
  855. "version_protocol = EXCLUDED.version_protocol",
  856. memberId,
  857. networkId,
  858. (bool)config["authorized"],
  859. (bool)config["activeBridge"],
  860. config["ipAssignments"].get<std::vector<std::string> >(),
  861. (bool)config["noAutoAssignIps"],
  862. (bool)config["ssoExempt"],
  863. (uint64_t)config["authenticationExpiryTime"],
  864. OSUtils::jsonDump(config["capabilities"], -1),
  865. (uint64_t)config["creationTime"],
  866. OSUtils::jsonString(config["identity"], ""),
  867. (uint64_t)config["lastAuthorizedTime"],
  868. (uint64_t)config["lastDeauthorizedTime"],
  869. (int)config["remoteTraceLevel"],
  870. target,
  871. (uint64_t)config["revision"],
  872. OSUtils::jsonDump(config["tags"], -1),
  873. (int)config["vMajor"],
  874. (int)config["vMinor"],
  875. (int)config["vRev"],
  876. (int)config["vProto"]);
  877. w.commit();
  878. // No hooks for now
  879. // if (_smee != NULL && isNewMember) {
  880. // pqxx::row row = w.exec_params1(
  881. // "SELECT "
  882. // " count(h.hook_id) "
  883. // "FROM "
  884. // " ztc_hook h "
  885. // " INNER JOIN ztc_org o ON o.org_id = h.org_id "
  886. // " INNER JOIN ztc_network n ON n.owner_id = o.owner_id "
  887. // " WHERE "
  888. // "n.id = $1 ",
  889. // networkId
  890. // );
  891. // int64_t hookCount = row[0].as<int64_t>();
  892. // if (hookCount > 0) {
  893. // notifyNewMember(networkId, memberId);
  894. // }
  895. // }
  896. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  897. const uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  898. if (nwidInt && memberidInt) {
  899. nlohmann::json nwOrig;
  900. nlohmann::json memOrig;
  901. nlohmann::json memNew(config);
  902. get(nwidInt, nwOrig, memberidInt, memOrig);
  903. _memberChanged(memOrig, memNew, qitem.second);
  904. }
  905. else {
  906. fprintf(stderr, "%s: Can't notify of change. Error parsing nwid or memberid: %llu-%llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt, (unsigned long long)memberidInt);
  907. }
  908. }
  909. catch (pqxx::data_exception& e) {
  910. std::string cfgDump = OSUtils::jsonDump(config, 2);
  911. fprintf(stderr, "Member save %s-%s: %s\n", networkId.c_str(), memberId.c_str(), cfgDump.c_str());
  912. const pqxx::sql_error* s = dynamic_cast<const pqxx::sql_error*>(&e);
  913. fprintf(stderr, "%s ERROR: Error updating member: %s\n", _myAddressStr.c_str(), e.what());
  914. if (s) {
  915. fprintf(stderr, "%s ERROR: SQL error: %s\n", _myAddressStr.c_str(), s->query().c_str());
  916. }
  917. mspan->SetStatus(opentelemetry::trace::StatusCode::kError, "pqxx::data_exception");
  918. mspan->SetAttribute("error", e.what());
  919. mspan->SetAttribute("config", cfgDump);
  920. }
  921. catch (std::exception& e) {
  922. std::string cfgDump = OSUtils::jsonDump(config, 2);
  923. fprintf(stderr, "%s ERROR: Error updating member %s-%s: %s\njsonDump: %s\n", _myAddressStr.c_str(), networkId.c_str(), memberId.c_str(), e.what(), cfgDump.c_str());
  924. mspan->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  925. mspan->SetAttribute("error", e.what());
  926. mspan->SetAttribute("config", cfgDump);
  927. }
  928. }
  929. else if (objtype == "network") {
  930. auto nspan = tracer->StartSpan("cv2::commitThread::network");
  931. auto nscope = tracer->WithActiveSpan(span);
  932. try {
  933. // fprintf(stderr, "%s: commitThread: network\n", _myAddressStr.c_str());
  934. pqxx::work w(*c->c);
  935. std::string id = config["id"];
  936. // network must already exist
  937. pqxx::result res = w.exec_params0(
  938. "INSERT INTO networks_ctl (id, name, configuration, controller_id, revision) "
  939. "VALUES ($1, $2, $3, $4, $5) "
  940. "ON CONFLICT (id) DO UPDATE SET "
  941. "name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1",
  942. id,
  943. OSUtils::jsonString(config["name"], ""),
  944. OSUtils::jsonDump(config, -1),
  945. _myAddressStr,
  946. ((uint64_t)config["revision"]));
  947. w.commit();
  948. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  949. if (nwidInt) {
  950. nlohmann::json nwOrig;
  951. nlohmann::json nwNew(config);
  952. get(nwidInt, nwOrig);
  953. _networkChanged(nwOrig, nwNew, qitem.second);
  954. }
  955. else {
  956. fprintf(stderr, "%s: Can't notify network changed: %llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt);
  957. }
  958. }
  959. catch (pqxx::data_exception& e) {
  960. const pqxx::sql_error* s = dynamic_cast<const pqxx::sql_error*>(&e);
  961. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  962. if (s) {
  963. fprintf(stderr, "%s ERROR: SQL error: %s\n", _myAddressStr.c_str(), s->query().c_str());
  964. }
  965. nspan->SetStatus(opentelemetry::trace::StatusCode::kError, "pqxx::data_exception");
  966. nspan->SetAttribute("error", e.what());
  967. nspan->SetAttribute("config", OSUtils::jsonDump(config, 2));
  968. }
  969. catch (std::exception& e) {
  970. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  971. nspan->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  972. nspan->SetAttribute("error", e.what());
  973. nspan->SetAttribute("config", OSUtils::jsonDump(config, 2));
  974. }
  975. }
  976. else if (objtype == "_delete_network") {
  977. auto dspan = tracer->StartSpan("cv2::commitThread::delete_network");
  978. auto dscope = tracer->WithActiveSpan(span);
  979. // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
  980. try {
  981. pqxx::work w(*c->c);
  982. std::string networkId = config["id"];
  983. w.exec_params0("DELETE FROM network_memberships_ctl WHERE network_id = $1", networkId);
  984. w.exec_params0("DELETE FROM networks_ctl WHERE id = $1", networkId);
  985. w.commit();
  986. }
  987. catch (std::exception& e) {
  988. fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
  989. dspan->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  990. dspan->SetAttribute("error", e.what());
  991. dspan->SetAttribute("config", OSUtils::jsonDump(config, 2));
  992. }
  993. }
  994. else if (objtype == "_delete_member") {
  995. auto dspan = tracer->StartSpan("cv2::commitThread::delete_member");
  996. auto dscope = tracer->WithActiveSpan(span);
  997. // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
  998. try {
  999. pqxx::work w(*c->c);
  1000. std::string memberId = config["id"];
  1001. std::string networkId = config["nwid"];
  1002. pqxx::result res = w.exec_params0("DELETE FROM network_memberships_ctl WHERE device_id = $1 AND network_id = $2", memberId, networkId);
  1003. w.commit();
  1004. }
  1005. catch (std::exception& e) {
  1006. fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());
  1007. dspan->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  1008. dspan->SetAttribute("error", e.what());
  1009. dspan->SetAttribute("config", OSUtils::jsonDump(config, 2));
  1010. }
  1011. }
  1012. else {
  1013. fprintf(stderr, "%s ERROR: unknown objtype\n", _myAddressStr.c_str());
  1014. }
  1015. }
  1016. catch (std::exception& e) {
  1017. fprintf(stderr, "%s ERROR: Error getting objtype: %s\n", _myAddressStr.c_str(), e.what());
  1018. span->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  1019. span->SetAttribute("error", e.what());
  1020. }
  1021. _pool->unborrow(c);
  1022. c.reset();
  1023. }
  1024. fprintf(stderr, "%s commitThread finished\n", _myAddressStr.c_str());
  1025. }
  1026. void CV2::onlineNotificationThread()
  1027. {
  1028. waitForReady();
  1029. _connected = 1;
  1030. nlohmann::json jtmp1, jtmp2;
  1031. while (_run == 1) {
  1032. auto provider = opentelemetry::trace::Provider::GetTracerProvider();
  1033. auto tracer = provider->GetTracer("cv2");
  1034. auto span = tracer->StartSpan("cv2::onlineNotificationThread");
  1035. auto scope = tracer->WithActiveSpan(span);
  1036. auto c = _pool->borrow();
  1037. auto c2 = _pool->borrow();
  1038. try {
  1039. fprintf(stderr, "%s onlineNotificationThread\n", _myAddressStr.c_str());
  1040. std::unordered_map<std::pair<uint64_t, uint64_t>, NodeOnlineRecord, _PairHasher> lastOnline;
  1041. {
  1042. std::lock_guard<std::mutex> l(_lastOnline_l);
  1043. lastOnline.swap(_lastOnline);
  1044. }
  1045. pqxx::work w(*c->c);
  1046. pqxx::work w2(*c2->c);
  1047. bool firstRun = true;
  1048. bool memberAdded = false;
  1049. uint64_t updateCount = 0;
  1050. pqxx::pipeline pipe(w);
  1051. for (auto i = lastOnline.begin(); i != lastOnline.end(); ++i) {
  1052. updateCount++;
  1053. uint64_t nwid_i = i->first.first;
  1054. char nwidTmp[64];
  1055. char memTmp[64];
  1056. char ipTmp[64];
  1057. OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
  1058. OSUtils::ztsnprintf(memTmp, sizeof(memTmp), "%.10llx", i->first.second);
  1059. if (! get(nwid_i, jtmp1, i->first.second, jtmp2)) {
  1060. continue; // skip non existent networks/members
  1061. }
  1062. std::string networkId(nwidTmp);
  1063. std::string memberId(memTmp);
  1064. try {
  1065. pqxx::row r = w2.exec_params1("SELECT device_id, network_id FROM network_memberships_ctl WHERE network_id = $1 AND device_id = $2", networkId, memberId);
  1066. }
  1067. catch (pqxx::unexpected_rows& e) {
  1068. continue;
  1069. }
  1070. int64_t ts = i->second.lastSeen;
  1071. std::string ipAddr = i->second.physicalAddress.toIpString(ipTmp);
  1072. std::string timestamp = std::to_string(ts);
  1073. std::string osArch = i->second.osArch;
  1074. std::vector<std::string> osArchSplit = split(osArch, '/');
  1075. std::string os = osArchSplit[0];
  1076. std::string arch = osArchSplit[1];
  1077. if (ipAddr.empty()) {
  1078. ipAddr = "relayed";
  1079. }
  1080. json record = {
  1081. { ipAddr, ts },
  1082. };
  1083. std::string device_network_insert = "INSERT INTO network_memberships_ctl (device_id, network_id, last_seen, os, arch) "
  1084. "VALUES ('"
  1085. + w2.esc(memberId) + "', '" + w2.esc(networkId) + "', '" + w2.esc(record.dump())
  1086. + "'::JSONB, "
  1087. "'"
  1088. + w2.esc(os) + "', '" + w2.esc(arch)
  1089. + "') "
  1090. "ON CONFLICT (device_id, network_id) DO UPDATE SET os = EXCLUDED.os, arch = EXCLUDED.arch, "
  1091. "last_seen = network_memberships_ctl.last_seen || EXCLUDED.last_seen";
  1092. pipe.insert(device_network_insert);
  1093. Metrics::pgsql_node_checkin++;
  1094. }
  1095. pipe.complete();
  1096. ;
  1097. w2.commit();
  1098. w.commit();
  1099. fprintf(stderr, "%s: Updated online status of %lu members\n", _myAddressStr.c_str(), updateCount);
  1100. }
  1101. catch (std::exception& e) {
  1102. fprintf(stderr, "%s ERROR: Error in onlineNotificationThread: %s\n", _myAddressStr.c_str(), e.what());
  1103. span->SetStatus(opentelemetry::trace::StatusCode::kError, "std::exception");
  1104. span->SetAttribute("error", e.what());
  1105. }
  1106. catch (...) {
  1107. fprintf(stderr, "%s ERROR: Unknown error in onlineNotificationThread\n", _myAddressStr.c_str());
  1108. span->SetStatus(opentelemetry::trace::StatusCode::kError, "unknown");
  1109. }
  1110. _pool->unborrow(c2);
  1111. _pool->unborrow(c);
  1112. span->End();
  1113. std::this_thread::sleep_for(std::chrono::seconds(10));
  1114. }
  1115. fprintf(stderr, "%s: Fell out of run loop in onlineNotificationThread\n", _myAddressStr.c_str());
  1116. if (_run == 1) {
  1117. fprintf(stderr, "ERROR: %s onlineNotificationThread should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  1118. exit(6);
  1119. }
  1120. }
  1121. #endif // ZT_CONTROLLER_USE_LIBPQ