From 20d4dada404ae6dfe9534721376403bb1efcb490 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Thu, 16 Jun 2016 16:05:57 -0700 Subject: [PATCH] Refactor controller for new merged format. --- controller/SqliteNetworkController.cpp | 112 +++++++++++-------------- controller/SqliteNetworkController.hpp | 8 +- 2 files changed, 51 insertions(+), 69 deletions(-) diff --git a/controller/SqliteNetworkController.cpp b/controller/SqliteNetworkController.cpp index 0c20c1c2d..eee9b7c74 100644 --- a/controller/SqliteNetworkController.cpp +++ b/controller/SqliteNetworkController.cpp @@ -53,7 +53,7 @@ #include "../node/Utils.hpp" #include "../node/CertificateOfMembership.hpp" #include "../node/NetworkConfig.hpp" -#include "../node/NetworkConfigRequestMetaData.hpp" +#include "../node/Dictionary.hpp" #include "../node/InetAddress.hpp" #include "../node/MAC.hpp" #include "../node/Address.hpp" @@ -115,17 +115,13 @@ static void _ipToBlob(const InetAddress &a,char *ipBlob,int &ipVersion) /* blob[ { switch(a.ss_family) { 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; 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; } } @@ -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); - return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,netconf); + return _doNetworkConfigRequest(fromAddr,signingId,identity,nwid,metaData,nc); } unsigned int SqliteNetworkController::handleControlPlaneHttpGET( @@ -1580,7 +1576,7 @@ unsigned int SqliteNetworkController::_doCPGet( 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 @@ -1705,10 +1701,10 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c sqlite3_bind_int64(_sAddNodeHistoryEntry,3,nextVC); sqlite3_bind_int(_sAddNodeHistoryEntry,4,(member.authorized ? 1 : 0)); 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) sqlite3_bind_text(_sAddNodeHistoryEntry,10,fastr.c_str(),-1,SQLITE_STATIC); 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 - Dictionary legacy; - NetworkConfig nc; - // New network config structure nc.networkId = Utils::hexStrToU64(network.id); 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; 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]; Utils::snprintf(tss,sizeof(tss),"%.16llx",(unsigned long long)now); 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_ENABLE_BROADCAST] = network.enableBroadcast ? "1" : "0"; legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = network.allowPassiveBridging ? "1" : "0"; + */ { // TODO: right now only etherTypes are supported in rules std::vector allowedEtherTypes; @@ -1784,6 +1778,7 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c nc.rules[k++].t = ZT_NETWORK_RULE_ACTION_ACCEPT; } + /* std::string allowedEtherTypesCsv; for(std::vector::const_iterator i(allowedEtherTypes.begin());i!=allowedEtherTypes.end();++i) { if (allowedEtherTypesCsv.length()) @@ -1793,46 +1788,49 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c allowedEtherTypesCsv.append(tmp); } legacy[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = allowedEtherTypesCsv; + */ } nc.multicastLimit = network.multicastLimit; + /* if (network.multicastLimit > 0) { char ml[16]; Utils::snprintf(ml,sizeof(ml),"%lx",(unsigned long)network.multicastLimit); legacy[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT] = ml; } + */ bool amActiveBridge = false; { - std::string activeBridges; + //std::string activeBridges; sqlite3_reset(_sGetActiveBridges); sqlite3_bind_text(_sGetActiveBridges,1,network.id,16,SQLITE_STATIC); while (sqlite3_step(_sGetActiveBridges) == SQLITE_ROW) { const char *ab = (const char *)sqlite3_column_text(_sGetActiveBridges,0); if ((ab)&&(strlen(ab) == 10)) { 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()) activeBridges.push_back(','); activeBridges.append(ab); + */ if (!strcmp(member.nodeId,ab)) amActiveBridge = true; } - if (activeBridges.length() > 1024) // sanity check -- you can't have too many active bridges at the moment - break; } + /* if (activeBridges.length()) 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 // 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_bind_text(_sGetRelays,1,network.id,16,SQLITE_STATIC); while (sqlite3_step(_sGetRelays) == SQLITE_ROW) { @@ -1842,18 +1840,8 @@ NetworkController::ResultCode SqliteNetworkController::_doNetworkConfigRequest(c Address node(n); InetAddress addr(a); if (node) { - bool sf = false; - for(unsigned int k=0;k= 6) { - nc.serialize(netconf); - } - return NetworkController::NETCONF_QUERY_OK; } diff --git a/controller/SqliteNetworkController.hpp b/controller/SqliteNetworkController.hpp index cbc2d857d..3bb53bd96 100644 --- a/controller/SqliteNetworkController.hpp +++ b/controller/SqliteNetworkController.hpp @@ -62,8 +62,8 @@ public: const Identity &signingId, const Identity &identity, uint64_t nwid, - const NetworkConfigRequestMetaData &metaData, - Buffer<8194> &netconf); + const Dictionary &metaData, + NetworkConfig &nc); unsigned int handleControlPlaneHttpGET( const std::vector &path, @@ -113,8 +113,8 @@ private: const Identity &signingId, const Identity &identity, 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);