CV2.cpp 43 KB

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