2
0

CV1.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. /*
  2. * Copyright (c)2019 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 "CV1.hpp"
  14. #ifdef ZT_CONTROLLER_USE_LIBPQ
  15. #include "../node/Constants.hpp"
  16. #include "../node/SHA512.hpp"
  17. #include "EmbeddedNetworkController.hpp"
  18. #include "../version.h"
  19. #include "Redis.hpp"
  20. #include "CtlUtil.hpp"
  21. #include <smeeclient.h>
  22. #include <libpq-fe.h>
  23. #include <sstream>
  24. #include <iomanip>
  25. #include <climits>
  26. #include <chrono>
  27. // #define REDIS_TRACE 1
  28. using json = nlohmann::json;
  29. namespace {
  30. static const int DB_MINIMUM_VERSION = 38;
  31. } // anonymous namespace
  32. using namespace ZeroTier;
  33. using Attrs = std::vector<std::pair<std::string, std::string>>;
  34. using Item = std::pair<std::string, Attrs>;
  35. using ItemStream = std::vector<Item>;
  36. CV1::CV1(const Identity &myId, const char *path, int listenPort, RedisConfig *rc)
  37. : DB()
  38. , _pool()
  39. , _myId(myId)
  40. , _myAddress(myId.address())
  41. , _ready(0)
  42. , _connected(1)
  43. , _run(1)
  44. , _waitNoticePrinted(false)
  45. , _listenPort(listenPort)
  46. , _rc(rc)
  47. , _redis(NULL)
  48. , _cluster(NULL)
  49. , _redisMemberStatus(false)
  50. , _smee(NULL)
  51. {
  52. char myAddress[64];
  53. _myAddressStr = myId.address().toString(myAddress);
  54. _connString = std::string(path);
  55. auto f = std::make_shared<PostgresConnFactory>(_connString);
  56. _pool = std::make_shared<ConnectionPool<PostgresConnection> >(
  57. 15, 5, std::static_pointer_cast<ConnectionFactory>(f));
  58. memset(_ssoPsk, 0, sizeof(_ssoPsk));
  59. char *const ssoPskHex = getenv("ZT_SSO_PSK");
  60. #ifdef ZT_TRACE
  61. fprintf(stderr, "ZT_SSO_PSK: %s\n", ssoPskHex);
  62. #endif
  63. if (ssoPskHex) {
  64. // SECURITY: note that ssoPskHex will always be null-terminated if libc actually
  65. // returns something non-NULL. If the hex encodes something shorter than 48 bytes,
  66. // it will be padded at the end with zeroes. If longer, it'll be truncated.
  67. Utils::unhex(ssoPskHex, _ssoPsk, sizeof(_ssoPsk));
  68. }
  69. const char *redisMemberStatus = getenv("ZT_REDIS_MEMBER_STATUS");
  70. if (redisMemberStatus && (strcmp(redisMemberStatus, "true") == 0)) {
  71. _redisMemberStatus = true;
  72. fprintf(stderr, "Using redis for member status\n");
  73. }
  74. auto c = _pool->borrow();
  75. pqxx::work txn{*c->c};
  76. pqxx::row r{txn.exec1("SELECT version FROM ztc_database")};
  77. int dbVersion = r[0].as<int>();
  78. txn.commit();
  79. if (dbVersion < DB_MINIMUM_VERSION) {
  80. fprintf(stderr, "Central database schema version too low. This controller version requires a minimum schema version of %d. Please upgrade your Central instance", DB_MINIMUM_VERSION);
  81. exit(1);
  82. }
  83. _pool->unborrow(c);
  84. if (_rc != NULL) {
  85. sw::redis::ConnectionOptions opts;
  86. sw::redis::ConnectionPoolOptions poolOpts;
  87. opts.host = _rc->hostname;
  88. opts.port = _rc->port;
  89. opts.password = _rc->password;
  90. opts.db = 0;
  91. opts.keep_alive = true;
  92. opts.connect_timeout = std::chrono::seconds(3);
  93. poolOpts.size = 25;
  94. poolOpts.wait_timeout = std::chrono::seconds(5);
  95. poolOpts.connection_lifetime = std::chrono::minutes(3);
  96. poolOpts.connection_idle_time = std::chrono::minutes(1);
  97. if (_rc->clusterMode) {
  98. fprintf(stderr, "Using Redis in Cluster Mode\n");
  99. _cluster = std::make_shared<sw::redis::RedisCluster>(opts, poolOpts);
  100. } else {
  101. fprintf(stderr, "Using Redis in Standalone Mode\n");
  102. _redis = std::make_shared<sw::redis::Redis>(opts, poolOpts);
  103. }
  104. }
  105. _readyLock.lock();
  106. fprintf(stderr, "[%s] NOTICE: %.10llx controller PostgreSQL waiting for initial data download..." ZT_EOL_S, ::_timestr(), (unsigned long long)_myAddress.toInt());
  107. _waitNoticePrinted = true;
  108. initializeNetworks();
  109. initializeMembers();
  110. _heartbeatThread = std::thread(&CV1::heartbeat, this);
  111. _membersDbWatcher = std::thread(&CV1::membersDbWatcher, this);
  112. _networksDbWatcher = std::thread(&CV1::networksDbWatcher, this);
  113. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  114. _commitThread[i] = std::thread(&CV1::commitThread, this);
  115. }
  116. _onlineNotificationThread = std::thread(&CV1::onlineNotificationThread, this);
  117. configureSmee();
  118. }
  119. CV1::~CV1()
  120. {
  121. if (_smee != NULL) {
  122. smeeclient::smee_client_delete(_smee);
  123. _smee = NULL;
  124. }
  125. _run = 0;
  126. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  127. _heartbeatThread.join();
  128. _membersDbWatcher.join();
  129. _networksDbWatcher.join();
  130. _commitQueue.stop();
  131. for (int i = 0; i < ZT_CENTRAL_CONTROLLER_COMMIT_THREADS; ++i) {
  132. _commitThread[i].join();
  133. }
  134. _onlineNotificationThread.join();
  135. }
  136. void CV1::configureSmee()
  137. {
  138. const char *TEMPORAL_SCHEME = "ZT_TEMPORAL_SCHEME";
  139. const char *TEMPORAL_HOST = "ZT_TEMPORAL_HOST";
  140. const char *TEMPORAL_PORT = "ZT_TEMPORAL_PORT";
  141. const char *TEMPORAL_NAMESPACE = "ZT_TEMPORAL_NAMESPACE";
  142. const char *SMEE_TASK_QUEUE = "ZT_SMEE_TASK_QUEUE";
  143. const char *scheme = getenv(TEMPORAL_SCHEME);
  144. if (scheme == NULL) {
  145. scheme = "http";
  146. }
  147. const char *host = getenv(TEMPORAL_HOST);
  148. const char *port = getenv(TEMPORAL_PORT);
  149. const char *ns = getenv(TEMPORAL_NAMESPACE);
  150. const char *task_queue = getenv(SMEE_TASK_QUEUE);
  151. if (scheme != NULL && host != NULL && port != NULL && ns != NULL && task_queue != NULL) {
  152. fprintf(stderr, "creating smee client\n");
  153. std::string hostPort = std::string(scheme) + std::string("://") + std::string(host) + std::string(":") + std::string(port);
  154. this->_smee = smeeclient::smee_client_new(hostPort.c_str(), ns, task_queue);
  155. } else {
  156. fprintf(stderr, "Smee client not configured\n");
  157. }
  158. }
  159. bool CV1::waitForReady()
  160. {
  161. while (_ready < 2) {
  162. _readyLock.lock();
  163. _readyLock.unlock();
  164. }
  165. return true;
  166. }
  167. bool CV1::isReady()
  168. {
  169. return ((_ready == 2)&&(_connected));
  170. }
  171. bool CV1::save(nlohmann::json &record,bool notifyListeners)
  172. {
  173. bool modified = false;
  174. try {
  175. if (!record.is_object()) {
  176. fprintf(stderr, "record is not an object?!?\n");
  177. return false;
  178. }
  179. const std::string objtype = record["objtype"];
  180. if (objtype == "network") {
  181. //fprintf(stderr, "network save\n");
  182. const uint64_t nwid = OSUtils::jsonIntHex(record["id"],0ULL);
  183. if (nwid) {
  184. nlohmann::json old;
  185. get(nwid,old);
  186. if ((!old.is_object())||(!_compareRecords(old,record))) {
  187. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  188. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  189. modified = true;
  190. }
  191. }
  192. } else if (objtype == "member") {
  193. std::string networkId = record["nwid"];
  194. std::string memberId = record["id"];
  195. const uint64_t nwid = OSUtils::jsonIntHex(record["nwid"],0ULL);
  196. const uint64_t id = OSUtils::jsonIntHex(record["id"],0ULL);
  197. //fprintf(stderr, "member save %s-%s\n", networkId.c_str(), memberId.c_str());
  198. if ((id)&&(nwid)) {
  199. nlohmann::json network,old;
  200. get(nwid,network,id,old);
  201. if ((!old.is_object())||(!_compareRecords(old,record))) {
  202. //fprintf(stderr, "commit queue post\n");
  203. record["revision"] = OSUtils::jsonInt(record["revision"],0ULL) + 1ULL;
  204. _commitQueue.post(std::pair<nlohmann::json,bool>(record,notifyListeners));
  205. modified = true;
  206. } else {
  207. //fprintf(stderr, "no change\n");
  208. }
  209. }
  210. } else {
  211. fprintf(stderr, "uhh waaat\n");
  212. }
  213. } catch (std::exception &e) {
  214. fprintf(stderr, "Error on PostgreSQL::save: %s\n", e.what());
  215. } catch (...) {
  216. fprintf(stderr, "Unknown error on PostgreSQL::save\n");
  217. }
  218. return modified;
  219. }
  220. void CV1::eraseNetwork(const uint64_t networkId)
  221. {
  222. fprintf(stderr, "PostgreSQL::eraseNetwork\n");
  223. char tmp2[24];
  224. waitForReady();
  225. Utils::hex(networkId, tmp2);
  226. std::pair<nlohmann::json,bool> tmp;
  227. tmp.first["id"] = tmp2;
  228. tmp.first["objtype"] = "_delete_network";
  229. tmp.second = true;
  230. _commitQueue.post(tmp);
  231. nlohmann::json nullJson;
  232. _networkChanged(tmp.first, nullJson, true);
  233. }
  234. void CV1::eraseMember(const uint64_t networkId, const uint64_t memberId)
  235. {
  236. fprintf(stderr, "PostgreSQL::eraseMember\n");
  237. char tmp2[24];
  238. waitForReady();
  239. std::pair<nlohmann::json,bool> tmp, nw;
  240. Utils::hex(networkId, tmp2);
  241. tmp.first["nwid"] = tmp2;
  242. Utils::hex(memberId, tmp2);
  243. tmp.first["id"] = tmp2;
  244. tmp.first["objtype"] = "_delete_member";
  245. tmp.second = true;
  246. _commitQueue.post(tmp);
  247. nlohmann::json nullJson;
  248. _memberChanged(tmp.first, nullJson, true);
  249. }
  250. void CV1::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress, const char *osArch)
  251. {
  252. std::lock_guard<std::mutex> l(_lastOnline_l);
  253. NodeOnlineRecord &i = _lastOnline[std::pair<uint64_t,uint64_t>(networkId, memberId)];
  254. i.lastSeen = OSUtils::now();
  255. if (physicalAddress) {
  256. i.physicalAddress = physicalAddress;
  257. }
  258. i.osArch = std::string(osArch);
  259. }
  260. void CV1::nodeIsOnline(const uint64_t networkId, const uint64_t memberId, const InetAddress &physicalAddress)
  261. {
  262. this->nodeIsOnline(networkId, memberId, physicalAddress, "unknown/unknown");
  263. }
  264. AuthInfo CV1::getSSOAuthInfo(const nlohmann::json &member, const std::string &redirectURL)
  265. {
  266. Metrics::db_get_sso_info++;
  267. // NONCE is just a random character string. no semantic meaning
  268. // state = HMAC SHA384 of Nonce based on shared sso key
  269. //
  270. // need nonce timeout in database? make sure it's used within X time
  271. // X is 5 minutes for now. Make configurable later?
  272. //
  273. // how do we tell when a nonce is used? if auth_expiration_time is set
  274. std::string networkId = member["nwid"];
  275. std::string memberId = member["id"];
  276. char authenticationURL[4096] = {0};
  277. AuthInfo info;
  278. info.enabled = true;
  279. //if (memberId == "a10dccea52" && networkId == "8056c2e21c24673d") {
  280. // fprintf(stderr, "invalid authinfo for grant's machine\n");
  281. // info.version=1;
  282. // return info;
  283. //}
  284. // fprintf(stderr, "PostgreSQL::updateMemberOnLoad: %s-%s\n", networkId.c_str(), memberId.c_str());
  285. std::shared_ptr<PostgresConnection> c;
  286. try {
  287. c = _pool->borrow();
  288. pqxx::work w(*c->c);
  289. char nonceBytes[16] = {0};
  290. std::string nonce = "";
  291. // check if the member exists first.
  292. pqxx::row count = w.exec_params1("SELECT count(id) FROM ztc_member WHERE id = $1 AND network_id = $2 AND deleted = false", memberId, networkId);
  293. if (count[0].as<int>() == 1) {
  294. // get active nonce, if exists.
  295. pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  296. "WHERE network_id = $1 AND member_id = $2 "
  297. "AND ((NOW() AT TIME ZONE 'UTC') <= authentication_expiry_time) AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  298. networkId, memberId);
  299. if (r.size() == 0) {
  300. // no active nonce.
  301. // find an unused nonce, if one exists.
  302. pqxx::result r = w.exec_params("SELECT nonce FROM ztc_sso_expiry "
  303. "WHERE network_id = $1 AND member_id = $2 "
  304. "AND authentication_expiry_time IS NULL AND ((NOW() AT TIME ZONE 'UTC') <= nonce_expiration)",
  305. networkId, memberId);
  306. if (r.size() == 1) {
  307. // we have an existing nonce. Use it
  308. nonce = r.at(0)[0].as<std::string>();
  309. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  310. } else if (r.empty()) {
  311. // create a nonce
  312. Utils::getSecureRandom(nonceBytes, 16);
  313. char nonceBuf[64] = {0};
  314. Utils::hex(nonceBytes, sizeof(nonceBytes), nonceBuf);
  315. nonce = std::string(nonceBuf);
  316. pqxx::result ir = w.exec_params0("INSERT INTO ztc_sso_expiry "
  317. "(nonce, nonce_expiration, network_id, member_id) VALUES "
  318. "($1, TO_TIMESTAMP($2::double precision/1000), $3, $4)",
  319. nonce, OSUtils::now() + 300000, networkId, memberId);
  320. w.commit();
  321. } else {
  322. // > 1 ?!? Thats an error!
  323. fprintf(stderr, "> 1 unused nonce!\n");
  324. exit(6);
  325. }
  326. } else if (r.size() == 1) {
  327. nonce = r.at(0)[0].as<std::string>();
  328. Utils::unhex(nonce.c_str(), nonceBytes, sizeof(nonceBytes));
  329. } else {
  330. // more than 1 nonce in use? Uhhh...
  331. fprintf(stderr, "> 1 nonce in use for network member?!?\n");
  332. exit(7);
  333. }
  334. r = w.exec_params(
  335. "SELECT oc.client_id, oc.authorization_endpoint, oc.issuer, oc.provider, oc.sso_impl_version "
  336. "FROM ztc_network AS n "
  337. "INNER JOIN ztc_org o "
  338. " ON o.owner_id = n.owner_id "
  339. "LEFT OUTER JOIN ztc_network_oidc_config noc "
  340. " ON noc.network_id = n.id "
  341. "LEFT OUTER JOIN ztc_oidc_config oc "
  342. " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  343. "WHERE n.id = $1 AND n.sso_enabled = true", networkId);
  344. std::string client_id = "";
  345. std::string authorization_endpoint = "";
  346. std::string issuer = "";
  347. std::string provider = "";
  348. uint64_t sso_version = 0;
  349. if (r.size() == 1) {
  350. client_id = r.at(0)[0].as<std::optional<std::string>>().value_or("");
  351. authorization_endpoint = r.at(0)[1].as<std::optional<std::string>>().value_or("");
  352. issuer = r.at(0)[2].as<std::optional<std::string>>().value_or("");
  353. provider = r.at(0)[3].as<std::optional<std::string>>().value_or("");
  354. sso_version = r.at(0)[4].as<std::optional<uint64_t>>().value_or(1);
  355. } else if (r.size() > 1) {
  356. fprintf(stderr, "ERROR: More than one auth endpoint for an organization?!?!? NetworkID: %s\n", networkId.c_str());
  357. } else {
  358. fprintf(stderr, "No client or auth endpoint?!?\n");
  359. }
  360. info.version = sso_version;
  361. // no catch all else because we don't actually care if no records exist here. just continue as normal.
  362. if ((!client_id.empty())&&(!authorization_endpoint.empty())) {
  363. uint8_t state[48];
  364. HMACSHA384(_ssoPsk, nonceBytes, sizeof(nonceBytes), state);
  365. char state_hex[256];
  366. Utils::hex(state, 48, state_hex);
  367. if (info.version == 0) {
  368. char url[2048] = {0};
  369. OSUtils::ztsnprintf(url, sizeof(authenticationURL),
  370. "%s?response_type=id_token&response_mode=form_post&scope=openid+email+profile&redirect_uri=%s&nonce=%s&state=%s&client_id=%s",
  371. authorization_endpoint.c_str(),
  372. url_encode(redirectURL).c_str(),
  373. nonce.c_str(),
  374. state_hex,
  375. client_id.c_str());
  376. info.authenticationURL = std::string(url);
  377. } else if (info.version == 1) {
  378. info.ssoClientID = client_id;
  379. info.issuerURL = issuer;
  380. info.ssoProvider = provider;
  381. info.ssoNonce = nonce;
  382. info.ssoState = std::string(state_hex) + "_" +networkId;
  383. info.centralAuthURL = redirectURL;
  384. #ifdef ZT_DEBUG
  385. fprintf(
  386. stderr,
  387. "ssoClientID: %s\nissuerURL: %s\nssoNonce: %s\nssoState: %s\ncentralAuthURL: %s\nprovider: %s\n",
  388. info.ssoClientID.c_str(),
  389. info.issuerURL.c_str(),
  390. info.ssoNonce.c_str(),
  391. info.ssoState.c_str(),
  392. info.centralAuthURL.c_str(),
  393. provider.c_str());
  394. #endif
  395. }
  396. } else {
  397. fprintf(stderr, "client_id: %s\nauthorization_endpoint: %s\n", client_id.c_str(), authorization_endpoint.c_str());
  398. }
  399. }
  400. _pool->unborrow(c);
  401. } catch (std::exception &e) {
  402. fprintf(stderr, "ERROR: Error updating member on load for network %s: %s\n", networkId.c_str(), e.what());
  403. }
  404. return info; //std::string(authenticationURL);
  405. }
  406. void CV1::initializeNetworks()
  407. {
  408. try {
  409. std::string setKey = "networks:{" + _myAddressStr + "}";
  410. fprintf(stderr, "Initializing Networks...\n");
  411. if (_redisMemberStatus) {
  412. fprintf(stderr, "Init Redis for networks...\n");
  413. try {
  414. if (_rc->clusterMode) {
  415. _cluster->del(setKey);
  416. } else {
  417. _redis->del(setKey);
  418. }
  419. } catch (sw::redis::Error &e) {
  420. // ignore. if this key doesn't exist, there's no reason to delete it
  421. }
  422. }
  423. std::unordered_set<std::string> networkSet;
  424. char qbuf[2048] = {0};
  425. sprintf(qbuf,
  426. "SELECT n.id, (EXTRACT(EPOCH FROM n.creation_time AT TIME ZONE 'UTC')*1000)::bigint as creation_time, n.capabilities, "
  427. "n.enable_broadcast, (EXTRACT(EPOCH FROM n.last_modified AT TIME ZONE 'UTC')*1000)::bigint AS last_modified, n.mtu, n.multicast_limit, n.name, n.private, n.remote_trace_level, "
  428. "n.remote_trace_target, n.revision, n.rules, n.tags, n.v4_assign_mode, n.v6_assign_mode, n.sso_enabled, (CASE WHEN n.sso_enabled THEN noc.client_id ELSE NULL END) as client_id, "
  429. "(CASE WHEN n.sso_enabled THEN oc.authorization_endpoint ELSE NULL END) as authorization_endpoint, "
  430. "(CASE WHEN n.sso_enabled THEN oc.provider ELSE NULL END) as provider, d.domain, d.servers, "
  431. "ARRAY(SELECT CONCAT(host(ip_range_start),'|', host(ip_range_end)) FROM ztc_network_assignment_pool WHERE network_id = n.id) AS assignment_pool, "
  432. "ARRAY(SELECT CONCAT(host(address),'/',bits::text,'|',COALESCE(host(via), 'NULL'))FROM ztc_network_route WHERE network_id = n.id) AS routes "
  433. "FROM ztc_network n "
  434. "LEFT OUTER JOIN ztc_org o "
  435. " ON o.owner_id = n.owner_id "
  436. "LEFT OUTER JOIN ztc_network_oidc_config noc "
  437. " ON noc.network_id = n.id "
  438. "LEFT OUTER JOIN ztc_oidc_config oc "
  439. " ON noc.client_id = oc.client_id AND oc.org_id = o.org_id "
  440. "LEFT OUTER JOIN ztc_network_dns d "
  441. " ON d.network_id = n.id "
  442. "WHERE deleted = false AND controller_id = '%s'", _myAddressStr.c_str());
  443. auto c = _pool->borrow();
  444. auto c2 = _pool->borrow();
  445. pqxx::work w{*c->c};
  446. fprintf(stderr, "Load networks from psql...\n");
  447. auto stream = pqxx::stream_from::query(w, qbuf);
  448. std::tuple<
  449. std::string // network ID
  450. , std::optional<int64_t> // creationTime
  451. , std::optional<std::string> // capabilities
  452. , std::optional<bool> // enableBroadcast
  453. , std::optional<uint64_t> // lastModified
  454. , std::optional<int> // mtu
  455. , std::optional<int> // multicastLimit
  456. , std::optional<std::string> // name
  457. , bool // private
  458. , std::optional<int> // remoteTraceLevel
  459. , std::optional<std::string> // remoteTraceTarget
  460. , std::optional<uint64_t> // revision
  461. , std::optional<std::string> // rules
  462. , std::optional<std::string> // tags
  463. , std::optional<std::string> // v4AssignMode
  464. , std::optional<std::string> // v6AssignMode
  465. , std::optional<bool> // ssoEnabled
  466. , std::optional<std::string> // clientId
  467. , std::optional<std::string> // authorizationEndpoint
  468. , std::optional<std::string> // ssoProvider
  469. , std::optional<std::string> // domain
  470. , std::optional<std::string> // servers
  471. , std::string // assignmentPoolString
  472. , std::string // routeString
  473. > row;
  474. uint64_t count = 0;
  475. auto tmp = std::chrono::high_resolution_clock::now();
  476. uint64_t total = 0;
  477. while (stream >> row) {
  478. auto start = std::chrono::high_resolution_clock::now();
  479. json empty;
  480. json config;
  481. initNetwork(config);
  482. std::string nwid = std::get<0>(row);
  483. std::optional<int64_t> creationTime = std::get<1>(row);
  484. std::optional<std::string> capabilities = std::get<2>(row);
  485. std::optional<bool> enableBroadcast = std::get<3>(row);
  486. std::optional<uint64_t> lastModified = std::get<4>(row);
  487. std::optional<int> mtu = std::get<5>(row);
  488. std::optional<int> multicastLimit = std::get<6>(row);
  489. std::optional<std::string> name = std::get<7>(row);
  490. bool isPrivate = std::get<8>(row);
  491. std::optional<int> remoteTraceLevel = std::get<9>(row);
  492. std::optional<std::string> remoteTraceTarget = std::get<10>(row);
  493. std::optional<uint64_t> revision = std::get<11>(row);
  494. std::optional<std::string> rules = std::get<12>(row);
  495. std::optional<std::string> tags = std::get<13>(row);
  496. std::optional<std::string> v4AssignMode = std::get<14>(row);
  497. std::optional<std::string> v6AssignMode = std::get<15>(row);
  498. std::optional<bool> ssoEnabled = std::get<16>(row);
  499. std::optional<std::string> clientId = std::get<17>(row);
  500. std::optional<std::string> authorizationEndpoint = std::get<18>(row);
  501. std::optional<std::string> ssoProvider = std::get<19>(row);
  502. std::optional<std::string> dnsDomain = std::get<20>(row);
  503. std::optional<std::string> dnsServers = std::get<21>(row);
  504. std::string assignmentPoolString = std::get<22>(row);
  505. std::string routesString = std::get<23>(row);
  506. config["id"] = nwid;
  507. config["nwid"] = nwid;
  508. config["creationTime"] = creationTime.value_or(0);
  509. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  510. config["enableBroadcast"] = enableBroadcast.value_or(false);
  511. config["lastModified"] = lastModified.value_or(0);
  512. config["mtu"] = mtu.value_or(2800);
  513. config["multicastLimit"] = multicastLimit.value_or(64);
  514. config["name"] = name.value_or("");
  515. config["private"] = isPrivate;
  516. config["remoteTraceLevel"] = remoteTraceLevel.value_or(0);
  517. config["remoteTraceTarget"] = remoteTraceTarget.value_or("");
  518. config["revision"] = revision.value_or(0);
  519. config["rules"] = json::parse(rules.value_or("[]"));
  520. config["tags"] = json::parse(tags.value_or("[]"));
  521. config["v4AssignMode"] = json::parse(v4AssignMode.value_or("{}"));
  522. config["v6AssignMode"] = json::parse(v6AssignMode.value_or("{}"));
  523. config["ssoEnabled"] = ssoEnabled.value_or(false);
  524. config["objtype"] = "network";
  525. config["ipAssignmentPools"] = json::array();
  526. config["routes"] = json::array();
  527. config["clientId"] = clientId.value_or("");
  528. config["authorizationEndpoint"] = authorizationEndpoint.value_or("");
  529. config["provider"] = ssoProvider.value_or("");
  530. networkSet.insert(nwid);
  531. if (dnsDomain.has_value()) {
  532. std::string serverList = dnsServers.value();
  533. json obj;
  534. auto servers = json::array();
  535. if (serverList.rfind("{",0) != std::string::npos) {
  536. serverList = serverList.substr(1, serverList.size()-2);
  537. std::stringstream ss(serverList);
  538. while(ss.good()) {
  539. std::string server;
  540. std::getline(ss, server, ',');
  541. servers.push_back(server);
  542. }
  543. }
  544. obj["domain"] = dnsDomain.value();
  545. obj["servers"] = servers;
  546. config["dns"] = obj;
  547. }
  548. config["ipAssignmentPools"] = json::array();
  549. if (assignmentPoolString != "{}") {
  550. std::string tmp = assignmentPoolString.substr(1, assignmentPoolString.size()-2);
  551. std::vector<std::string> assignmentPools = split(tmp, ',');
  552. for (auto it = assignmentPools.begin(); it != assignmentPools.end(); ++it) {
  553. std::vector<std::string> r = split(*it, '|');
  554. json ip;
  555. ip["ipRangeStart"] = r[0];
  556. ip["ipRangeEnd"] = r[1];
  557. config["ipAssignmentPools"].push_back(ip);
  558. }
  559. }
  560. config["routes"] = json::array();
  561. if (routesString != "{}") {
  562. std::string tmp = routesString.substr(1, routesString.size()-2);
  563. std::vector<std::string> routes = split(tmp, ',');
  564. for (auto it = routes.begin(); it != routes.end(); ++it) {
  565. std::vector<std::string> r = split(*it, '|');
  566. json route;
  567. route["target"] = r[0];
  568. route["via"] = ((route["via"] == "NULL")? nullptr : r[1]);
  569. config["routes"].push_back(route);
  570. }
  571. }
  572. Metrics::network_count++;
  573. _networkChanged(empty, config, false);
  574. auto end = std::chrono::high_resolution_clock::now();
  575. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);;
  576. total += dur.count();
  577. ++count;
  578. if (count > 0 && count % 10000 == 0) {
  579. fprintf(stderr, "Averaging %llu us per network\n", (total/count));
  580. }
  581. }
  582. if (count > 0) {
  583. fprintf(stderr, "Took %llu us per network to load\n", (total/count));
  584. }
  585. stream.complete();
  586. w.commit();
  587. _pool->unborrow(c2);
  588. _pool->unborrow(c);
  589. fprintf(stderr, "done.\n");
  590. if (!networkSet.empty()) {
  591. if (_redisMemberStatus) {
  592. fprintf(stderr, "adding networks to redis...\n");
  593. if (_rc->clusterMode) {
  594. auto tx = _cluster->transaction(_myAddressStr, true, false);
  595. uint64_t count = 0;
  596. for (std::string nwid : networkSet) {
  597. tx.sadd(setKey, nwid);
  598. if (++count % 30000 == 0) {
  599. tx.exec();
  600. tx = _cluster->transaction(_myAddressStr, true, false);
  601. }
  602. }
  603. tx.exec();
  604. } else {
  605. auto tx = _redis->transaction(true, false);
  606. uint64_t count = 0;
  607. for (std::string nwid : networkSet) {
  608. tx.sadd(setKey, nwid);
  609. if (++count % 30000 == 0) {
  610. tx.exec();
  611. tx = _redis->transaction(true, false);
  612. }
  613. }
  614. tx.exec();
  615. }
  616. fprintf(stderr, "done.\n");
  617. }
  618. }
  619. if (++this->_ready == 2) {
  620. if (_waitNoticePrinted) {
  621. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  622. }
  623. _readyLock.unlock();
  624. }
  625. fprintf(stderr, "network init done.\n");
  626. } catch (sw::redis::Error &e) {
  627. fprintf(stderr, "ERROR: Error initializing networks in Redis: %s\n", e.what());
  628. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  629. exit(-1);
  630. } catch (std::exception &e) {
  631. fprintf(stderr, "ERROR: Error initializing networks: %s\n", e.what());
  632. std::this_thread::sleep_for(std::chrono::milliseconds(5000));
  633. exit(-1);
  634. }
  635. }
  636. void CV1::initializeMembers()
  637. {
  638. std::string memberId;
  639. std::string networkId;
  640. try {
  641. std::unordered_map<std::string, std::string> networkMembers;
  642. fprintf(stderr, "Initializing Members...\n");
  643. std::string setKeyBase = "network-nodes-all:{" + _myAddressStr + "}:";
  644. if (_redisMemberStatus) {
  645. fprintf(stderr, "Initialize Redis for members...\n");
  646. std::unique_lock<std::shared_mutex> l(_networks_l);
  647. std::unordered_set<std::string> deletes;
  648. for ( auto it : _networks) {
  649. uint64_t nwid_i = it.first;
  650. char nwidTmp[64] = {0};
  651. OSUtils::ztsnprintf(nwidTmp, sizeof(nwidTmp), "%.16llx", nwid_i);
  652. std::string nwid(nwidTmp);
  653. std::string key = setKeyBase + nwid;
  654. deletes.insert(key);
  655. }
  656. if (!deletes.empty()) {
  657. try {
  658. if (_rc->clusterMode) {
  659. auto tx = _cluster->transaction(_myAddressStr, true, false);
  660. for (std::string k : deletes) {
  661. tx.del(k);
  662. }
  663. tx.exec();
  664. } else {
  665. auto tx = _redis->transaction(true, false);
  666. for (std::string k : deletes) {
  667. tx.del(k);
  668. }
  669. tx.exec();
  670. }
  671. } catch (sw::redis::Error &e) {
  672. // ignore
  673. }
  674. }
  675. }
  676. char qbuf[2048];
  677. sprintf(qbuf,
  678. "SELECT m.id, m.network_id, m.active_bridge, m.authorized, m.capabilities, "
  679. "(EXTRACT(EPOCH FROM m.creation_time AT TIME ZONE 'UTC')*1000)::bigint, m.identity, "
  680. "(EXTRACT(EPOCH FROM m.last_authorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  681. "(EXTRACT(EPOCH FROM m.last_deauthorized_time AT TIME ZONE 'UTC')*1000)::bigint, "
  682. "m.remote_trace_level, m.remote_trace_target, m.tags, m.v_major, m.v_minor, m.v_rev, m.v_proto, "
  683. "m.no_auto_assign_ips, m.revision, m.sso_exempt, "
  684. "(CASE WHEN n.sso_enabled = TRUE AND m.sso_exempt = FALSE THEN "
  685. " ( "
  686. " SELECT (EXTRACT(EPOCH FROM e.authentication_expiry_time)*1000)::bigint "
  687. " FROM ztc_sso_expiry e "
  688. " INNER JOIN ztc_network n1 "
  689. " ON n1.id = e.network_id AND n1.deleted = TRUE "
  690. " WHERE e.network_id = m.network_id AND e.member_id = m.id AND n.sso_enabled = TRUE AND e.authentication_expiry_time IS NOT NULL "
  691. " ORDER BY e.authentication_expiry_time DESC LIMIT 1 "
  692. " ) "
  693. " ELSE NULL "
  694. " END) AS authentication_expiry_time, "
  695. "ARRAY(SELECT DISTINCT address FROM ztc_member_ip_assignment WHERE member_id = m.id AND network_id = m.network_id) AS assigned_addresses "
  696. "FROM ztc_member m "
  697. "INNER JOIN ztc_network n "
  698. " ON n.id = m.network_id "
  699. "WHERE n.controller_id = '%s' AND n.deleted = FALSE AND m.deleted = FALSE", _myAddressStr.c_str());
  700. auto c = _pool->borrow();
  701. auto c2 = _pool->borrow();
  702. pqxx::work w{*c->c};
  703. fprintf(stderr, "Load members from psql...\n");
  704. auto stream = pqxx::stream_from::query(w, qbuf);
  705. std::tuple<
  706. std::string // memberId
  707. , std::string // memberId
  708. , std::optional<bool> // activeBridge
  709. , std::optional<bool> // authorized
  710. , std::optional<std::string> // capabilities
  711. , std::optional<uint64_t> // creationTime
  712. , std::optional<std::string> // identity
  713. , std::optional<uint64_t> // lastAuthorizedTime
  714. , std::optional<uint64_t> // lastDeauthorizedTime
  715. , std::optional<int> // remoteTraceLevel
  716. , std::optional<std::string> // remoteTraceTarget
  717. , std::optional<std::string> // tags
  718. , std::optional<int> // vMajor
  719. , std::optional<int> // vMinor
  720. , std::optional<int> // vRev
  721. , std::optional<int> // vProto
  722. , std::optional<bool> // noAutoAssignIps
  723. , std::optional<uint64_t> // revision
  724. , std::optional<bool> // ssoExempt
  725. , std::optional<uint64_t> // authenticationExpiryTime
  726. , std::string // assignedAddresses
  727. > row;
  728. uint64_t count = 0;
  729. auto tmp = std::chrono::high_resolution_clock::now();
  730. uint64_t total = 0;
  731. while (stream >> row) {
  732. auto start = std::chrono::high_resolution_clock::now();
  733. json empty;
  734. json config;
  735. initMember(config);
  736. memberId = std::get<0>(row);
  737. networkId = std::get<1>(row);
  738. std::optional<bool> activeBridge = std::get<2>(row);
  739. std::optional<bool> authorized = std::get<3>(row);
  740. std::optional<std::string> capabilities = std::get<4>(row);
  741. std::optional<uint64_t> creationTime = std::get<5>(row);
  742. std::optional<std::string> identity = std::get<6>(row);
  743. std::optional<uint64_t> lastAuthorizedTime = std::get<7>(row);
  744. std::optional<uint64_t> lastDeauthorizedTime = std::get<8>(row);
  745. std::optional<int> remoteTraceLevel = std::get<9>(row);
  746. std::optional<std::string> remoteTraceTarget = std::get<10>(row);
  747. std::optional<std::string> tags = std::get<11>(row);
  748. std::optional<int> vMajor = std::get<12>(row);
  749. std::optional<int> vMinor = std::get<13>(row);
  750. std::optional<int> vRev = std::get<14>(row);
  751. std::optional<int> vProto = std::get<15>(row);
  752. std::optional<bool> noAutoAssignIps = std::get<16>(row);
  753. std::optional<uint64_t> revision = std::get<17>(row);
  754. std::optional<bool> ssoExempt = std::get<18>(row);
  755. std::optional<uint64_t> authenticationExpiryTime = std::get<19>(row);
  756. std::string assignedAddresses = std::get<20>(row);
  757. networkMembers.insert(std::pair<std::string, std::string>(setKeyBase+networkId, memberId));
  758. config["id"] = memberId;
  759. config["address"] = memberId;
  760. config["nwid"] = networkId;
  761. config["activeBridge"] = activeBridge.value_or(false);
  762. config["authorized"] = authorized.value_or(false);
  763. config["capabilities"] = json::parse(capabilities.value_or("[]"));
  764. config["creationTime"] = creationTime.value_or(0);
  765. config["identity"] = identity.value_or("");
  766. config["lastAuthorizedTime"] = lastAuthorizedTime.value_or(0);
  767. config["lastDeauthorizedTime"] = lastDeauthorizedTime.value_or(0);
  768. config["remoteTraceLevel"] = remoteTraceLevel.value_or(0);
  769. config["remoteTraceTarget"] = remoteTraceTarget.value_or("");
  770. config["tags"] = json::parse(tags.value_or("[]"));
  771. config["vMajor"] = vMajor.value_or(-1);
  772. config["vMinor"] = vMinor.value_or(-1);
  773. config["vRev"] = vRev.value_or(-1);
  774. config["vProto"] = vProto.value_or(-1);
  775. config["noAutoAssignIps"] = noAutoAssignIps.value_or(false);
  776. config["revision"] = revision.value_or(0);
  777. config["ssoExempt"] = ssoExempt.value_or(false);
  778. config["authenticationExpiryTime"] = authenticationExpiryTime.value_or(0);
  779. config["objtype"] = "member";
  780. config["ipAssignments"] = json::array();
  781. if (assignedAddresses != "{}") {
  782. std::string tmp = assignedAddresses.substr(1, assignedAddresses.size()-2);
  783. std::vector<std::string> addrs = split(tmp, ',');
  784. for (auto it = addrs.begin(); it != addrs.end(); ++it) {
  785. config["ipAssignments"].push_back(*it);
  786. }
  787. }
  788. Metrics::member_count++;
  789. _memberChanged(empty, config, false);
  790. memberId = "";
  791. networkId = "";
  792. auto end = std::chrono::high_resolution_clock::now();
  793. auto dur = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
  794. total += dur.count();
  795. ++count;
  796. if (count > 0 && count % 10000 == 0) {
  797. fprintf(stderr, "Averaging %llu us per member\n", (total/count));
  798. }
  799. }
  800. if (count > 0) {
  801. fprintf(stderr, "Took %llu us per member to load\n", (total/count));
  802. }
  803. stream.complete();
  804. w.commit();
  805. _pool->unborrow(c2);
  806. _pool->unborrow(c);
  807. fprintf(stderr, "done.\n");
  808. if (!networkMembers.empty()) {
  809. if (_redisMemberStatus) {
  810. fprintf(stderr, "Load member data into redis...\n");
  811. if (_rc->clusterMode) {
  812. auto tx = _cluster->transaction(_myAddressStr, true, false);
  813. uint64_t count = 0;
  814. for (auto it : networkMembers) {
  815. tx.sadd(it.first, it.second);
  816. if (++count % 30000 == 0) {
  817. tx.exec();
  818. tx = _cluster->transaction(_myAddressStr, true, false);
  819. }
  820. }
  821. tx.exec();
  822. } else {
  823. auto tx = _redis->transaction(true, false);
  824. uint64_t count = 0;
  825. for (auto it : networkMembers) {
  826. tx.sadd(it.first, it.second);
  827. if (++count % 30000 == 0) {
  828. tx.exec();
  829. tx = _redis->transaction(true, false);
  830. }
  831. }
  832. tx.exec();
  833. }
  834. fprintf(stderr, "done.\n");
  835. }
  836. }
  837. fprintf(stderr, "Done loading members...\n");
  838. if (++this->_ready == 2) {
  839. if (_waitNoticePrinted) {
  840. fprintf(stderr,"[%s] NOTICE: %.10llx controller PostgreSQL data download complete." ZT_EOL_S,_timestr(),(unsigned long long)_myAddress.toInt());
  841. }
  842. _readyLock.unlock();
  843. }
  844. } catch (sw::redis::Error &e) {
  845. fprintf(stderr, "ERROR: Error initializing members (redis): %s\n", e.what());
  846. exit(-1);
  847. } catch (std::exception &e) {
  848. fprintf(stderr, "ERROR: Error initializing member: %s-%s %s\n", networkId.c_str(), memberId.c_str(), e.what());
  849. exit(-1);
  850. }
  851. }
  852. void CV1::heartbeat()
  853. {
  854. char publicId[1024];
  855. char hostnameTmp[1024];
  856. _myId.toString(false,publicId);
  857. if (gethostname(hostnameTmp, sizeof(hostnameTmp))!= 0) {
  858. hostnameTmp[0] = (char)0;
  859. } else {
  860. for (int i = 0; i < (int)sizeof(hostnameTmp); ++i) {
  861. if ((hostnameTmp[i] == '.')||(hostnameTmp[i] == 0)) {
  862. hostnameTmp[i] = (char)0;
  863. break;
  864. }
  865. }
  866. }
  867. const char *controllerId = _myAddressStr.c_str();
  868. const char *publicIdentity = publicId;
  869. const char *hostname = hostnameTmp;
  870. while (_run == 1) {
  871. // fprintf(stderr, "%s: heartbeat\n", controllerId);
  872. auto c = _pool->borrow();
  873. int64_t ts = OSUtils::now();
  874. if(c->c) {
  875. std::string major = std::to_string(ZEROTIER_ONE_VERSION_MAJOR);
  876. std::string minor = std::to_string(ZEROTIER_ONE_VERSION_MINOR);
  877. std::string rev = std::to_string(ZEROTIER_ONE_VERSION_REVISION);
  878. std::string build = std::to_string(ZEROTIER_ONE_VERSION_BUILD);
  879. std::string now = std::to_string(ts);
  880. std::string host_port = std::to_string(_listenPort);
  881. std::string use_redis = (_rc != NULL) ? "true" : "false";
  882. std::string redis_mem_status = (_redisMemberStatus) ? "true" : "false";
  883. try {
  884. pqxx::work w{*c->c};
  885. pqxx::result res =
  886. w.exec0("INSERT INTO ztc_controller (id, cluster_host, last_alive, public_identity, v_major, v_minor, v_rev, v_build, host_port, use_redis, redis_member_status) "
  887. "VALUES ("+w.quote(controllerId)+", "+w.quote(hostname)+", TO_TIMESTAMP("+now+"::double precision/1000), "+
  888. w.quote(publicIdentity)+", "+major+", "+minor+", "+rev+", "+build+", "+host_port+", "+use_redis+", "+redis_mem_status+") "
  889. "ON CONFLICT (id) DO UPDATE SET cluster_host = EXCLUDED.cluster_host, last_alive = EXCLUDED.last_alive, "
  890. "public_identity = EXCLUDED.public_identity, v_major = EXCLUDED.v_major, v_minor = EXCLUDED.v_minor, "
  891. "v_rev = EXCLUDED.v_rev, v_build = EXCLUDED.v_rev, host_port = EXCLUDED.host_port, "
  892. "use_redis = EXCLUDED.use_redis, redis_member_status = EXCLUDED.redis_member_status");
  893. w.commit();
  894. } catch (std::exception &e) {
  895. fprintf(stderr, "%s: Heartbeat update failed: %s\n", controllerId, e.what());
  896. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  897. continue;
  898. }
  899. }
  900. _pool->unborrow(c);
  901. try {
  902. if (_redisMemberStatus) {
  903. if (_rc->clusterMode) {
  904. _cluster->zadd("controllers", "controllerId", ts);
  905. } else {
  906. _redis->zadd("controllers", "controllerId", ts);
  907. }
  908. }
  909. } catch (sw::redis::Error &e) {
  910. fprintf(stderr, "ERROR: Redis error in heartbeat thread: %s\n", e.what());
  911. }
  912. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  913. }
  914. fprintf(stderr, "Exited heartbeat thread\n");
  915. }
  916. void CV1::membersDbWatcher()
  917. {
  918. if (_rc) {
  919. _membersWatcher_Redis();
  920. } else {
  921. _membersWatcher_Postgres();
  922. }
  923. if (_run == 1) {
  924. fprintf(stderr, "ERROR: %s membersDbWatcher should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  925. exit(9);
  926. }
  927. fprintf(stderr, "Exited membersDbWatcher\n");
  928. }
  929. void CV1::_membersWatcher_Postgres() {
  930. auto c = _pool->borrow();
  931. std::string stream = "member_" + _myAddressStr;
  932. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  933. MemberNotificationReceiver m(this, *c->c, stream);
  934. while(_run == 1) {
  935. c->c->await_notification(5, 0);
  936. }
  937. _pool->unborrow(c);
  938. }
  939. void CV1::_membersWatcher_Redis() {
  940. char buf[11] = {0};
  941. std::string key = "member-stream:{" + std::string(_myAddress.toString(buf)) + "}";
  942. std::string lastID = "0";
  943. fprintf(stderr, "Listening to member stream: %s\n", key.c_str());
  944. while (_run == 1) {
  945. try {
  946. json tmp;
  947. std::unordered_map<std::string, ItemStream> result;
  948. if (_rc->clusterMode) {
  949. _cluster->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  950. } else {
  951. _redis->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  952. }
  953. if (!result.empty()) {
  954. for (auto element : result) {
  955. #ifdef REDIS_TRACE
  956. fprintf(stdout, "Received notification from: %s\n", element.first.c_str());
  957. #endif
  958. for (auto rec : element.second) {
  959. std::string id = rec.first;
  960. auto attrs = rec.second;
  961. #ifdef REDIS_TRACE
  962. fprintf(stdout, "Record ID: %s\n", id.c_str());
  963. fprintf(stdout, "attrs len: %lu\n", attrs.size());
  964. #endif
  965. for (auto a : attrs) {
  966. #ifdef REDIS_TRACE
  967. fprintf(stdout, "key: %s\nvalue: %s\n", a.first.c_str(), a.second.c_str());
  968. #endif
  969. try {
  970. tmp = json::parse(a.second);
  971. json &ov = tmp["old_val"];
  972. json &nv = tmp["new_val"];
  973. json oldConfig, newConfig;
  974. if (ov.is_object()) oldConfig = ov;
  975. if (nv.is_object()) newConfig = nv;
  976. if (oldConfig.is_object()||newConfig.is_object()) {
  977. _memberChanged(oldConfig,newConfig,(this->_ready >= 2));
  978. }
  979. } catch (...) {
  980. fprintf(stderr, "json parse error in _membersWatcher_Redis: %s\n", a.second.c_str());
  981. }
  982. }
  983. if (_rc->clusterMode) {
  984. _cluster->xdel(key, id);
  985. } else {
  986. _redis->xdel(key, id);
  987. }
  988. lastID = id;
  989. Metrics::redis_mem_notification++;
  990. }
  991. }
  992. }
  993. } catch (sw::redis::Error &e) {
  994. fprintf(stderr, "Error in Redis members watcher: %s\n", e.what());
  995. }
  996. }
  997. fprintf(stderr, "membersWatcher ended\n");
  998. }
  999. void CV1::networksDbWatcher()
  1000. {
  1001. if (_rc) {
  1002. _networksWatcher_Redis();
  1003. } else {
  1004. _networksWatcher_Postgres();
  1005. }
  1006. if (_run == 1) {
  1007. fprintf(stderr, "ERROR: %s networksDbWatcher should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  1008. exit(8);
  1009. }
  1010. fprintf(stderr, "Exited networksDbWatcher\n");
  1011. }
  1012. void CV1::_networksWatcher_Postgres() {
  1013. std::string stream = "network_" + _myAddressStr;
  1014. fprintf(stderr, "Listening to member stream: %s\n", stream.c_str());
  1015. auto c = _pool->borrow();
  1016. NetworkNotificationReceiver n(this, *c->c, stream);
  1017. while(_run == 1) {
  1018. c->c->await_notification(5,0);
  1019. }
  1020. }
  1021. void CV1::_networksWatcher_Redis() {
  1022. char buf[11] = {0};
  1023. std::string key = "network-stream:{" + std::string(_myAddress.toString(buf)) + "}";
  1024. std::string lastID = "0";
  1025. while (_run == 1) {
  1026. try {
  1027. json tmp;
  1028. std::unordered_map<std::string, ItemStream> result;
  1029. if (_rc->clusterMode) {
  1030. _cluster->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  1031. } else {
  1032. _redis->xread(key, lastID, std::chrono::seconds(1), 0, std::inserter(result, result.end()));
  1033. }
  1034. if (!result.empty()) {
  1035. for (auto element : result) {
  1036. #ifdef REDIS_TRACE
  1037. fprintf(stdout, "Received notification from: %s\n", element.first.c_str());
  1038. #endif
  1039. for (auto rec : element.second) {
  1040. std::string id = rec.first;
  1041. auto attrs = rec.second;
  1042. #ifdef REDIS_TRACE
  1043. fprintf(stdout, "Record ID: %s\n", id.c_str());
  1044. fprintf(stdout, "attrs len: %lu\n", attrs.size());
  1045. #endif
  1046. for (auto a : attrs) {
  1047. #ifdef REDIS_TRACE
  1048. fprintf(stdout, "key: %s\nvalue: %s\n", a.first.c_str(), a.second.c_str());
  1049. #endif
  1050. try {
  1051. tmp = json::parse(a.second);
  1052. json &ov = tmp["old_val"];
  1053. json &nv = tmp["new_val"];
  1054. json oldConfig, newConfig;
  1055. if (ov.is_object()) oldConfig = ov;
  1056. if (nv.is_object()) newConfig = nv;
  1057. if (oldConfig.is_object()||newConfig.is_object()) {
  1058. _networkChanged(oldConfig,newConfig,(this->_ready >= 2));
  1059. }
  1060. } catch (std::exception &e) {
  1061. fprintf(stderr, "json parse error in networkWatcher_Redis: what: %s json: %s\n", e.what(), a.second.c_str());
  1062. }
  1063. }
  1064. if (_rc->clusterMode) {
  1065. _cluster->xdel(key, id);
  1066. } else {
  1067. _redis->xdel(key, id);
  1068. }
  1069. lastID = id;
  1070. }
  1071. Metrics::redis_net_notification++;
  1072. }
  1073. }
  1074. } catch (sw::redis::Error &e) {
  1075. fprintf(stderr, "Error in Redis networks watcher: %s\n", e.what());
  1076. }
  1077. }
  1078. fprintf(stderr, "networksWatcher ended\n");
  1079. }
  1080. void CV1::commitThread()
  1081. {
  1082. fprintf(stderr, "%s: commitThread start\n", _myAddressStr.c_str());
  1083. std::pair<nlohmann::json,bool> qitem;
  1084. while(_commitQueue.get(qitem)&(_run == 1)) {
  1085. //fprintf(stderr, "commitThread tick\n");
  1086. if (!qitem.first.is_object()) {
  1087. fprintf(stderr, "not an object\n");
  1088. continue;
  1089. }
  1090. std::shared_ptr<PostgresConnection> c;
  1091. try {
  1092. c = _pool->borrow();
  1093. } catch (std::exception &e) {
  1094. fprintf(stderr, "ERROR: %s\n", e.what());
  1095. continue;
  1096. }
  1097. if (!c) {
  1098. fprintf(stderr, "Error getting database connection\n");
  1099. continue;
  1100. }
  1101. Metrics::pgsql_commit_ticks++;
  1102. try {
  1103. nlohmann::json &config = (qitem.first);
  1104. const std::string objtype = config["objtype"];
  1105. if (objtype == "member") {
  1106. // fprintf(stderr, "%s: commitThread: member\n", _myAddressStr.c_str());
  1107. std::string memberId;
  1108. std::string networkId;
  1109. try {
  1110. pqxx::work w(*c->c);
  1111. memberId = config["id"];
  1112. networkId = config["nwid"];
  1113. std::string target = "NULL";
  1114. if (!config["remoteTraceTarget"].is_null()) {
  1115. target = config["remoteTraceTarget"];
  1116. }
  1117. pqxx::row nwrow = w.exec_params1("SELECT COUNT(id) FROM ztc_network WHERE id = $1", networkId);
  1118. int nwcount = nwrow[0].as<int>();
  1119. if (nwcount != 1) {
  1120. fprintf(stderr, "network %s does not exist. skipping member upsert\n", networkId.c_str());
  1121. w.abort();
  1122. _pool->unborrow(c);
  1123. continue;
  1124. }
  1125. pqxx::row mrow = w.exec_params1("SELECT COUNT(id) FROM ztc_member WHERE id = $1 AND network_id = $2", memberId, networkId);
  1126. int membercount = mrow[0].as<int>();
  1127. bool isNewMember = false;
  1128. if (membercount == 0) {
  1129. // new member
  1130. isNewMember = true;
  1131. pqxx::result res = w.exec_params0(
  1132. "INSERT INTO ztc_member (id, network_id, active_bridge, authorized, capabilities, "
  1133. "identity, last_authorized_time, last_deauthorized_time, no_auto_assign_ips, "
  1134. "remote_trace_level, remote_trace_target, revision, tags, v_major, v_minor, v_rev, v_proto) "
  1135. "VALUES ($1, $2, $3, $4, $5, $6, "
  1136. "TO_TIMESTAMP($7::double precision/1000), TO_TIMESTAMP($8::double precision/1000), "
  1137. "$9, $10, $11, $12, $13, $14, $15, $16, $17)",
  1138. memberId,
  1139. networkId,
  1140. (bool)config["activeBridge"],
  1141. (bool)config["authorized"],
  1142. OSUtils::jsonDump(config["capabilities"], -1),
  1143. OSUtils::jsonString(config["identity"], ""),
  1144. (uint64_t)config["lastAuthorizedTime"],
  1145. (uint64_t)config["lastDeauthorizedTime"],
  1146. (bool)config["noAutoAssignIps"],
  1147. (int)config["remoteTraceLevel"],
  1148. target,
  1149. (uint64_t)config["revision"],
  1150. OSUtils::jsonDump(config["tags"], -1),
  1151. (int)config["vMajor"],
  1152. (int)config["vMinor"],
  1153. (int)config["vRev"],
  1154. (int)config["vProto"]);
  1155. } else {
  1156. // existing member
  1157. pqxx::result res = w.exec_params0(
  1158. "UPDATE ztc_member "
  1159. "SET active_bridge = $3, authorized = $4, capabilities = $5, identity = $6, "
  1160. "last_authorized_time = TO_TIMESTAMP($7::double precision/1000), "
  1161. "last_deauthorized_time = TO_TIMESTAMP($8::double precision/1000), "
  1162. "no_auto_assign_ips = $9, remote_trace_level = $10, remote_trace_target= $11, "
  1163. "revision = $12, tags = $13, v_major = $14, v_minor = $15, v_rev = $16, v_proto = $17 "
  1164. "WHERE id = $1 AND network_id = $2",
  1165. memberId,
  1166. networkId,
  1167. (bool)config["activeBridge"],
  1168. (bool)config["authorized"],
  1169. OSUtils::jsonDump(config["capabilities"], -1),
  1170. OSUtils::jsonString(config["identity"], ""),
  1171. (uint64_t)config["lastAuthorizedTime"],
  1172. (uint64_t)config["lastDeauthorizedTime"],
  1173. (bool)config["noAutoAssignIps"],
  1174. (int)config["remoteTraceLevel"],
  1175. target,
  1176. (uint64_t)config["revision"],
  1177. OSUtils::jsonDump(config["tags"], -1),
  1178. (int)config["vMajor"],
  1179. (int)config["vMinor"],
  1180. (int)config["vRev"],
  1181. (int)config["vProto"]
  1182. );
  1183. }
  1184. if (!isNewMember) {
  1185. pqxx::result res = w.exec_params0("DELETE FROM ztc_member_ip_assignment WHERE member_id = $1 AND network_id = $2",
  1186. memberId, networkId);
  1187. }
  1188. std::vector<std::string> assignments;
  1189. bool ipAssignError = false;
  1190. for (auto i = config["ipAssignments"].begin(); i != config["ipAssignments"].end(); ++i) {
  1191. std::string addr = *i;
  1192. if (std::find(assignments.begin(), assignments.end(), addr) != assignments.end()) {
  1193. continue;
  1194. }
  1195. pqxx::result res = w.exec_params0(
  1196. "INSERT INTO ztc_member_ip_assignment (member_id, network_id, address) VALUES ($1, $2, $3) ON CONFLICT (network_id, member_id, address) DO NOTHING",
  1197. memberId, networkId, addr);
  1198. assignments.push_back(addr);
  1199. }
  1200. if (ipAssignError) {
  1201. fprintf(stderr, "%s: ipAssignError\n", _myAddressStr.c_str());
  1202. w.abort();
  1203. _pool->unborrow(c);
  1204. c.reset();
  1205. continue;
  1206. }
  1207. w.commit();
  1208. if (_smee != NULL && isNewMember) {
  1209. pqxx::row row = w.exec_params1(
  1210. "SELECT "
  1211. " count(h.hook_id) "
  1212. "FROM "
  1213. " ztc_hook h "
  1214. " INNER JOIN ztc_org o ON o.org_id = h.org_id "
  1215. " INNER JOIN ztc_network n ON n.owner_id = o.owner_id "
  1216. " WHERE "
  1217. "n.id = $1 ",
  1218. networkId
  1219. );
  1220. int64_t hookCount = row[0].as<int64_t>();
  1221. if (hookCount > 0) {
  1222. notifyNewMember(networkId, memberId);
  1223. }
  1224. }
  1225. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1226. const uint64_t memberidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
  1227. if (nwidInt && memberidInt) {
  1228. nlohmann::json nwOrig;
  1229. nlohmann::json memOrig;
  1230. nlohmann::json memNew(config);
  1231. get(nwidInt, nwOrig, memberidInt, memOrig);
  1232. _memberChanged(memOrig, memNew, qitem.second);
  1233. } else {
  1234. 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);
  1235. }
  1236. } catch (std::exception &e) {
  1237. fprintf(stderr, "%s ERROR: Error updating member %s-%s: %s\n", _myAddressStr.c_str(), networkId.c_str(), memberId.c_str(), e.what());
  1238. }
  1239. } else if (objtype == "network") {
  1240. try {
  1241. // fprintf(stderr, "%s: commitThread: network\n", _myAddressStr.c_str());
  1242. pqxx::work w(*c->c);
  1243. std::string id = config["id"];
  1244. std::string remoteTraceTarget = "";
  1245. if(!config["remoteTraceTarget"].is_null()) {
  1246. remoteTraceTarget = config["remoteTraceTarget"];
  1247. }
  1248. std::string rulesSource = "";
  1249. if (config["rulesSource"].is_string()) {
  1250. rulesSource = config["rulesSource"];
  1251. }
  1252. // This ugly query exists because when we want to mirror networks to/from
  1253. // another data store (e.g. FileDB or LFDB) it is possible to get a network
  1254. // that doesn't exist in Central's database. This does an upsert and sets
  1255. // the owner_id to the "first" global admin in the user DB if the record
  1256. // did not previously exist. If the record already exists owner_id is left
  1257. // unchanged, so owner_id should be left out of the update clause.
  1258. pqxx::result res = w.exec_params0(
  1259. "INSERT INTO ztc_network (id, creation_time, owner_id, controller_id, capabilities, enable_broadcast, "
  1260. "last_modified, mtu, multicast_limit, name, private, "
  1261. "remote_trace_level, remote_trace_target, rules, rules_source, "
  1262. "tags, v4_assign_mode, v6_assign_mode, sso_enabled) VALUES ("
  1263. "$1, TO_TIMESTAMP($5::double precision/1000), "
  1264. "(SELECT user_id AS owner_id FROM ztc_global_permissions WHERE authorize = true AND del = true AND modify = true AND read = true LIMIT 1),"
  1265. "$2, $3, $4, TO_TIMESTAMP($5::double precision/1000), "
  1266. "$6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) "
  1267. "ON CONFLICT (id) DO UPDATE set controller_id = EXCLUDED.controller_id, "
  1268. "capabilities = EXCLUDED.capabilities, enable_broadcast = EXCLUDED.enable_broadcast, "
  1269. "last_modified = EXCLUDED.last_modified, mtu = EXCLUDED.mtu, "
  1270. "multicast_limit = EXCLUDED.multicast_limit, name = EXCLUDED.name, "
  1271. "private = EXCLUDED.private, remote_trace_level = EXCLUDED.remote_trace_level, "
  1272. "remote_trace_target = EXCLUDED.remote_trace_target, rules = EXCLUDED.rules, "
  1273. "rules_source = EXCLUDED.rules_source, tags = EXCLUDED.tags, "
  1274. "v4_assign_mode = EXCLUDED.v4_assign_mode, v6_assign_mode = EXCLUDED.v6_assign_mode, "
  1275. "sso_enabled = EXCLUDED.sso_enabled",
  1276. id,
  1277. _myAddressStr,
  1278. OSUtils::jsonDump(config["capabilities"], -1),
  1279. (bool)config["enableBroadcast"],
  1280. OSUtils::now(),
  1281. (int)config["mtu"],
  1282. (int)config["multicastLimit"],
  1283. OSUtils::jsonString(config["name"],""),
  1284. (bool)config["private"],
  1285. (int)config["remoteTraceLevel"],
  1286. remoteTraceTarget,
  1287. OSUtils::jsonDump(config["rules"], -1),
  1288. rulesSource,
  1289. OSUtils::jsonDump(config["tags"], -1),
  1290. OSUtils::jsonDump(config["v4AssignMode"],-1),
  1291. OSUtils::jsonDump(config["v6AssignMode"], -1),
  1292. OSUtils::jsonBool(config["ssoEnabled"], false));
  1293. res = w.exec_params0("DELETE FROM ztc_network_assignment_pool WHERE network_id = $1", 0);
  1294. auto pool = config["ipAssignmentPools"];
  1295. bool err = false;
  1296. for (auto i = pool.begin(); i != pool.end(); ++i) {
  1297. std::string start = (*i)["ipRangeStart"];
  1298. std::string end = (*i)["ipRangeEnd"];
  1299. res = w.exec_params0(
  1300. "INSERT INTO ztc_network_assignment_pool (network_id, ip_range_start, ip_range_end) "
  1301. "VALUES ($1, $2, $3)", id, start, end);
  1302. }
  1303. res = w.exec_params0("DELETE FROM ztc_network_route WHERE network_id = $1", id);
  1304. auto routes = config["routes"];
  1305. err = false;
  1306. for (auto i = routes.begin(); i != routes.end(); ++i) {
  1307. std::string t = (*i)["target"];
  1308. std::vector<std::string> target;
  1309. std::istringstream f(t);
  1310. std::string s;
  1311. while(std::getline(f, s, '/')) {
  1312. target.push_back(s);
  1313. }
  1314. if (target.empty() || target.size() != 2) {
  1315. continue;
  1316. }
  1317. std::string targetAddr = target[0];
  1318. std::string targetBits = target[1];
  1319. std::string via = "NULL";
  1320. if (!(*i)["via"].is_null()) {
  1321. via = (*i)["via"];
  1322. }
  1323. res = w.exec_params0("INSERT INTO ztc_network_route (network_id, address, bits, via) VALUES ($1, $2, $3, $4)",
  1324. id, targetAddr, targetBits, (via == "NULL" ? NULL : via.c_str()));
  1325. }
  1326. if (err) {
  1327. fprintf(stderr, "%s: route add error\n", _myAddressStr.c_str());
  1328. w.abort();
  1329. _pool->unborrow(c);
  1330. continue;
  1331. }
  1332. auto dns = config["dns"];
  1333. std::string domain = dns["domain"];
  1334. std::stringstream servers;
  1335. servers << "{";
  1336. for (auto j = dns["servers"].begin(); j < dns["servers"].end(); ++j) {
  1337. servers << *j;
  1338. if ( (j+1) != dns["servers"].end()) {
  1339. servers << ",";
  1340. }
  1341. }
  1342. servers << "}";
  1343. std::string s = servers.str();
  1344. res = w.exec_params0("INSERT INTO ztc_network_dns (network_id, domain, servers) VALUES ($1, $2, $3) ON CONFLICT (network_id) DO UPDATE SET domain = EXCLUDED.domain, servers = EXCLUDED.servers",
  1345. id, domain, s);
  1346. w.commit();
  1347. const uint64_t nwidInt = OSUtils::jsonIntHex(config["nwid"], 0ULL);
  1348. if (nwidInt) {
  1349. nlohmann::json nwOrig;
  1350. nlohmann::json nwNew(config);
  1351. get(nwidInt, nwOrig);
  1352. _networkChanged(nwOrig, nwNew, qitem.second);
  1353. } else {
  1354. fprintf(stderr, "%s: Can't notify network changed: %llu\n", _myAddressStr.c_str(), (unsigned long long)nwidInt);
  1355. }
  1356. } catch (std::exception &e) {
  1357. fprintf(stderr, "%s ERROR: Error updating network: %s\n", _myAddressStr.c_str(), e.what());
  1358. }
  1359. if (_redisMemberStatus) {
  1360. try {
  1361. std::string id = config["id"];
  1362. std::string controllerId = _myAddressStr.c_str();
  1363. std::string key = "networks:{" + controllerId + "}";
  1364. if (_rc->clusterMode) {
  1365. _cluster->sadd(key, id);
  1366. } else {
  1367. _redis->sadd(key, id);
  1368. }
  1369. } catch (sw::redis::Error &e) {
  1370. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1371. }
  1372. }
  1373. } else if (objtype == "_delete_network") {
  1374. // fprintf(stderr, "%s: commitThread: delete network\n", _myAddressStr.c_str());
  1375. try {
  1376. pqxx::work w(*c->c);
  1377. std::string networkId = config["nwid"];
  1378. pqxx::result res = w.exec_params0("UPDATE ztc_network SET deleted = true WHERE id = $1",
  1379. networkId);
  1380. w.commit();
  1381. } catch (std::exception &e) {
  1382. fprintf(stderr, "%s ERROR: Error deleting network: %s\n", _myAddressStr.c_str(), e.what());
  1383. }
  1384. if (_redisMemberStatus) {
  1385. try {
  1386. std::string id = config["id"];
  1387. std::string controllerId = _myAddressStr.c_str();
  1388. std::string key = "networks:{" + controllerId + "}";
  1389. if (_rc->clusterMode) {
  1390. _cluster->srem(key, id);
  1391. _cluster->del("network-nodes-online:{"+controllerId+"}:"+id);
  1392. } else {
  1393. _redis->srem(key, id);
  1394. _redis->del("network-nodes-online:{"+controllerId+"}:"+id);
  1395. }
  1396. } catch (sw::redis::Error &e) {
  1397. fprintf(stderr, "ERROR: Error adding network to Redis: %s\n", e.what());
  1398. }
  1399. }
  1400. } else if (objtype == "_delete_member") {
  1401. // fprintf(stderr, "%s commitThread: delete member\n", _myAddressStr.c_str());
  1402. try {
  1403. pqxx::work w(*c->c);
  1404. std::string memberId = config["id"];
  1405. std::string networkId = config["nwid"];
  1406. pqxx::result res = w.exec_params0(
  1407. "UPDATE ztc_member SET hidden = true, deleted = true WHERE id = $1 AND network_id = $2",
  1408. memberId, networkId);
  1409. w.commit();
  1410. } catch (std::exception &e) {
  1411. fprintf(stderr, "%s ERROR: Error deleting member: %s\n", _myAddressStr.c_str(), e.what());
  1412. }
  1413. if (_redisMemberStatus) {
  1414. try {
  1415. std::string memberId = config["id"];
  1416. std::string networkId = config["nwid"];
  1417. std::string controllerId = _myAddressStr.c_str();
  1418. std::string key = "network-nodes-all:{" + controllerId + "}:" + networkId;
  1419. if (_rc->clusterMode) {
  1420. _cluster->srem(key, memberId);
  1421. _cluster->del("member:{"+controllerId+"}:"+networkId+":"+memberId);
  1422. } else {
  1423. _redis->srem(key, memberId);
  1424. _redis->del("member:{"+controllerId+"}:"+networkId+":"+memberId);
  1425. }
  1426. } catch (sw::redis::Error &e) {
  1427. fprintf(stderr, "ERROR: Error deleting member from Redis: %s\n", e.what());
  1428. }
  1429. }
  1430. } else {
  1431. fprintf(stderr, "%s ERROR: unknown objtype\n", _myAddressStr.c_str());
  1432. }
  1433. } catch (std::exception &e) {
  1434. fprintf(stderr, "%s ERROR: Error getting objtype: %s\n", _myAddressStr.c_str(), e.what());
  1435. }
  1436. _pool->unborrow(c);
  1437. c.reset();
  1438. }
  1439. fprintf(stderr, "%s commitThread finished\n", _myAddressStr.c_str());
  1440. }
  1441. void CV1::notifyNewMember(const std::string &networkID, const std::string &memberID) {
  1442. smeeclient::smee_client_notify_network_joined(
  1443. _smee,
  1444. networkID.c_str(),
  1445. memberID.c_str());
  1446. }
  1447. void CV1::onlineNotificationThread()
  1448. {
  1449. waitForReady();
  1450. if (_redisMemberStatus) {
  1451. onlineNotification_Redis();
  1452. } else {
  1453. onlineNotification_Postgres();
  1454. }
  1455. }
  1456. /**
  1457. * ONLY UNCOMMENT FOR TEMPORARY DB MAINTENANCE
  1458. *
  1459. * This define temporarily turns off writing to the member status table
  1460. * so it can be reindexed when the indexes get too large.
  1461. */
  1462. // #define DISABLE_MEMBER_STATUS 1
  1463. void CV1::onlineNotification_Postgres()
  1464. {
  1465. _connected = 1;
  1466. nlohmann::json jtmp1, jtmp2;
  1467. while (_run == 1) {
  1468. auto c = _pool->borrow();
  1469. auto c2 = _pool->borrow();
  1470. try {
  1471. fprintf(stderr, "%s onlineNotification_Postgres\n", _myAddressStr.c_str());
  1472. std::unordered_map< std::pair<uint64_t,uint64_t>,NodeOnlineRecord,_PairHasher > lastOnline;
  1473. {
  1474. std::lock_guard<std::mutex> l(_lastOnline_l);
  1475. lastOnline.swap(_lastOnline);
  1476. }
  1477. #ifndef DISABLE_MEMBER_STATUS
  1478. pqxx::work w(*c->c);
  1479. pqxx::work w2(*c2->c);
  1480. fprintf(stderr, "online notification tick\n");
  1481. bool firstRun = true;
  1482. bool memberAdded = false;
  1483. int updateCount = 0;
  1484. pqxx::pipeline pipe(w);
  1485. for (auto i=lastOnline.begin(); i != lastOnline.end(); ++i) {
  1486. updateCount += 1;
  1487. uint64_t nwid_i = i->first.first;
  1488. char nwidTmp[64];
  1489. char memTmp[64];
  1490. char ipTmp[64];
  1491. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1492. OSUtils::ztsnprintf(memTmp,sizeof(memTmp), "%.10llx", i->first.second);
  1493. if(!get(nwid_i, jtmp1, i->first.second, jtmp2)) {
  1494. continue; // skip non existent networks/members
  1495. }
  1496. std::string networkId(nwidTmp);
  1497. std::string memberId(memTmp);
  1498. try {
  1499. pqxx::row r = w2.exec_params1("SELECT id, network_id FROM ztc_member WHERE network_id = $1 AND id = $2",
  1500. networkId, memberId);
  1501. } catch (pqxx::unexpected_rows &e) {
  1502. continue;
  1503. }
  1504. int64_t ts = i->second.lastSeen;
  1505. std::string ipAddr = i->second.physicalAddress.toIpString(ipTmp);
  1506. std::string timestamp = std::to_string(ts);
  1507. std::string osArch = i->second.osArch;
  1508. std::stringstream memberUpdate;
  1509. memberUpdate << "INSERT INTO ztc_member_status (network_id, member_id, address, last_updated) VALUES "
  1510. << "('" << networkId << "', '" << memberId << "', ";
  1511. if (ipAddr.empty()) {
  1512. memberUpdate << "NULL, ";
  1513. } else {
  1514. memberUpdate << "'" << ipAddr << "', ";
  1515. }
  1516. memberUpdate << "TO_TIMESTAMP(" << timestamp << "::double precision/1000)) "
  1517. << " ON CONFLICT (network_id, member_id) DO UPDATE SET address = EXCLUDED.address, last_updated = EXCLUDED.last_updated";
  1518. pipe.insert(memberUpdate.str());
  1519. Metrics::pgsql_node_checkin++;
  1520. }
  1521. while(!pipe.empty()) {
  1522. pipe.retrieve();
  1523. }
  1524. pipe.complete();
  1525. w.commit();
  1526. fprintf(stderr, "%s: Updated online status of %d members\n", _myAddressStr.c_str(), updateCount);
  1527. #endif
  1528. } catch (std::exception &e) {
  1529. fprintf(stderr, "%s: error in onlinenotification thread: %s\n", _myAddressStr.c_str(), e.what());
  1530. }
  1531. _pool->unborrow(c2);
  1532. _pool->unborrow(c);
  1533. ConnectionPoolStats stats = _pool->get_stats();
  1534. fprintf(stderr, "%s pool stats: in use size: %llu, available size: %llu, total: %llu\n",
  1535. _myAddressStr.c_str(), stats.borrowed_size, stats.pool_size, (stats.borrowed_size + stats.pool_size));
  1536. std::this_thread::sleep_for(std::chrono::seconds(10));
  1537. }
  1538. fprintf(stderr, "%s: Fell out of run loop in onlineNotificationThread\n", _myAddressStr.c_str());
  1539. if (_run == 1) {
  1540. fprintf(stderr, "ERROR: %s onlineNotificationThread should still be running! Exiting Controller.\n", _myAddressStr.c_str());
  1541. exit(6);
  1542. }
  1543. }
  1544. void CV1::onlineNotification_Redis()
  1545. {
  1546. _connected = 1;
  1547. char buf[11] = {0};
  1548. std::string controllerId = std::string(_myAddress.toString(buf));
  1549. while (_run == 1) {
  1550. fprintf(stderr, "onlineNotification tick\n");
  1551. auto start = std::chrono::high_resolution_clock::now();
  1552. uint64_t count = 0;
  1553. std::unordered_map< std::pair<uint64_t,uint64_t>,NodeOnlineRecord,_PairHasher > lastOnline;
  1554. {
  1555. std::lock_guard<std::mutex> l(_lastOnline_l);
  1556. lastOnline.swap(_lastOnline);
  1557. }
  1558. try {
  1559. if (!lastOnline.empty()) {
  1560. if (_rc->clusterMode) {
  1561. auto tx = _cluster->transaction(controllerId, true, false);
  1562. count = _doRedisUpdate(tx, controllerId, lastOnline);
  1563. } else {
  1564. auto tx = _redis->transaction(true, false);
  1565. count = _doRedisUpdate(tx, controllerId, lastOnline);
  1566. }
  1567. }
  1568. } catch (sw::redis::Error &e) {
  1569. fprintf(stderr, "Error in online notification thread (redis): %s\n", e.what());
  1570. }
  1571. auto end = std::chrono::high_resolution_clock::now();
  1572. auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
  1573. auto total = dur.count();
  1574. fprintf(stderr, "onlineNotification ran in %llu ms\n", total);
  1575. std::this_thread::sleep_for(std::chrono::seconds(5));
  1576. }
  1577. }
  1578. uint64_t CV1::_doRedisUpdate(sw::redis::Transaction &tx, std::string &controllerId,
  1579. std::unordered_map< std::pair<uint64_t,uint64_t>,NodeOnlineRecord,_PairHasher > &lastOnline)
  1580. {
  1581. nlohmann::json jtmp1, jtmp2;
  1582. uint64_t count = 0;
  1583. for (auto i=lastOnline.begin(); i != lastOnline.end(); ++i) {
  1584. uint64_t nwid_i = i->first.first;
  1585. uint64_t memberid_i = i->first.second;
  1586. char nwidTmp[64];
  1587. char memTmp[64];
  1588. char ipTmp[64];
  1589. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1590. OSUtils::ztsnprintf(memTmp,sizeof(memTmp), "%.10llx", memberid_i);
  1591. if (!get(nwid_i, jtmp1, memberid_i, jtmp2)){
  1592. continue; // skip non existent members/networks
  1593. }
  1594. std::string networkId(nwidTmp);
  1595. std::string memberId(memTmp);
  1596. int64_t ts = i->second.lastSeen;
  1597. std::string ipAddr = i->second.physicalAddress.toIpString(ipTmp);
  1598. std::string timestamp = std::to_string(ts);
  1599. std::string osArch = i->second.osArch;
  1600. std::unordered_map<std::string, std::string> record = {
  1601. {"id", memberId},
  1602. {"address", ipAddr},
  1603. {"last_updated", std::to_string(ts)}
  1604. };
  1605. tx.zadd("nodes-online:{"+controllerId+"}", memberId, ts)
  1606. .zadd("nodes-online2:{"+controllerId+"}", networkId+"-"+memberId, ts)
  1607. .zadd("network-nodes-online:{"+controllerId+"}:"+networkId, memberId, ts)
  1608. .zadd("active-networks:{"+controllerId+"}", networkId, ts)
  1609. .sadd("network-nodes-all:{"+controllerId+"}:"+networkId, memberId)
  1610. .hmset("member:{"+controllerId+"}:"+networkId+":"+memberId, record.begin(), record.end());
  1611. ++count;
  1612. Metrics::redis_node_checkin++;
  1613. }
  1614. // expire records from all-nodes and network-nodes member list
  1615. uint64_t expireOld = OSUtils::now() - 300000;
  1616. tx.zremrangebyscore("nodes-online:{"+controllerId+"}",
  1617. sw::redis::RightBoundedInterval<double>(expireOld,
  1618. sw::redis::BoundType::LEFT_OPEN));
  1619. tx.zremrangebyscore("nodes-online2:{"+controllerId+"}",
  1620. sw::redis::RightBoundedInterval<double>(expireOld,
  1621. sw::redis::BoundType::LEFT_OPEN));
  1622. tx.zremrangebyscore("active-networks:{"+controllerId+"}",
  1623. sw::redis::RightBoundedInterval<double>(expireOld,
  1624. sw::redis::BoundType::LEFT_OPEN));
  1625. {
  1626. std::shared_lock<std::shared_mutex> l(_networks_l);
  1627. for (const auto &it : _networks) {
  1628. uint64_t nwid_i = it.first;
  1629. char nwidTmp[64];
  1630. OSUtils::ztsnprintf(nwidTmp,sizeof(nwidTmp), "%.16llx", nwid_i);
  1631. tx.zremrangebyscore("network-nodes-online:{"+controllerId+"}:"+nwidTmp,
  1632. sw::redis::RightBoundedInterval<double>(expireOld, sw::redis::BoundType::LEFT_OPEN));
  1633. }
  1634. }
  1635. tx.exec();
  1636. fprintf(stderr, "%s: Updated online status of %d members\n", _myAddressStr.c_str(), count);
  1637. return count;
  1638. }
  1639. #endif //ZT_CONTROLLER_USE_LIBPQ