浏览代码

debug output for IP addressing & fixing order of operations in a couple of places. Only send notification of a change to pubsub after it's been written to the DB

Grant Limberg 2 周之前
父节点
当前提交
fe221b9359
共有 3 个文件被更改,包括 55 次插入29 次删除
  1. 30 24
      nonfree/controller/CentralDB.cpp
  2. 15 1
      nonfree/controller/EmbeddedNetworkController.cpp
  3. 10 4
      nonfree/controller/PubSubListener.cpp

+ 30 - 24
nonfree/controller/CentralDB.cpp

@@ -1189,25 +1189,18 @@ void CentralDB::commitThread()
 						change_source = config["change_source"];
 					}
 					if (! isNewMember && change_source != "controller" && frontend != change_source) {
+						fprintf(
+							stderr, "skipping member %s-%s update.  change source: %s, frontend: %s\n",
+							networkId.c_str(), memberId.c_str(), change_source.c_str(), frontend.c_str());
 						// if it is not a new member and the change source is not the controller and doesn't match the
 						// frontend, don't apply the change.
 						continue;
 					}
 
-					if (_listenerMode == LISTENER_MODE_PUBSUB) {
-						// Publish change to pubsub stream
-
-						if (config["change_source"].is_null() || config["change_source"] == "controller") {
-							nlohmann::json oldMember;
-							nlohmann::json newMember = config;
-							if (! isNewMember) {
-								oldMember = _getNetworkMember(w, networkId, memberId);
-							}
-							_changeNotifier->notifyMemberChange(oldMember, newMember, frontend);
-						}
-					}
-
 					std::vector<std::string> ipAssignments;
