2015-01-06 21:45:10 +00:00
|
|
|
/*
|
2015-02-17 21:11:34 +00:00
|
|
|
* ZeroTier One - Network Virtualization Everywhere
|
|
|
|
* Copyright (C) 2011-2015 ZeroTier, Inc.
|
2015-01-06 21:45:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* --
|
|
|
|
*
|
|
|
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
|
|
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
*
|
|
|
|
* If you would like to embed ZeroTier into a commercial application or
|
|
|
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
|
|
|
* LLC. Start here: http://www.zerotier.com/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
2015-02-24 22:17:57 +00:00
|
|
|
#include <stdexcept>
|
2015-01-08 22:27:55 +00:00
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
#include "SqliteNetworkConfigMaster.hpp"
|
2015-02-24 22:17:57 +00:00
|
|
|
#include "../node/Utils.hpp"
|
|
|
|
#include "../node/CertificateOfMembership.hpp"
|
|
|
|
#include "../node/NetworkConfig.hpp"
|
2015-01-06 21:45:10 +00:00
|
|
|
|
2015-03-17 21:54:13 +00:00
|
|
|
// Include ZT_NETCONF_SCHEMA_SQL constant to init database
|
|
|
|
#include "netconf-schema.sql.c"
|
|
|
|
|
2015-03-12 21:27:14 +00:00
|
|
|
// Stored in database as schemaVersion key in Config.
|
|
|
|
// If not present, database is assumed to be empty and at the current schema version
|
|
|
|
// and this key/value is added automatically.
|
|
|
|
#define ZT_NETCONF_SQLITE_SCHEMA_VERSION 1
|
2015-03-17 22:20:45 +00:00
|
|
|
#define ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR "1"
|
2015-03-12 21:27:14 +00:00
|
|
|
|
2015-01-06 21:45:10 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
SqliteNetworkConfigMaster::SqliteNetworkConfigMaster(const Identity &signingId,const char *dbPath) :
|
2015-02-24 22:17:57 +00:00
|
|
|
_signingId(signingId),
|
2015-03-12 21:03:53 +00:00
|
|
|
_dbPath(dbPath),
|
2015-03-17 21:54:13 +00:00
|
|
|
_db((sqlite3 *)0),
|
2015-03-12 21:03:53 +00:00
|
|
|
_lock()
|
2015-01-06 21:45:10 +00:00
|
|
|
{
|
2015-02-24 22:17:57 +00:00
|
|
|
if (!_signingId.hasPrivate())
|
2015-03-12 21:03:53 +00:00
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster signing identity must have a private key");
|
|
|
|
|
2015-03-17 21:54:13 +00:00
|
|
|
if (sqlite3_open_v2(dbPath,&_db,SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE,(const char *)0) != SQLITE_OK)
|
2015-03-12 21:03:53 +00:00
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster cannot open database file");
|
|
|
|
sqlite3_busy_timeout(_db,10000);
|
2015-03-17 22:20:45 +00:00
|
|
|
|
2015-03-17 23:27:52 +00:00
|
|
|
sqlite3_stmt *s;
|
|
|
|
for(int k=0;k<2;++k) {
|
|
|
|
s = (sqlite3_stmt *)0;
|
|
|
|
if ((sqlite3_prepare_v2(_db,"SELECT 'v' FROM Config WHERE 'k' = 'schemaVersion';",-1,&s,(const char **)0) != SQLITE_OK)||(!s)) {
|
|
|
|
if (sqlite3_exec(_db,ZT_NETCONF_SCHEMA_SQL"INSERT INTO Config (k,v) VALUES ('schemaVersion',"ZT_NETCONF_SQLITE_SCHEMA_VERSION_STR");",0,0,0) != SQLITE_OK) {
|
|
|
|
sqlite3_close(_db);
|
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster cannot initialize database and/or insert schemaVersion into Config table");
|
|
|
|
} else {
|
|
|
|
// Initialized database and set schema version, so we are done.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else break;
|
2015-03-17 22:20:45 +00:00
|
|
|
}
|
|
|
|
if (!s) {
|
|
|
|
sqlite3_close(_db);
|
2015-03-17 23:27:52 +00:00
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster unable to create prepared statement or initialize database");
|
2015-03-17 22:20:45 +00:00
|
|
|
}
|
|
|
|
|
2015-03-17 23:27:52 +00:00
|
|
|
// If we made it here, database was opened and prepared statement was created
|
|
|
|
// to check schema version. Check and upgrade if needed.
|
|
|
|
|
|
|
|
int schemaVersion = -1234;
|
2015-03-17 22:20:45 +00:00
|
|
|
if (sqlite3_step(s) == SQLITE_ROW)
|
|
|
|
schemaVersion = sqlite3_column_int(s,0);
|
|
|
|
|
|
|
|
sqlite3_finalize(s);
|
|
|
|
|
2015-03-17 23:27:52 +00:00
|
|
|
if (schemaVersion == -1234) {
|
|
|
|
sqlite3_close(_db);
|
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster schemaVersion not found in Config table (init failure?)");
|
2015-03-17 22:20:45 +00:00
|
|
|
} else if (schemaVersion != ZT_NETCONF_SQLITE_SCHEMA_VERSION) {
|
|
|
|
// Note -- this will eventually run auto-upgrades so this isn't how it'll work going forward
|
|
|
|
sqlite3_close(_db);
|
|
|
|
throw std::runtime_error("SqliteNetworkConfigMaster database schema version mismatch");
|
|
|
|
}
|
2015-01-06 21:45:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
SqliteNetworkConfigMaster::~SqliteNetworkConfigMaster()
|
2015-01-06 21:45:10 +00:00
|
|
|
{
|
|
|
|
Mutex::Lock _l(_lock);
|
2015-03-12 21:03:53 +00:00
|
|
|
if (_db)
|
|
|
|
sqlite3_close(_db);
|
2015-01-06 21:45:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
NetworkConfigMaster::ResultCode SqliteNetworkConfigMaster::doNetworkConfigRequest(const InetAddress &fromAddr,uint64_t packetId,const Identity &member,uint64_t nwid,const Dictionary &metaData,uint64_t haveTimestamp,Dictionary &netconf)
|
2015-01-06 21:45:10 +00:00
|
|
|
{
|
2015-03-12 21:03:53 +00:00
|
|
|
#if 0
|
2015-01-08 22:27:55 +00:00
|
|
|
char memberKey[128],nwids[24],addrs[16],nwKey[128],revKey[128];
|
2015-01-07 01:16:54 +00:00
|
|
|
Dictionary memberRecord;
|
|
|
|
std::string revision,tmps2;
|
|
|
|
|
|
|
|
Mutex::Lock _l(_lock);
|
|
|
|
|
|
|
|
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
2015-02-24 22:17:57 +00:00
|
|
|
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
|
2015-01-07 01:16:54 +00:00
|
|
|
Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
|
|
|
|
Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
|
2015-01-08 22:27:55 +00:00
|
|
|
Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-02-24 22:17:57 +00:00
|
|
|
//TRACE("netconf: %s : %s if > %llu",nwids,addrs,(unsigned long long)haveTimestamp);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// Check to make sure network itself exists and is valid
|
2015-01-07 01:16:54 +00:00
|
|
|
if (!_hget(nwKey,"id",tmps2)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
netconf["error"] = "Sqlite error retrieving network record ID field";
|
2015-02-24 22:17:57 +00:00
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
2015-02-24 22:17:57 +00:00
|
|
|
if (tmps2 != nwids)
|
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_OBJECT_NOT_FOUND;
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// Get network revision
|
2015-01-08 22:27:55 +00:00
|
|
|
if (!_get(revKey,revision)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
netconf["error"] = "Sqlite error retrieving network revision";
|
2015-02-24 22:17:57 +00:00
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
if (!revision.length())
|
|
|
|
revision = "0";
|
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// Get network member record for this peer
|
2015-01-07 01:16:54 +00:00
|
|
|
if (!_hgetall(memberKey,memberRecord)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
netconf["error"] = "Sqlite error retrieving member record";
|
2015-02-24 22:17:57 +00:00
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// If there is no member record, init a new one -- for public networks this
|
|
|
|
// auto-authorizes, and for private nets it makes the peer show up in the UI
|
|
|
|
// so the admin can authorize or delete/hide it.
|
2015-01-07 01:16:54 +00:00
|
|
|
if ((memberRecord.size() == 0)||(memberRecord.get("id","") != addrs)||(memberRecord.get("nwid","") != nwids)) {
|
2015-02-24 22:17:57 +00:00
|
|
|
if (!_initNewMember(nwid,member,metaData,memberRecord)) {
|
|
|
|
netconf["error"] = "_initNewMember() failed";
|
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (memberRecord.getBoolean("authorized")) {
|
2015-01-09 21:35:20 +00:00
|
|
|
// Get current netconf and netconf timestamp
|
2015-02-24 22:17:57 +00:00
|
|
|
uint64_t ts = memberRecord.getUInt("netconfTimestamp",0);
|
|
|
|
std::string netconfStr(memberRecord.get("netconf",""));
|
|
|
|
netconf.fromString(netconfStr);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// Update statistics for this node
|
2015-01-07 01:16:54 +00:00
|
|
|
Dictionary upd;
|
2015-02-24 22:17:57 +00:00
|
|
|
upd.set("netconfClientTimestamp",haveTimestamp);
|
2015-01-07 01:16:54 +00:00
|
|
|
if (fromAddr)
|
|
|
|
upd.set("lastAt",fromAddr.toString());
|
2015-02-24 22:17:57 +00:00
|
|
|
upd.set("lastSeen",Utils::now());
|
2015-01-07 01:16:54 +00:00
|
|
|
_hmset(memberKey,upd);
|
|
|
|
|
2015-01-09 21:35:20 +00:00
|
|
|
// Attempt to generate netconf for this node if there isn't
|
|
|
|
// one or it's not in step with the network's revision.
|
2015-02-24 22:17:57 +00:00
|
|
|
if (((ts == 0)||(netconfStr.length() == 0))||(memberRecord.get("netconfRevision","") != revision)) {
|
|
|
|
std::string errorMessage;
|
|
|
|
if (!_generateNetconf(nwid,member,metaData,netconf,ts,errorMessage)) {
|
|
|
|
netconf["error"] = errorMessage;
|
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-24 22:17:57 +00:00
|
|
|
|
|
|
|
if (ts > haveTimestamp)
|
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_OK;
|
|
|
|
else return NetworkConfigMaster::NETCONF_QUERY_OK_BUT_NOT_NEWER;
|
2015-01-07 01:16:54 +00:00
|
|
|
} else {
|
2015-02-24 22:17:57 +00:00
|
|
|
return NetworkConfigMaster::NETCONF_QUERY_ACCESS_DENIED;
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
2015-03-12 21:03:53 +00:00
|
|
|
#endif
|
2015-01-06 21:45:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
bool SqliteNetworkConfigMaster::_initNewMember(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &memberRecord)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2015-03-12 21:03:53 +00:00
|
|
|
#if 0
|
2015-02-25 00:15:01 +00:00
|
|
|
char memberKey[128],nwids[24],addrs[16],nwKey[128],membersKey[128];
|
2015-01-07 01:16:54 +00:00
|
|
|
Dictionary networkRecord;
|
|
|
|
|
|
|
|
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
2015-02-24 22:17:57 +00:00
|
|
|
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
|
2015-01-07 01:16:54 +00:00
|
|
|
Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
|
|
|
|
Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
|
2015-02-25 00:15:01 +00:00
|
|
|
Utils::snprintf(membersKey,sizeof(membersKey),"zt1:network:%s:members",nwids);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
|
|
|
if (!_hgetall(nwKey,networkRecord)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
//LOG("netconf: Sqlite error retrieving %s",nwKey);
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (networkRecord.get("id","") != nwids) {
|
2015-02-24 22:17:57 +00:00
|
|
|
//TRACE("netconf: network %s not found (initNewMember)",nwids);
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memberRecord.clear();
|
|
|
|
memberRecord["id"] = addrs;
|
|
|
|
memberRecord["nwid"] = nwids;
|
2015-02-25 00:15:01 +00:00
|
|
|
memberRecord["authorized"] = ((networkRecord.get("private","1") == "0") ? "1" : "0"); // auto-authorize on public networks
|
2015-02-24 22:17:57 +00:00
|
|
|
memberRecord.set("firstSeen",Utils::now());
|
|
|
|
memberRecord["identity"] = member.toString(false);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-02-25 00:15:01 +00:00
|
|
|
if (!_hmset(memberKey,memberRecord))
|
|
|
|
return false;
|
|
|
|
if (!_sadd(membersKey,addrs))
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2015-03-12 21:03:53 +00:00
|
|
|
#endif
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-12 21:03:53 +00:00
|
|
|
bool SqliteNetworkConfigMaster::_generateNetconf(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &netconf,uint64_t &ts,std::string &errorMessage)
|
2015-01-07 01:16:54 +00:00
|
|
|
{
|
2015-03-12 21:03:53 +00:00
|
|
|
#if 0
|
2015-01-08 22:27:55 +00:00
|
|
|
char memberKey[256],nwids[24],addrs[16],tss[24],nwKey[256],revKey[128],abKey[128],ipaKey[128];
|
2015-02-24 22:17:57 +00:00
|
|
|
Dictionary networkRecord,memberRecord;
|
2015-01-08 22:27:55 +00:00
|
|
|
std::string revision;
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
|
2015-01-07 01:16:54 +00:00
|
|
|
Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
|
2015-02-24 22:17:57 +00:00
|
|
|
Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
|
2015-01-07 01:16:54 +00:00
|
|
|
Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
|
2015-01-08 22:27:55 +00:00
|
|
|
Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
|
|
|
|
Utils::snprintf(abKey,sizeof(revKey),"zt1:network:%s:activeBridges",nwids);
|
|
|
|
Utils::snprintf(ipaKey,sizeof(revKey),"zt1:network:%s:ipAssignments",nwids);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
|
|
|
if (!_hgetall(nwKey,networkRecord)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error retrieving network record";
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (networkRecord.get("id","") != nwids) {
|
2015-02-24 22:17:57 +00:00
|
|
|
errorMessage = "network IDs do not match in database";
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_hgetall(memberKey,memberRecord)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error retrieving member record";
|
2015-01-07 01:16:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
if (!_get(revKey,revision)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error retrieving network revision";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!revision.length())
|
|
|
|
revision = "0";
|
|
|
|
|
2015-01-07 01:16:54 +00:00
|
|
|
bool isPrivate = networkRecord.getBoolean("private",true);
|
|
|
|
ts = Utils::now();
|
|
|
|
Utils::snprintf(tss,sizeof(tss),"%llx",ts);
|
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
// Core configuration
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = tss;
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = nwids;
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = addrs;
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_PRIVATE] = isPrivate ? "1" : "0";
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = networkRecord.get("name",nwids);
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = networkRecord.get("desc","");
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = networkRecord.getBoolean("enableBroadcast",true) ? "1" : "0";
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = networkRecord.getBoolean("allowPassiveBridging",false) ? "1" : "0";
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = networkRecord.get("etherTypes",""); // these are stored as hex comma-delimited list
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
// Multicast options
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = networkRecord.get("multicastRates","");
|
2015-01-07 01:16:54 +00:00
|
|
|
uint64_t ml = networkRecord.getHexUInt("multicastLimit",0);
|
|
|
|
if (ml > 0)
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf.setHex(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,ml);
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
// Active bridge configuration
|
|
|
|
{
|
|
|
|
std::string activeBridgeList;
|
|
|
|
std::vector<std::string> activeBridgeSet;
|
|
|
|
if (!_smembers(abKey,activeBridgeSet)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error retrieving active bridge set";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::sort(activeBridgeSet.begin(),activeBridgeSet.end());
|
|
|
|
for(std::vector<std::string>::const_iterator i(activeBridgeSet.begin());i!=activeBridgeSet.end();++i) {
|
|
|
|
if (i->length() == 10) {
|
|
|
|
if (activeBridgeList.length() > 0)
|
|
|
|
activeBridgeList.push_back(',');
|
|
|
|
activeBridgeList.append(*i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (activeBridgeList.length() > 0)
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridgeList;
|
2015-01-08 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IP address assignment and auto-assign using the ZeroTier-internal mechanism (not DHCP, etc.)
|
|
|
|
{
|
|
|
|
std::string ipAssignments(memberRecord.get("ipAssignments",""));
|
|
|
|
|
|
|
|
// Get sorted, separated lists of IPv4 and IPv6 IP address assignments already present
|
|
|
|
std::vector<InetAddress> ip4s,ip6s;
|
|
|
|
{
|
|
|
|
std::vector<std::string> ips(Utils::split(ipAssignments.c_str(),",","",""));
|
|
|
|
for(std::vector<std::string>::iterator i(ips.begin());i!=ips.end();++i) {
|
|
|
|
InetAddress a(*i);
|
|
|
|
if (a.isV4())
|
|
|
|
ip4s.push_back(a);
|
|
|
|
else if (a.isV6())
|
|
|
|
ip6s.push_back(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::sort(ip4s.begin(),ip4s.end());
|
|
|
|
std::unique(ip4s.begin(),ip4s.end());
|
|
|
|
std::sort(ip6s.begin(),ip6s.end());
|
|
|
|
std::unique(ip6s.begin(),ip6s.end());
|
|
|
|
|
|
|
|
// If IPv4 assignment mode is 'zt', send them to the client
|
|
|
|
if (networkRecord.get("v4AssignMode","") == "zt") {
|
|
|
|
// If we have no IPv4 addresses and we have an assignment pool, auto-assign
|
|
|
|
if (ip4s.empty()) {
|
|
|
|
InetAddress v4AssignPool(networkRecord.get("v4AssignPool",""));
|
|
|
|
uint32_t pnet = Utils::ntoh(*((const uint32_t *)v4AssignPool.rawIpData()));
|
|
|
|
unsigned int pbits = v4AssignPool.netmaskBits();
|
|
|
|
|
|
|
|
if ((v4AssignPool.isV4())&&(pbits > 0)&&(pbits < 32)&&(pnet != 0)) {
|
|
|
|
uint32_t pmask = 0xffffffff << (32 - pbits); // netmask over network part
|
|
|
|
uint32_t invmask = ~pmask; // netmask over "random" part
|
|
|
|
|
|
|
|
// Begin exploring the IP space by generating an IP from the ZeroTier address
|
2015-02-24 22:17:57 +00:00
|
|
|
uint32_t first = (((uint32_t)(member.address().toInt() & 0xffffffffULL)) & invmask) | (pnet & pmask);
|
2015-01-08 22:27:55 +00:00
|
|
|
if ((first & 0xff) == 0)
|
|
|
|
first |= 1;
|
|
|
|
else if ((first & 0xff) == 0xff)
|
|
|
|
first &= 0xfe;
|
|
|
|
|
|
|
|
// Start by trying this first IP
|
|
|
|
uint32_t abcd = first;
|
|
|
|
|
|
|
|
InetAddress ip;
|
|
|
|
bool gotone = false;
|
|
|
|
unsigned long sanityCounter = 0;
|
|
|
|
do {
|
|
|
|
// Convert to IPv4 InetAddress
|
|
|
|
uint32_t abcdNetworkByteOrder = Utils::hton(abcd);
|
|
|
|
ip.set(&abcdNetworkByteOrder,4,pbits);
|
|
|
|
|
|
|
|
// Is 'ip' already assigned to another node?
|
|
|
|
std::string assignment;
|
|
|
|
if (!_hget(ipaKey,ip.toString().c_str(),assignment)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error while checking IP allocation";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-24 22:17:57 +00:00
|
|
|
if ((assignment.length() != 10)||(assignment == member.address().toString())) {
|
2015-01-08 22:27:55 +00:00
|
|
|
gotone = true;
|
|
|
|
break; // not taken!
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we made it here, the IP was taken so increment and mask and try again
|
|
|
|
++abcd;
|
|
|
|
abcd &= invmask;
|
|
|
|
abcd |= (pnet & pmask);
|
|
|
|
if ((abcd & 0xff) == 0)
|
|
|
|
abcd |= 1;
|
|
|
|
else if ((abcd & 0xff) == 0xff)
|
|
|
|
abcd &= 0xfe;
|
|
|
|
|
|
|
|
// Don't spend insane amounts of time here -- if we have to try this hard, the user
|
|
|
|
// needs to allocate a larger IP block.
|
|
|
|
if (++sanityCounter >= 65535)
|
|
|
|
break;
|
|
|
|
} while (abcd != first); // keep going until we loop back around to 'first'
|
|
|
|
|
|
|
|
// If we got one, add to IP list and claim in database
|
|
|
|
if (gotone) {
|
|
|
|
ip4s.push_back(ip);
|
2015-02-24 22:17:57 +00:00
|
|
|
_hset(ipaKey,ip.toString().c_str(),member.address().toString().c_str());
|
2015-01-08 22:27:55 +00:00
|
|
|
if (ipAssignments.length() > 0)
|
|
|
|
ipAssignments.push_back(',');
|
|
|
|
ipAssignments.append(ip.toString());
|
|
|
|
_hset(memberKey,"ipAssignments",ipAssignments.c_str());
|
|
|
|
} else {
|
2015-02-24 22:17:57 +00:00
|
|
|
char tmp[1024];
|
|
|
|
Utils::snprintf(tmp,sizeof(tmp),"failed to allocate IP in %s for %s in network %s, need a larger pool!",v4AssignPool.toString().c_str(),addrs,nwids);
|
|
|
|
errorMessage = tmp;
|
|
|
|
return false;
|
2015-01-08 22:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create comma-delimited list to send to client
|
|
|
|
std::string v4s;
|
|
|
|
for(std::vector<InetAddress>::iterator i(ip4s.begin());i!=ip4s.end();++i) {
|
|
|
|
if (v4s.length() > 0)
|
|
|
|
v4s.push_back(',');
|
|
|
|
v4s.append(i->toString());
|
|
|
|
}
|
|
|
|
if (v4s.length())
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
|
2015-01-08 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (networkRecord.get("v6AssignMode","") == "zt") {
|
|
|
|
// TODO: IPv6 auto-assign ... not quite baked yet. :)
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
std::string v6s;
|
|
|
|
for(std::vector<InetAddress>::iterator i(ip6s.begin());i!=ip6s.end();++i) {
|
|
|
|
if (v6s.length() > 0)
|
|
|
|
v6s.push_back(',');
|
|
|
|
v6s.append(i->toString());
|
|
|
|
}
|
|
|
|
if (v6s.length())
|
2015-02-24 22:17:57 +00:00
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = v6s;
|
2015-01-08 22:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-07 01:16:54 +00:00
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
// If this is a private network, generate a signed certificate of membership
|
2015-01-07 01:16:54 +00:00
|
|
|
if (isPrivate) {
|
2015-02-24 22:17:57 +00:00
|
|
|
CertificateOfMembership com(Utils::strToU64(revision.c_str()),1,nwid,member.address());
|
|
|
|
if (com.sign(_signingId)) // basically can't fail unless our identity is invalid
|
|
|
|
netconf[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
|
2015-01-08 22:27:55 +00:00
|
|
|
else {
|
2015-02-24 22:17:57 +00:00
|
|
|
errorMessage = "unable to sign COM";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 22:27:55 +00:00
|
|
|
// Sign netconf dictionary itself
|
2015-02-24 22:17:57 +00:00
|
|
|
if (!netconf.sign(_signingId)) {
|
|
|
|
errorMessage = "unable to sign netconf dictionary";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record new netconf in database for re-use on subsequent repeat queries
|
|
|
|
{
|
|
|
|
Dictionary upd;
|
2015-02-24 22:17:57 +00:00
|
|
|
upd["netconf"] = netconf.toString();
|
|
|
|
upd.set("netconfTimestamp",ts);
|
2015-01-08 22:27:55 +00:00
|
|
|
upd["netconfRevision"] = revision;
|
|
|
|
if (!_hmset(memberKey,upd)) {
|
2015-03-12 21:03:53 +00:00
|
|
|
errorMessage = "Sqlite error updating network record with new netconf dictionary";
|
2015-01-08 22:27:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-01-07 01:16:54 +00:00
|
|
|
|
|
|
|
return true;
|
2015-03-12 21:03:53 +00:00
|
|
|
#endif
|
2015-01-07 01:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 21:45:10 +00:00
|
|
|
} // namespace ZeroTier
|