소스 검색

Refactor controller for new merged format.

Adam Ierymenko 9 년 전
부모
커밋
20d4dada40
2개의 변경된 파일51개의 추가작업 그리고 69개의 파일을 삭제
  1. 47 65
      controller/SqliteNetworkController.cpp
  2. 4 4
      controller/SqliteNetworkController.hpp

+ 47 - 65
controller/SqliteNetworkController.cpp

@@ -53,7 +53,7 @@
 #include "../node/Utils.hpp"
 #include "../node/Utils.hpp"
 #include "../node/CertificateOfMembership.hpp"
 #include "../node/CertificateOfMembership.hpp"
 #include "../node/NetworkConfig.hpp"
 #include "../node/NetworkConfig.hpp"
-#include "../node/NetworkConfigRequestMetaData.hpp"
+#include "../node/Dictionary.hpp"
 #include "../node/InetAddress.hpp"
 #include "../node/InetAddress.hpp"
 #include "../node/MAC.hpp"
 #include "../node/MAC.hpp"
 #include "../node/Address.hpp"
 #include "../node/Address.hpp"
@@ -115,17 +115,13 @@ static void _ipToBlob(const InetAddress &a,char *ipBlob,int &ipVersion) /* blob[
 {
 {
 	switch(a.ss_family) {
 	switch(a.ss_family) {
 		case AF_INET:
 		case AF_INET:
-			if ((a.netmaskBits() >= 0)&&(a.netmaskBits() <= 32)) {
-				memset(ipBlob,0,12);
-				memcpy(ipBlob + 12,a.rawIpData(),4);
-				ipVersion = 4;
-			}
+			memset(ipBlob,0,12);
+			memcpy(ipBlob + 12,a.rawIpData(),4);
+			ipVersion = 4;
 			break;
 			break;
 		case AF_INET6:
 		case AF_INET6:
-			if ((a.netmaskBits() >= 0)&&(a.netmaskBits() <= 128)) {
-				memcpy(ipBlob,a.rawIpData(),16);
-				ipVersion = 6;
-			}
+			memcpy(ipBlob,a.rawIpData(),16);
+			ipVersion = 6;
 			break;
 			break;
 	}
 	}
 }
 }
@@ -406,10 +402,10 @@ SqliteNetworkController::~SqliteNetworkController()
 	}
 	}
 }
 }
 
 
-NetworkController::ResultCode SqliteNetworkController::doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const NetworkConfigRequestMetaData &metaData,Buffer<8194> &netconf)
+NetworkController::ResultCode SqliteNetworkController::doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const Dictionary &metaData,NetworkConfig &nc)
 {
 {
 	Mutex::Lock _l(_lock);
 	Mutex::Lock _l(_lock);
-	return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,netconf);
+	return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,nc);
 }
 }
 
 
 unsigned int SqliteNetworkController::handleControlPlaneHttpGET(
 unsigned int SqliteNetworkController::handleControlPlaneHttpGET(
@@ -1580,7 +1576,7 @@ unsigned int SqliteNetworkController::_doCPGet(
 	return 404;
 	return 404;
 }
 }
 
 
-NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const NetworkConfigRequestMetaData &metaData,Buffer<8194> &netconf)
+NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(const InetAddress &fromAddr,const Identity &signingId,const Identity &identity,uint64_t nwid,const Dictionary &metaData,NetworkConfig &nc)
 {
 {
 	// Assumes _lock is locked
 	// Assumes _lock is locked
 
 
@@ -1705,10 +1701,10 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 		sqlite3_bind_int64(_sAddNodeHistoryEntry,3,nextVC);
 		sqlite3_bind_int64(_sAddNodeHistoryEntry,3,nextVC);
 		sqlite3_bind_int(_sAddNodeHistoryEntry,4,(member.authorized ? 1 : 0));
 		sqlite3_bind_int(_sAddNodeHistoryEntry,4,(member.authorized ? 1 : 0));
 		sqlite3_bind_int64(_sAddNodeHistoryEntry,5,(long long)now);
 		sqlite3_bind_int64(_sAddNodeHistoryEntry,5,(long long)now);
-		sqlite3_bind_int(_sAddNodeHistoryEntry,6,(int)metaData.majorVersion);
-		sqlite3_bind_int(_sAddNodeHistoryEntry,7,(int)metaData.minorVersion);
-		sqlite3_bind_int(_sAddNodeHistoryEntry,8,(int)metaData.revision);
-		sqlite3_bind_text(_sAddNodeHistoryEntry,9,"",-1,SQLITE_STATIC);
+		sqlite3_bind_int(_sAddNodeHistoryEntry,6,(int)metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,0));
+		sqlite3_bind_int(_sAddNodeHistoryEntry,7,(int)metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,0));
+		sqlite3_bind_int(_sAddNodeHistoryEntry,8,(int)metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,0));
+		sqlite3_bind_text(_sAddNodeHistoryEntry,9,metaData.data(),-1,SQLITE_STATIC);
 		if (fastr.length() > 0)
 		if (fastr.length() > 0)
 			sqlite3_bind_text(_sAddNodeHistoryEntry,10,fastr.c_str(),-1,SQLITE_STATIC);
 			sqlite3_bind_text(_sAddNodeHistoryEntry,10,fastr.c_str(),-1,SQLITE_STATIC);
 		else sqlite3_bind_null(_sAddNodeHistoryEntry,10);
 		else sqlite3_bind_null(_sAddNodeHistoryEntry,10);
@@ -1731,9 +1727,6 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 
 
 	// Create network configuration -- we create both legacy and new types and send both for backward compatibility
 	// Create network configuration -- we create both legacy and new types and send both for backward compatibility
 
 
-	Dictionary legacy;
-	NetworkConfig nc;
-
 	// New network config structure
 	// New network config structure
 	nc.networkId = Utils::hexStrToU64(network.id);
 	nc.networkId = Utils::hexStrToU64(network.id);
 	nc.type = network.isPrivate ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
 	nc.type = network.isPrivate ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
@@ -1744,7 +1737,7 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 	if (network.allowPassiveBridging) nc.flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING;
 	if (network.allowPassiveBridging) nc.flags |= ZT_NETWORKCONFIG_FLAG_ALLOW_PASSIVE_BRIDGING;
 	memcpy(nc.name,network.name,std::min((unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH,(unsigned int)strlen(network.name)));
 	memcpy(nc.name,network.name,std::min((unsigned int)ZT_MAX_NETWORK_SHORT_NAME_LENGTH,(unsigned int)strlen(network.name)));
 
 
-	// Legacy dictionary -- will go away eventually
+	/*
 	char tss[24],rs[24];
 	char tss[24],rs[24];
 	Utils::snprintf(tss,sizeof(tss),"%.16llx",(unsigned long long)now);
 	Utils::snprintf(tss,sizeof(tss),"%.16llx",(unsigned long long)now);
 	Utils::snprintf(rs,sizeof(rs),"%.16llx",(unsigned long long)network.revision);
 	Utils::snprintf(rs,sizeof(rs),"%.16llx",(unsigned long long)network.revision);
@@ -1756,6 +1749,7 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_NAME] = (network.name) ? network.name : "";
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_NAME] = (network.name) ? network.name : "";
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = network.enableBroadcast ? "1" : "0";
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = network.enableBroadcast ? "1" : "0";
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = network.allowPassiveBridging ? "1" : "0";
 	legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = network.allowPassiveBridging ? "1" : "0";
+	*/
 
 
 	{	// TODO: right now only etherTypes are supported in rules
 	{	// TODO: right now only etherTypes are supported in rules
 		std::vector<int> allowedEtherTypes;
 		std::vector<int> allowedEtherTypes;
@@ -1784,6 +1778,7 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 			nc.rules[k++].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
 			nc.rules[k++].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
 		}
 		}
 
 
