RedisNetworkConfigMaster.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #include <sys/types.h>
  34. #include <algorithm>
  35. #include <utility>
  36. #include <stdexcept>
  37. #include "RedisNetworkConfigMaster.hpp"
  38. #include "../node/Utils.hpp"
  39. #include "../node/CertificateOfMembership.hpp"
  40. #include "../node/NetworkConfig.hpp"
  41. namespace ZeroTier {
  42. RedisNetworkConfigMaster::RedisNetworkConfigMaster(
  43. const Identity &signingId,
  44. const char *redisHost,
  45. unsigned int redisPort,
  46. const char *redisPassword,
  47. unsigned int redisDatabaseNumber) :
  48. _lock(),
  49. _signingId(signingId),
  50. _redisHost(redisHost),
  51. _redisPassword((redisPassword) ? redisPassword : ""),
  52. _redisPort(redisPort),
  53. _redisDatabaseNumber(redisDatabaseNumber),
  54. _rc((redisContext *)0)
  55. {
  56. if (!_signingId.hasPrivate())
  57. throw std::runtime_error("RedisNetworkConfigMaster signing identity must have a private key");
  58. }
  59. RedisNetworkConfigMaster::~RedisNetworkConfigMaster()
  60. {
  61. Mutex::Lock _l(_lock);
  62. if (_rc)
  63. redisFree(_rc);
  64. }
  65. NetworkConfigMaster::ResultCode RedisNetworkConfigMaster::doNetworkConfigRequest(const InetAddress &fromAddr,uint64_t packetId,const Identity &member,uint64_t nwid,const Dictionary &metaData,uint64_t haveTimestamp,Dictionary &netconf)
  66. {
  67. char memberKey[128],nwids[24],addrs[16],nwKey[128],revKey[128];
  68. Dictionary memberRecord;
  69. std::string revision,tmps2;
  70. Mutex::Lock _l(_lock);
  71. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  72. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
  73. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  74. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  75. Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
  76. //TRACE("netconf: %s : %s if > %llu",nwids,addrs,(unsigned long long)haveTimestamp);
  77. // Check to make sure network itself exists and is valid
  78. if (!_hget(nwKey,"id",tmps2)) {
  79. netconf["error"] = "Redis error retrieving network record";
  80. return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
  81. }
  82. if (tmps2 != nwids)
  83. return NetworkConfigMaster::NETCONF_QUERY_OBJECT_NOT_FOUND;
  84. // Get network revision
  85. if (!_get(revKey,revision)) {
  86. netconf["error"] = "Redis error retrieving network revision";
  87. return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
  88. }
  89. if (!revision.length())
  90. revision = "0";
  91. // Get network member record for this peer
  92. if (!_hgetall(memberKey,memberRecord)) {
  93. netconf["error"] = "Redis error retrieving member record";
  94. return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
  95. }
  96. // If there is no member record, init a new one -- for public networks this
  97. // auto-authorizes, and for private nets it makes the peer show up in the UI
  98. // so the admin can authorize or delete/hide it.
  99. if ((memberRecord.size() == 0)||(memberRecord.get("id","") != addrs)||(memberRecord.get("nwid","") != nwids)) {
  100. if (!_initNewMember(nwid,member,metaData,memberRecord)) {
  101. netconf["error"] = "_initNewMember() failed";
  102. return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
  103. }
  104. }
  105. if (memberRecord.getBoolean("authorized")) {
  106. // Get current netconf and netconf timestamp
  107. uint64_t ts = memberRecord.getUInt("netconfTimestamp",0);
  108. std::string netconfStr(memberRecord.get("netconf",""));
  109. netconf.fromString(netconfStr);
  110. // Update statistics for this node
  111. Dictionary upd;
  112. upd.set("netconfClientTimestamp",haveTimestamp);
  113. if (fromAddr)
  114. upd.set("lastAt",fromAddr.toString());
  115. upd.set("lastSeen",Utils::now());
  116. _hmset(memberKey,upd);
  117. // Attempt to generate netconf for this node if there isn't
  118. // one or it's not in step with the network's revision.
  119. if (((ts == 0)||(netconfStr.length() == 0))||(memberRecord.get("netconfRevision","") != revision)) {
  120. std::string errorMessage;
  121. if (!_generateNetconf(nwid,member,metaData,netconf,ts,errorMessage)) {
  122. netconf["error"] = errorMessage;
  123. return NetworkConfigMaster::NETCONF_QUERY_INTERNAL_SERVER_ERROR;
  124. }
  125. }
  126. if (ts > haveTimestamp)
  127. return NetworkConfigMaster::NETCONF_QUERY_OK;
  128. else return NetworkConfigMaster::NETCONF_QUERY_OK_BUT_NOT_NEWER;
  129. } else {
  130. return NetworkConfigMaster::NETCONF_QUERY_ACCESS_DENIED;
  131. }
  132. }
  133. bool RedisNetworkConfigMaster::_reconnect()
  134. {
  135. struct timeval tv;
  136. if (_rc)
  137. redisFree(_rc);
  138. tv.tv_sec = ZT_NETCONF_REDIS_TIMEOUT;
  139. tv.tv_usec = 0;
  140. _rc = redisConnectWithTimeout(_redisHost.c_str(),_redisPort,tv);
  141. if (!_rc)
  142. return false;
  143. if (_rc->err) {
  144. redisFree(_rc);
  145. _rc = (redisContext *)0;
  146. return false;
  147. }
  148. redisSetTimeout(_rc,tv); // necessary???
  149. // TODO: support AUTH and SELECT !!!
  150. return true;
  151. }
  152. bool RedisNetworkConfigMaster::_hgetall(const char *key,Dictionary &hdata)
  153. {
  154. if (!_rc) {
  155. if (!_reconnect())
  156. return false;
  157. }
  158. redisReply *reply = (redisReply *)redisCommand(_rc,"HGETALL %s",key);
  159. if (!reply) {
  160. if (_reconnect())
  161. return _hgetall(key,hdata);
  162. return false;
  163. }
  164. hdata.clear();
  165. if (reply->type == REDIS_REPLY_ARRAY) {
  166. for(long i=0;i<reply->elements;) {
  167. try {
  168. const char *k = reply->element[i]->str;
  169. if (++i >= reply->elements)
  170. break;
  171. if ((k)&&(reply->element[i]->str))
  172. hdata[k] = reply->element[i]->str;
  173. ++i;
  174. } catch ( ... ) {
  175. break; // memory safety
  176. }
  177. }
  178. }
  179. freeReplyObject(reply);
  180. return true;
  181. }
  182. bool RedisNetworkConfigMaster::_hmset(const char *key,const Dictionary &hdata)
  183. {
  184. const char *hargv[1024];
  185. if (!hdata.size())
  186. return true;
  187. if (!_rc) {
  188. if (!_reconnect())
  189. return false;
  190. }
  191. hargv[0] = "HMSET";
  192. hargv[1] = key;
  193. int hargc = 2;
  194. for(Dictionary::const_iterator i(hdata.begin());i!=hdata.end();++i) {
  195. if (hargc >= 1024)
  196. break;
  197. hargv[hargc++] = i->first.c_str();
  198. hargv[hargc++] = i->second.c_str();
  199. }
  200. redisReply *reply = (redisReply *)redisCommandArgv(_rc,hargc,hargv,(const size_t *)0);
  201. if (!reply) {
  202. if (_reconnect())
  203. return _hmset(key,hdata);
  204. return false;
  205. }
  206. if (reply->type == REDIS_REPLY_ERROR) {
  207. freeReplyObject(reply);
  208. return false;
  209. }
  210. freeReplyObject(reply);
  211. return true;
  212. }
  213. bool RedisNetworkConfigMaster::_hget(const char *key,const char *hashKey,std::string &value)
  214. {
  215. if (!_rc) {
  216. if (!_reconnect())
  217. return false;
  218. }
  219. redisReply *reply = (redisReply *)redisCommand(_rc,"HGET %s %s",key,hashKey);
  220. if (!reply) {
  221. if (_reconnect())
  222. return _hget(key,hashKey,value);
  223. return false;
  224. }
  225. if (reply->type == REDIS_REPLY_STRING)
  226. value = reply->str;
  227. else value = "";
  228. freeReplyObject(reply);
  229. return true;
  230. }
  231. bool RedisNetworkConfigMaster::_hset(const char *key,const char *hashKey,const char *value)
  232. {
  233. if (!_rc) {
  234. if (!_reconnect())
  235. return false;
  236. }
  237. redisReply *reply = (redisReply *)redisCommand(_rc,"HSET %s %s %s",key,hashKey,value);
  238. if (!reply) {
  239. if (_reconnect())
  240. return _hset(key,hashKey,value);
  241. return false;
  242. }
  243. if (reply->type == REDIS_REPLY_ERROR) {
  244. freeReplyObject(reply);
  245. return false;
  246. }
  247. freeReplyObject(reply);
  248. return true;
  249. }
  250. bool RedisNetworkConfigMaster::_get(const char *key,std::string &value)
  251. {
  252. if (!_rc) {
  253. if (!_reconnect())
  254. return false;
  255. }
  256. redisReply *reply = (redisReply *)redisCommand(_rc,"GET %s",key);
  257. if (!reply) {
  258. if (_reconnect())
  259. return _get(key,value);
  260. return false;
  261. }
  262. if ((reply->type == REDIS_REPLY_STRING)&&(reply->str))
  263. value = reply->str;
  264. else value = "";
  265. freeReplyObject(reply);
  266. return true;
  267. }
  268. bool RedisNetworkConfigMaster::_smembers(const char *key,std::vector<std::string> &sdata)
  269. {
  270. if (!_rc) {
  271. if (!_reconnect())
  272. return false;
  273. }
  274. redisReply *reply = (redisReply *)redisCommand(_rc,"SMEMBERS %s",key);
  275. if (!reply) {
  276. if (_reconnect())
  277. return _smembers(key,sdata);
  278. return false;
  279. }
  280. sdata.clear();
  281. if (reply->type == REDIS_REPLY_ARRAY) {
  282. for(long i=0;i<reply->elements;++i) {
  283. if (reply->element[i]->str)
  284. sdata.push_back(reply->element[i]->str);
  285. }
  286. }
  287. return true;
  288. }
  289. bool RedisNetworkConfigMaster::_initNewMember(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &memberRecord)
  290. {
  291. char memberKey[256],nwids[24],addrs[16],nwKey[256];
  292. Dictionary networkRecord;
  293. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  294. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
  295. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  296. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  297. if (!_hgetall(nwKey,networkRecord)) {
  298. //LOG("netconf: Redis error retrieving %s",nwKey);
  299. return false;
  300. }
  301. if (networkRecord.get("id","") != nwids) {
  302. //TRACE("netconf: network %s not found (initNewMember)",nwids);
  303. return false;
  304. }
  305. memberRecord.clear();
  306. memberRecord["id"] = addrs;
  307. memberRecord["nwid"] = nwids;
  308. memberRecord["authorized"] = (networkRecord.getBoolean("private",true) ? "0" : "1"); // auto-authorize on public networks
  309. memberRecord.set("firstSeen",Utils::now());
  310. memberRecord["identity"] = member.toString(false);
  311. if (!_hmset(memberKey,memberRecord)) {
  312. //LOG("netconf: Redis error storing %s for new member %s",memberKey,addrs);
  313. return false;
  314. }
  315. return true;
  316. }
  317. bool RedisNetworkConfigMaster::_generateNetconf(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &netconf,uint64_t &ts,std::string &errorMessage)
  318. {
  319. char memberKey[256],nwids[24],addrs[16],tss[24],nwKey[256],revKey[128],abKey[128],ipaKey[128];
  320. Dictionary networkRecord,memberRecord;
  321. std::string revision;
  322. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  323. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  324. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
  325. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  326. Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
  327. Utils::snprintf(abKey,sizeof(revKey),"zt1:network:%s:activeBridges",nwids);
  328. Utils::snprintf(ipaKey,sizeof(revKey),"zt1:network:%s:ipAssignments",nwids);
  329. if (!_hgetall(nwKey,networkRecord)) {
  330. errorMessage = "Redis error retrieving network record";
  331. return false;
  332. }
  333. if (networkRecord.get("id","") != nwids) {
  334. errorMessage = "network IDs do not match in database";
  335. return false;
  336. }
  337. if (!_hgetall(memberKey,memberRecord)) {
  338. errorMessage = "Redis error retrieving member record";
  339. return false;
  340. }
  341. if (!_get(revKey,revision)) {
  342. errorMessage = "Redis error retrieving network revision";
  343. return false;
  344. }
  345. if (!revision.length())
  346. revision = "0";
  347. bool isPrivate = networkRecord.getBoolean("private",true);
  348. ts = Utils::now();
  349. Utils::snprintf(tss,sizeof(tss),"%llx",ts);
  350. // Core configuration
  351. netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = tss;
  352. netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = nwids;
  353. netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = addrs;
  354. netconf[ZT_NETWORKCONFIG_DICT_KEY_PRIVATE] = isPrivate ? "1" : "0";
  355. netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = networkRecord.get("name",nwids);
  356. netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = networkRecord.get("desc","");
  357. netconf[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = networkRecord.getBoolean("enableBroadcast",true) ? "1" : "0";
  358. netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = networkRecord.getBoolean("allowPassiveBridging",false) ? "1" : "0";
  359. netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = networkRecord.get("etherTypes",""); // these are stored as hex comma-delimited list
  360. // Multicast options
  361. netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = networkRecord.get("multicastRates","");
  362. uint64_t ml = networkRecord.getHexUInt("multicastLimit",0);
  363. if (ml > 0)
  364. netconf.setHex(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,ml);
  365. // Active bridge configuration
  366. {
  367. std::string activeBridgeList;
  368. std::vector<std::string> activeBridgeSet;
  369. if (!_smembers(abKey,activeBridgeSet)) {
  370. errorMessage = "Redis error retrieving active bridge set";
  371. return false;
  372. }
  373. std::sort(activeBridgeSet.begin(),activeBridgeSet.end());
  374. for(std::vector<std::string>::const_iterator i(activeBridgeSet.begin());i!=activeBridgeSet.end();++i) {
  375. if (i->length() == 10) {
  376. if (activeBridgeList.length() > 0)
  377. activeBridgeList.push_back(',');
  378. activeBridgeList.append(*i);
  379. }
  380. }
  381. if (activeBridgeList.length() > 0)
  382. netconf[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridgeList;
  383. }
  384. // IP address assignment and auto-assign using the ZeroTier-internal mechanism (not DHCP, etc.)
  385. {
  386. std::string ipAssignments(memberRecord.get("ipAssignments",""));
  387. // Get sorted, separated lists of IPv4 and IPv6 IP address assignments already present
  388. std::vector<InetAddress> ip4s,ip6s;
  389. {
  390. std::vector<std::string> ips(Utils::split(ipAssignments.c_str(),",","",""));
  391. for(std::vector<std::string>::iterator i(ips.begin());i!=ips.end();++i) {
  392. InetAddress a(*i);
  393. if (a.isV4())
  394. ip4s.push_back(a);
  395. else if (a.isV6())
  396. ip6s.push_back(a);
  397. }
  398. }
  399. std::sort(ip4s.begin(),ip4s.end());
  400. std::unique(ip4s.begin(),ip4s.end());
  401. std::sort(ip6s.begin(),ip6s.end());
  402. std::unique(ip6s.begin(),ip6s.end());
  403. // If IPv4 assignment mode is 'zt', send them to the client
  404. if (networkRecord.get("v4AssignMode","") == "zt") {
  405. // If we have no IPv4 addresses and we have an assignment pool, auto-assign
  406. if (ip4s.empty()) {
  407. InetAddress v4AssignPool(networkRecord.get("v4AssignPool",""));
  408. uint32_t pnet = Utils::ntoh(*((const uint32_t *)v4AssignPool.rawIpData()));
  409. unsigned int pbits = v4AssignPool.netmaskBits();
  410. if ((v4AssignPool.isV4())&&(pbits > 0)&&(pbits < 32)&&(pnet != 0)) {
  411. uint32_t pmask = 0xffffffff << (32 - pbits); // netmask over network part
  412. uint32_t invmask = ~pmask; // netmask over "random" part
  413. // Begin exploring the IP space by generating an IP from the ZeroTier address
  414. uint32_t first = (((uint32_t)(member.address().toInt() & 0xffffffffULL)) & invmask) | (pnet & pmask);
  415. if ((first & 0xff) == 0)
  416. first |= 1;
  417. else if ((first & 0xff) == 0xff)
  418. first &= 0xfe;
  419. // Start by trying this first IP
  420. uint32_t abcd = first;
  421. InetAddress ip;
  422. bool gotone = false;
  423. unsigned long sanityCounter = 0;
  424. do {
  425. // Convert to IPv4 InetAddress
  426. uint32_t abcdNetworkByteOrder = Utils::hton(abcd);
  427. ip.set(&abcdNetworkByteOrder,4,pbits);
  428. // Is 'ip' already assigned to another node?
  429. std::string assignment;
  430. if (!_hget(ipaKey,ip.toString().c_str(),assignment)) {
  431. errorMessage = "Redis error while checking IP allocation";
  432. return false;
  433. }
  434. if ((assignment.length() != 10)||(assignment == member.address().toString())) {
  435. gotone = true;
  436. break; // not taken!
  437. }
  438. // If we made it here, the IP was taken so increment and mask and try again
  439. ++abcd;
  440. abcd &= invmask;
  441. abcd |= (pnet & pmask);
  442. if ((abcd & 0xff) == 0)
  443. abcd |= 1;
  444. else if ((abcd & 0xff) == 0xff)
  445. abcd &= 0xfe;
  446. // Don't spend insane amounts of time here -- if we have to try this hard, the user
  447. // needs to allocate a larger IP block.
  448. if (++sanityCounter >= 65535)
  449. break;
  450. } while (abcd != first); // keep going until we loop back around to 'first'
  451. // If we got one, add to IP list and claim in database
  452. if (gotone) {
  453. ip4s.push_back(ip);
  454. _hset(ipaKey,ip.toString().c_str(),member.address().toString().c_str());
  455. if (ipAssignments.length() > 0)
  456. ipAssignments.push_back(',');
  457. ipAssignments.append(ip.toString());
  458. _hset(memberKey,"ipAssignments",ipAssignments.c_str());
  459. } else {
  460. char tmp[1024];
  461. 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);
  462. errorMessage = tmp;
  463. return false;
  464. }
  465. }
  466. }
  467. // Create comma-delimited list to send to client
  468. std::string v4s;
  469. for(std::vector<InetAddress>::iterator i(ip4s.begin());i!=ip4s.end();++i) {
  470. if (v4s.length() > 0)
  471. v4s.push_back(',');
  472. v4s.append(i->toString());
  473. }
  474. if (v4s.length())
  475. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
  476. }
  477. if (networkRecord.get("v6AssignMode","") == "zt") {
  478. // TODO: IPv6 auto-assign ... not quite baked yet. :)
  479. std::string v6s;
  480. for(std::vector<InetAddress>::iterator i(ip6s.begin());i!=ip6s.end();++i) {
  481. if (v6s.length() > 0)
  482. v6s.push_back(',');
  483. v6s.append(i->toString());
  484. }
  485. if (v6s.length())
  486. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = v6s;
  487. }
  488. }
  489. // If this is a private network, generate a signed certificate of membership
  490. if (isPrivate) {
  491. CertificateOfMembership com(Utils::strToU64(revision.c_str()),1,nwid,member.address());
  492. if (com.sign(_signingId)) // basically can't fail unless our identity is invalid
  493. netconf[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
  494. else {
  495. errorMessage = "unable to sign COM";
  496. return false;
  497. }
  498. }
  499. // Sign netconf dictionary itself
  500. if (!netconf.sign(_signingId)) {
  501. errorMessage = "unable to sign netconf dictionary";
  502. return false;
  503. }
  504. // Record new netconf in database for re-use on subsequent repeat queries
  505. {
  506. Dictionary upd;
  507. upd["netconf"] = netconf.toString();
  508. upd.set("netconfTimestamp",ts);
  509. upd["netconfRevision"] = revision;
  510. if (!_hmset(memberKey,upd)) {
  511. errorMessage = "Redis error updating network record with new netconf dictionary";
  512. return false;
  513. }
  514. }
  515. return true;
  516. }
  517. } // namespace ZeroTier