CV2.cpp 44 KB

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