+		/*
 		std::string allowedEtherTypesCsv;
 		std::string allowedEtherTypesCsv;
 		for(std::vector<int>::const_iterator i(allowedEtherTypes.begin());i!=allowedEtherTypes.end();++i) {
 		for(std::vector<int>::const_iterator i(allowedEtherTypes.begin());i!=allowedEtherTypes.end();++i) {
 			if (allowedEtherTypesCsv.length())
 			if (allowedEtherTypesCsv.length())
@@ -1793,46 +1788,49 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 			allowedEtherTypesCsv.append(tmp);
 			allowedEtherTypesCsv.append(tmp);
 		}
 		}
 		legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = allowedEtherTypesCsv;
 		legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = allowedEtherTypesCsv;
+		*/
 	}
 	}
 
 
 	nc.multicastLimit = network.multicastLimit;
 	nc.multicastLimit = network.multicastLimit;
+	/*
 	if (network.multicastLimit > 0) {
 	if (network.multicastLimit > 0) {
 		char ml[16];
 		char ml[16];
 		Utils::snprintf(ml,sizeof(ml),"%lx",(unsigned long)network.multicastLimit);
 		Utils::snprintf(ml,sizeof(ml),"%lx",(unsigned long)network.multicastLimit);
 		legacy[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT] = ml;
 		legacy[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT] = ml;
 	}
 	}
+	*/
 
 
 	bool amActiveBridge = false;
 	bool amActiveBridge = false;
 	{
 	{
-		std::string activeBridges;
+		//std::string activeBridges;
 		sqlite3_reset(_sGetActiveBridges);
 		sqlite3_reset(_sGetActiveBridges);
 		sqlite3_bind_text(_sGetActiveBridges,1,network.id,16,SQLITE_STATIC);
 		sqlite3_bind_text(_sGetActiveBridges,1,network.id,16,SQLITE_STATIC);
 		while (sqlite3_step(_sGetActiveBridges) == SQLITE_ROW) {
 		while (sqlite3_step(_sGetActiveBridges) == SQLITE_ROW) {
 			const char *ab = (const char *)sqlite3_column_text(_sGetActiveBridges,0);
 			const char *ab = (const char *)sqlite3_column_text(_sGetActiveBridges,0);
 			if ((ab)&&(strlen(ab) == 10)) {
 			if ((ab)&&(strlen(ab) == 10)) {
 				const uint64_t ab2 = Utils::hexStrToU64(ab);
 				const uint64_t ab2 = Utils::hexStrToU64(ab);
-				if ((ab2)&&(nc.specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
-					nc.specialists[nc.specialistCount++] = ab2 | ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE;
-				}
+				nc.addSpecialist(Address(ab2),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
 
 
+				/*
 				if (activeBridges.length())
 				if (activeBridges.length())
 					activeBridges.push_back(',');
 					activeBridges.push_back(',');
 				activeBridges.append(ab);
 				activeBridges.append(ab);
+				*/
 
 
 				if (!strcmp(member.nodeId,ab))
 				if (!strcmp(member.nodeId,ab))
 					amActiveBridge = true;
 					amActiveBridge = true;
 			}
 			}
-			if (activeBridges.length() > 1024) // sanity check -- you can't have too many active bridges at the moment
-				break;
 		}
 		}
+		/*
 		if (activeBridges.length())
 		if (activeBridges.length())
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridges;
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridges;
+		*/
 	}
 	}
 
 
 	// Do not send relays to 1.1.0 since it had a serious bug in using them
 	// Do not send relays to 1.1.0 since it had a serious bug in using them
 	// 1.1.0 will still work, it'll just fall back to roots instead of using network preferred relays
 	// 1.1.0 will still work, it'll just fall back to roots instead of using network preferred relays
-	if (!((metaData.majorVersion == 1)&&(metaData.minorVersion == 1)&&(metaData.revision == 0))) {
-		std::string relays;
+	if (!((metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MAJOR_VERSION,0) == 1)&&(metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_MINOR_VERSION,0) == 1)&&(metaData.getUI(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_NODE_REVISION,0) == 0))) {
+		//std::string relays;
 		sqlite3_reset(_sGetRelays);
 		sqlite3_reset(_sGetRelays);
 		sqlite3_bind_text(_sGetRelays,1,network.id,16,SQLITE_STATIC);
 		sqlite3_bind_text(_sGetRelays,1,network.id,16,SQLITE_STATIC);
 		while (sqlite3_step(_sGetRelays) == SQLITE_ROW) {
 		while (sqlite3_step(_sGetRelays) == SQLITE_ROW) {
@@ -1842,18 +1840,8 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 				Address node(n);
 				Address node(n);
 				InetAddress addr(a);
 				InetAddress addr(a);
 				if (node) {
 				if (node) {
-					bool sf = false;
-					for(unsigned int k=0;k<nc.specialistCount;++k) {
-						if ((nc.specialists[k] & 0xffffffffffULL) == node.toInt()) {
-							nc.specialists[k] |= ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY;
-							sf = true;
-							break;
-						}
-					}
-					if ((!sf)&&(nc.specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
-						nc.specialists[nc.specialistCount++] = node.toInt() | ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY;
-					}
-
+					nc.addSpecialist(node,ZT_NETWORKCONFIG_SPECIALIST_TYPE_NETWORK_PREFERRED_RELAY);
+					/*
 					if (relays.length())
 					if (relays.length())
 						relays.push_back(',');
 						relays.push_back(',');
 					relays.append(node.toString());
 					relays.append(node.toString());
@@ -1861,11 +1849,14 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 						relays.push_back(';');
 						relays.push_back(';');
 						relays.append(addr.toString());
 						relays.append(addr.toString());
 					}
 					}
+					*/
 				}
 				}
 			}
 			}
 		}
 		}
+		/*
 		if (relays.length())
 		if (relays.length())
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_RELAYS] = relays;
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_RELAYS] = relays;
+		*/
 	}
 	}
 
 
 	sqlite3_reset(_sGetRoutes);
 	sqlite3_reset(_sGetRoutes);