+					fprintf(
+						stderr, "Saving IP Assignments: \n\tipAssignments: %s\n",
+						OSUtils::jsonDump(config["ipAssignments"], -1).c_str());
 					if (config["ipAssignments"].is_array()) {
 						for (auto& ip : config["ipAssignments"]) {
 							if (ip.is_string()) {
@@ -1282,6 +1275,19 @@ void CentralDB::commitThread()
 
 					w.commit();
 
+					if (_listenerMode == LISTENER_MODE_PUBSUB) {
+						// Publish change to pubsub stream
+
+						if (config["change_source"].is_null() || config["change_source"] == "controller") {
+							nlohmann::json oldMember;
+							nlohmann::json newMember = config;
+							if (! isNewMember) {
+								oldMember = _getNetworkMember(w, networkId, memberId);
+							}
+							_changeNotifier->notifyMemberChange(oldMember, newMember, frontend);
+						}
+					}
+
 					if (_smee != NULL && isNewMember) {
 						// TODO: Smee Notifications for New Members
 						// pqxx::row row = w.exec_params1(
@@ -1363,6 +1369,17 @@ void CentralDB::commitThread()
 						continue;
 					}
 
+					pqxx::result res = w.exec(
+						"INSERT INTO networks_ctl (id, name, configuration, controller_id, revision, frontend) "
+						"VALUES ($1, $2, $3, $4, $5, $6) "
+						"ON CONFLICT (id) DO UPDATE SET "
+						"name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1, "
+						"frontend = EXCLUDED.frontend",
+						pqxx::params { id, OSUtils::jsonString(config["name"], ""), OSUtils::jsonDump(config, -1),
+									   _myAddressStr, ((uint64_t)config["revision"]), change_source });
+
+					w.commit();
+
 					if (_listenerMode == LISTENER_MODE_PUBSUB) {
 						// Publish change to pubsub stream
 						if (config["change_source"].is_null() || config["change_source"] == "controller") {
@@ -1375,17 +1392,6 @@ void CentralDB::commitThread()
 						}
 					}
 
-					pqxx::result res = w.exec(
-						"INSERT INTO networks_ctl (id, name, configuration, controller_id, revision, frontend) "
-						"VALUES ($1, $2, $3, $4, $5, $6) "
-						"ON CONFLICT (id) DO UPDATE SET "
-						"name = EXCLUDED.name, configuration = EXCLUDED.configuration, revision = EXCLUDED.revision+1, "
-						"frontend = EXCLUDED.frontend",
-						pqxx::params { id, OSUtils::jsonString(config["name"], ""), OSUtils::jsonDump(config, -1),
-									   _myAddressStr, ((uint64_t)config["revision"]), change_source });
-
-					w.commit();
-
 					const uint64_t nwidInt = OSUtils::jsonIntHex(config["id"], 0ULL);
 					if (nwidInt) {
 						nlohmann::json nwOrig;

+ 15 - 1
nonfree/controller/EmbeddedNetworkController.cpp

@@ -2301,10 +2301,20 @@ void EmbeddedNetworkController::_request(
 		}
 	}
 
+	fprintf(stderr, "haveManagedIpv4AutoAssignment=%s\n", haveManagedIpv4AutoAssignment ? "true" : "false");
+	fprintf(stderr, "ipAssignmentPools.is_array()=%s\n", ipAssignmentPools.is_array() ? "true" : "false");
+	fprintf(stderr, "v4AssignMode.is_object()=%s\n", v4AssignMode.is_object() ? "true" : "false");
+	fprintf(
+		stderr, "v4AssignMode[\"zt\"]=%s\n",
+		(v4AssignMode.is_object() && OSUtils::jsonBool(v4AssignMode["zt"], false)) ? "true" : "false");
+	fprintf(stderr, "noAutoAssignIps=%s\n", noAutoAssignIps ? "true" : "false");
+
 	if ((ipAssignmentPools.is_array()) && ((v4AssignMode.is_object()) && (OSUtils::jsonBool(v4AssignMode["zt"], false)))
 		&& (! haveManagedIpv4AutoAssignment) && (! noAutoAssignIps)) {
+		fprintf(stderr, "Trying to do managed IPv4 auto-assignment\n");
 		for (unsigned long p = 0; ((p < ipAssignmentPools.size()) && (! haveManagedIpv4AutoAssignment)); ++p) {
 			json& pool = ipAssignmentPools[p];
+			fprintf(stderr, "  considering pool %lu: %s\n", p, OSUtils::jsonDump(pool, 2).c_str());
 			if (pool.is_object()) {
 				InetAddress ipRangeStartIA(OSUtils::jsonString(pool["ipRangeStart"], "").c_str());
 				InetAddress ipRangeEndIA(OSUtils::jsonString(pool["ipRangeEnd"], "").c_str());
@@ -2314,8 +2324,10 @@ void EmbeddedNetworkController::_request(
 					uint32_t ipRangeEnd =
 						Utils::ntoh((uint32_t)(reinterpret_cast<struct sockaddr_in*>(&ipRangeEndIA)->sin_addr.s_addr));
 
-					if ((ipRangeEnd < ipRangeStart) || (ipRangeStart == 0))
+					if ((ipRangeEnd < ipRangeStart) || (ipRangeStart == 0)) {
+						fprintf(stderr, "  bad ip range range\n");
 						continue;
+					}
 					uint32_t ipRangeLen = ipRangeEnd - ipRangeStart;
 
 					// Start with the LSB of the member's address
@@ -2353,6 +2365,7 @@ void EmbeddedNetworkController::_request(
 							char tmpip[64];
 							const std::string ipStr(ip4.toIpString(tmpip));
 							if (std::find(ipAssignments.begin(), ipAssignments.end(), ipStr) == ipAssignments.end()) {
+								fprintf(stderr, "  assigning %s\n", ipStr.c_str());
 								ipAssignments.push_back(ipStr);
 								member["ipAssignments"] = ipAssignments;
 								if (nc->staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
@@ -2426,6 +2439,7 @@ void EmbeddedNetworkController::_request(
 	c10++;
 	b10.start();
 #endif
+	member["change_source"] = "controller";
 	DB::cleanMember(member);
 	_db.save(member, true);
 #ifdef CENTRAL_CONTROLLER_REQUEST_BENCHMARK

+ 10 - 4
nonfree/controller/PubSubListener.cpp

@@ -358,11 +358,17 @@ nlohmann::json toJson(const pbmessages::NetworkChange_Network& nc, pbmessages::N
 		nlohmann::json routes = nlohmann::json::array();
 		for (const auto& r : nc.routes()) {
 			nlohmann::json route;
-			route["target"] = r.target();
-			if (r.has_via()) {
-				route["via"] = r.via();
+			std::string target = r.target();
+			if (target.length() > 0) {
+				route["target"] = r.target();
+				if (r.has_via()) {
+					route["via"] = r.via();
+				}
+				else {
+					route["via"] = nullptr;
+				}
+				routes.push_back(route);
 			}
-			routes.push_back(route);
 		}
 		out["routes"] = routes;
 	}