@@ -1900,8 +1891,15 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 		++nc.routeCount;
 		++nc.routeCount;
 	}
 	}
 
 
+	if ((network.v6AssignMode)&&(!strcmp(network.v6AssignMode,"rfc4193"))) {
+		InetAddress rfc4193Addr(InetAddress::makeIpv6rfc4193(nwid,identity.address().toInt()));
+		//legacy[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = rfc4193Addr.toString();
+		if (nc.staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)
+			nc.staticIps[nc.staticIpCount++] = rfc4193Addr;
+	}
+
 	if ((network.v4AssignMode)&&(!strcmp(network.v4AssignMode,"zt"))) {
 	if ((network.v4AssignMode)&&(!strcmp(network.v4AssignMode,"zt"))) {
-		std::string v4s;
+		//std::string v4s;
 		bool haveStaticIpAssignment = false;
 		bool haveStaticIpAssignment = false;
 
 
 		sqlite3_reset(_sGetIpAssignmentsForNode);
 		sqlite3_reset(_sGetIpAssignmentsForNode);
@@ -1924,9 +1922,11 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 				InetAddress tmp2(ips);
 				InetAddress tmp2(ips);
 				if (tmp2)
 				if (tmp2)
 					nc.staticIps[nc.staticIpCount++] = tmp2;
 					nc.staticIps[nc.staticIpCount++] = tmp2;
+				/*
 				if (v4s.length())
 				if (v4s.length())
 					v4s.push_back(',');
 					v4s.push_back(',');
 				v4s.append(ips);
 				v4s.append(ips);
+				*/
 			}
 			}
 		}
 		}
 
 
@@ -1999,9 +1999,11 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 										nc.staticIps[nc.staticIpCount++] = tmp2;
 										nc.staticIps[nc.staticIpCount++] = tmp2;
 								}
 								}
 
 
+								/*
 								if (v4s.length())
 								if (v4s.length())
 									v4s.push_back(',');
 									v4s.push_back(',');
 								v4s.append(ips);
 								v4s.append(ips);
+								*/
 
 
 								haveStaticIpAssignment = true;
 								haveStaticIpAssignment = true;
 								break;
 								break;
@@ -2012,42 +2014,22 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c
 			}
 			}
 		}
 		}
 
 
+		/*
 		if (v4s.length())
 		if (v4s.length())
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
 			legacy[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
-	}
-
-	if ((network.v6AssignMode)&&(!strcmp(network.v6AssignMode,"rfc4193"))) {
-		InetAddress rfc4193Addr(InetAddress::makeIpv6rfc4193(nwid,identity.address().toInt()));
-		legacy[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = rfc4193Addr.toString();
-		if (nc.staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)
-			nc.staticIps[nc.staticIpCount++] = rfc4193Addr;
+		*/
 	}
 	}
 
 
 	if (network.isPrivate) {
 	if (network.isPrivate) {
 		CertificateOfMembership com(now,ZT_NETWORK_COM_DEFAULT_REVISION_MAX_DELTA,nwid,identity.address());
 		CertificateOfMembership com(now,ZT_NETWORK_COM_DEFAULT_REVISION_MAX_DELTA,nwid,identity.address());
 		if (com.sign(signingId)) {
 		if (com.sign(signingId)) {
-			legacy[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
+			//legacy[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
 			nc.com = com;
 			nc.com = com;
 		} else {
 		} else {
 			return NETCONF_QUERY_INTERNAL_SERVER_ERROR;
 			return NETCONF_QUERY_INTERNAL_SERVER_ERROR;
 		}
 		}
 	}
 	}
 
 
-	// Append legacy network config data for older devices
-	if (metaData.protocolVersion < 6) {
-		if (!legacy.sign(signingId,now))
-			return NETCONF_QUERY_INTERNAL_SERVER_ERROR;
-		std::string legacyStr(legacy.toString());
-		netconf.append((const void *)legacyStr.data(),(unsigned int)legacyStr.length());
-	}
-
-	netconf.append((uint8_t)0);
-
-	// Append new format data for newer devices
-	if (metaData.protocolVersion >= 6) {
-		nc.serialize(netconf);
-	}
-
 	return NetworkController::NETCONF_QUERY_OK;
 	return NetworkController::NETCONF_QUERY_OK;
 }
 }
 
 

+ 4 - 4
controller/SqliteNetworkController.hpp

@@ -62,8 +62,8 @@ public:
 		const Identity &signingId,
 		const Identity &signingId,
 		const Identity &identity,
 		const Identity &identity,
 		uint64_t nwid,
 		uint64_t nwid,
-		const NetworkConfigRequestMetaData &metaData,
-		Buffer<8194> &netconf);
+		const Dictionary &metaData,
+		NetworkConfig &nc);
 
 
 	unsigned int handleControlPlaneHttpGET(
 	unsigned int handleControlPlaneHttpGET(
 		const std::vector<std::string> &path,
 		const std::vector<std::string> &path,
@@ -113,8 +113,8 @@ private:
 		const Identity &signingId,
 		const Identity &signingId,
 		const Identity &identity,
 		const Identity &identity,
 		uint64_t nwid,
 		uint64_t nwid,
-		const NetworkConfigRequestMetaData &metaData,
-		Buffer<8194> &netconf);
+		const Dictionary &metaData,
+		NetworkConfig &nc);
 
 
 	static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);
 	static void _circuitTestCallback(ZT_Node *node,ZT_CircuitTest *test,const ZT_CircuitTestReport *report);