NetworkConfigMaster.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2015 ZeroTier Networks
  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 "Constants.hpp"
  28. #include "NetworkConfigMaster.hpp"
  29. #ifdef ZT_ENABLE_NETCONF_MASTER
  30. #include <stdint.h>
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <sys/time.h>
  36. #include <sys/types.h>
  37. #include <algorithm>
  38. #include <utility>
  39. #include "RuntimeEnvironment.hpp"
  40. #include "Switch.hpp"
  41. #include "Packet.hpp"
  42. #include "NetworkConfig.hpp"
  43. #include "Utils.hpp"
  44. #include "Node.hpp"
  45. #include "Logger.hpp"
  46. #include "Topology.hpp"
  47. #include "Peer.hpp"
  48. #include "CertificateOfMembership.hpp"
  49. // Redis timeout in seconds
  50. #define ZT_NETCONF_REDIS_TIMEOUT 10
  51. namespace ZeroTier {
  52. NetworkConfigMaster::NetworkConfigMaster(
  53. const RuntimeEnvironment *renv,
  54. const char *redisHost,
  55. unsigned int redisPort,
  56. const char *redisPassword,
  57. unsigned int redisDatabaseNumber) :
  58. _lock(),
  59. _redisHost(redisHost),
  60. _redisPassword((redisPassword) ? redisPassword : ""),
  61. _redisPort(redisPort),
  62. _redisDatabaseNumber(redisDatabaseNumber),
  63. RR(renv),
  64. _rc((redisContext *)0)
  65. {
  66. }
  67. NetworkConfigMaster::~NetworkConfigMaster()
  68. {
  69. Mutex::Lock _l(_lock);
  70. if (_rc)
  71. redisFree(_rc);
  72. }
  73. void NetworkConfigMaster::doNetworkConfigRequest(const InetAddress &fromAddr,uint64_t packetId,const Address &member,uint64_t nwid,const Dictionary &metaData,uint64_t haveTimestamp)
  74. {
  75. char memberKey[128],nwids[24],addrs[16],nwKey[128],revKey[128];
  76. Dictionary memberRecord;
  77. std::string revision,tmps2;
  78. Mutex::Lock _l(_lock);
  79. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  80. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.toInt());
  81. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  82. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  83. Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
  84. TRACE("netconf: request from %s for %s (if newer than %llu)",addrs,nwids,(unsigned long long)haveTimestamp);
  85. if (!_hget(nwKey,"id",tmps2)) {
  86. LOG("netconf: Redis error retrieving %s/id",nwKey);
  87. return;
  88. }
  89. if (tmps2 != nwids) {
  90. TRACE("netconf: network %s not found",nwids);
  91. Packet outp(member,RR->identity.address(),Packet::VERB_ERROR);
  92. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  93. outp.append(packetId);
  94. outp.append((unsigned char)Packet::ERROR_OBJ_NOT_FOUND);
  95. outp.append(nwid);
  96. RR->sw->send(outp,true);
  97. return;
  98. }
  99. if (!_get(revKey,revision)) {
  100. LOG("netconf: Redis error retrieving %s",revKey);
  101. return;
  102. }
  103. if (!revision.length())
  104. revision = "0";
  105. if (!_hgetall(memberKey,memberRecord)) {
  106. LOG("netconf: Redis error retrieving %s",memberKey);
  107. return;
  108. }
  109. if ((memberRecord.size() == 0)||(memberRecord.get("id","") != addrs)||(memberRecord.get("nwid","") != nwids)) {
  110. if (!_initNewMember(nwid,member,metaData,memberRecord))
  111. return;
  112. }
  113. if (memberRecord.getBoolean("authorized")) {
  114. uint64_t ts = memberRecord.getHexUInt("netconfTimestamp",0);
  115. std::string netconf(memberRecord.get("netconf",""));
  116. Dictionary upd;
  117. upd.setHex("netconfClientTimestamp",haveTimestamp);
  118. if (fromAddr)
  119. upd.set("lastAt",fromAddr.toString());
  120. upd.setHex("lastSeen",Utils::now());
  121. _hmset(memberKey,upd);
  122. if (((ts == 0)||(netconf.length() == 0))||(memberRecord.get("netconfRevision","") != revision)) {
  123. if (!_generateNetconf(nwid,member,metaData,netconf,ts))
  124. return;
  125. }
  126. if (ts > haveTimestamp) {
  127. TRACE("netconf: sending %u bytes of netconf data to %s",netconf.length(),addrs);
  128. Packet outp(member,RR->identity.address(),Packet::VERB_OK);
  129. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  130. outp.append(packetId);
  131. outp.append(nwid);
  132. outp.append((uint16_t)netconf.length());
  133. outp.append(netconf.data(),netconf.length());
  134. outp.compress();
  135. if (outp.size() > ZT_PROTO_MAX_PACKET_LENGTH) { // sanity check -- this would be weird
  136. TRACE("netconf: compressed packet exceeds ZT_PROTO_MAX_PACKET_LENGTH!");
  137. return;
  138. }
  139. RR->sw->send(outp,true);
  140. }
  141. } else {
  142. TRACE("netconf: access denied for %s on %s",addrs,nwids);
  143. Packet outp(member,RR->identity.address(),Packet::VERB_ERROR);
  144. outp.append((unsigned char)Packet::VERB_NETWORK_CONFIG_REQUEST);
  145. outp.append(packetId);
  146. outp.append((unsigned char)Packet::ERROR_NETWORK_ACCESS_DENIED_);
  147. outp.append(nwid);
  148. RR->sw->send(outp,true);
  149. }
  150. }
  151. bool NetworkConfigMaster::_reconnect()
  152. {
  153. struct timeval tv;
  154. if (_rc)
  155. redisFree(_rc);
  156. tv.tv_sec = ZT_NETCONF_REDIS_TIMEOUT;
  157. tv.tv_usec = 0;
  158. _rc = redisConnectWithTimeout(_redisHost.c_str(),_redisPort,tv);
  159. if (!_rc)
  160. return false;
  161. if (_rc->err) {
  162. redisFree(_rc);
  163. _rc = (redisContext *)0;
  164. return false;
  165. }
  166. redisSetTimeout(_rc,tv); // necessary???
  167. // TODO: support AUTH and SELECT !!!
  168. return true;
  169. }
  170. bool NetworkConfigMaster::_hgetall(const char *key,Dictionary &hdata)
  171. {
  172. if (!_rc) {
  173. if (!_reconnect())
  174. return false;
  175. }
  176. redisReply *reply = (redisReply *)redisCommand(_rc,"HGETALL %s",key);
  177. if (!reply) {
  178. if (_reconnect())
  179. return _hgetall(key,hdata);
  180. return false;
  181. }
  182. hdata.clear();
  183. if (reply->type == REDIS_REPLY_ARRAY) {
  184. for(long i=0;i<reply->elements;) {
  185. try {
  186. const char *k = reply->element[i]->str;
  187. if (++i >= reply->elements)
  188. break;
  189. if ((k)&&(reply->element[i]->str))
  190. hdata[k] = reply->element[i]->str;
  191. ++i;
  192. } catch ( ... ) {
  193. break; // memory safety
  194. }
  195. }
  196. }
  197. freeReplyObject(reply);
  198. return true;
  199. }
  200. bool NetworkConfigMaster::_hmset(const char *key,const Dictionary &hdata)
  201. {
  202. const char *hargv[1024];
  203. if (!hdata.size())
  204. return true;
  205. if (!_rc) {
  206. if (!_reconnect())
  207. return false;
  208. }
  209. hargv[0] = "HMSET";
  210. hargv[1] = key;
  211. int hargc = 2;
  212. for(Dictionary::const_iterator i(hdata.begin());i!=hdata.end();++i) {
  213. if (hargc >= 1024)
  214. break;
  215. hargv[hargc++] = i->first.c_str();
  216. hargv[hargc++] = i->second.c_str();
  217. }
  218. redisReply *reply = (redisReply *)redisCommandArgv(_rc,hargc,hargv,(const size_t *)0);
  219. if (!reply) {
  220. if (_reconnect())
  221. return _hmset(key,hdata);
  222. return false;
  223. }
  224. if (reply->type == REDIS_REPLY_ERROR) {
  225. freeReplyObject(reply);
  226. return false;
  227. }
  228. freeReplyObject(reply);
  229. return true;
  230. }
  231. bool NetworkConfigMaster::_hget(const char *key,const char *hashKey,std::string &value)
  232. {
  233. if (!_rc) {
  234. if (!_reconnect())
  235. return false;
  236. }
  237. redisReply *reply = (redisReply *)redisCommand(_rc,"HGET %s %s",key,hashKey);
  238. if (!reply) {
  239. if (_reconnect())
  240. return _hget(key,hashKey,value);
  241. return false;
  242. }
  243. if (reply->type == REDIS_REPLY_STRING)
  244. value = reply->str;
  245. else value = "";
  246. freeReplyObject(reply);
  247. return true;
  248. }
  249. bool NetworkConfigMaster::_hset(const char *key,const char *hashKey,const char *value)
  250. {
  251. if (!_rc) {
  252. if (!_reconnect())
  253. return false;
  254. }
  255. redisReply *reply = (redisReply *)redisCommand(_rc,"HSET %s %s %s",key,hashKey,value);
  256. if (!reply) {
  257. if (_reconnect())
  258. return _hset(key,hashKey,value);
  259. return false;
  260. }
  261. if (reply->type == REDIS_REPLY_ERROR) {
  262. freeReplyObject(reply);
  263. return false;
  264. }
  265. freeReplyObject(reply);
  266. return true;
  267. }
  268. bool NetworkConfigMaster::_get(const char *key,std::string &value)
  269. {
  270. if (!_rc) {
  271. if (!_reconnect())
  272. return false;
  273. }
  274. redisReply *reply = (redisReply *)redisCommand(_rc,"GET %s",key);
  275. if (!reply) {
  276. if (_reconnect())
  277. return _get(key,value);
  278. return false;
  279. }
  280. if ((reply->type == REDIS_REPLY_STRING)&&(reply->str))
  281. value = reply->str;
  282. else value = "";
  283. freeReplyObject(reply);
  284. return true;
  285. }
  286. bool NetworkConfigMaster::_smembers(const char *key,std::vector<std::string> &sdata)
  287. {
  288. if (!_rc) {
  289. if (!_reconnect())
  290. return false;
  291. }
  292. redisReply *reply = (redisReply *)redisCommand(_rc,"SMEMBERS %s",key);
  293. if (!reply) {
  294. if (_reconnect())
  295. return _smembers(key,sdata);
  296. return false;
  297. }
  298. sdata.clear();
  299. if (reply->type == REDIS_REPLY_ARRAY) {
  300. for(long i=0;i<reply->elements;++i) {
  301. if (reply->element[i]->str)
  302. sdata.push_back(reply->element[i]->str);
  303. }
  304. }
  305. return true;
  306. }
  307. bool NetworkConfigMaster::_initNewMember(uint64_t nwid,const Address &member,const Dictionary &metaData,Dictionary &memberRecord)
  308. {
  309. char memberKey[256],nwids[24],addrs[16],nwKey[256];
  310. Dictionary networkRecord;
  311. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  312. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.toInt());
  313. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  314. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  315. if (!_hgetall(nwKey,networkRecord)) {
  316. LOG("netconf: Redis error retrieving %s",nwKey);
  317. return false;
  318. }
  319. if (networkRecord.get("id","") != nwids) {
  320. TRACE("netconf: network %s not found (initNewMember)",nwids);
  321. return false;
  322. }
  323. memberRecord.clear();
  324. memberRecord["id"] = addrs;
  325. memberRecord["nwid"] = nwids;
  326. memberRecord["authorized"] = (networkRecord.getBoolean("private",true) ? "0" : "1"); // auto-authorize on public networks
  327. memberRecord.setHex("firstSeen",Utils::now());
  328. {
  329. SharedPtr<Peer> peer(RR->topology->getPeer(member));
  330. if (peer)
  331. memberRecord["identity"] = peer->identity().toString(false);
  332. }
  333. if (!_hmset(memberKey,memberRecord)) {
  334. LOG("netconf: Redis error storing %s for new member %s",memberKey,addrs);
  335. return false;
  336. }
  337. return true;
  338. }
  339. bool NetworkConfigMaster::_generateNetconf(uint64_t nwid,const Address &member,const Dictionary &metaData,std::string &netconf,uint64_t &ts)
  340. {
  341. char memberKey[256],nwids[24],addrs[16],tss[24],nwKey[256],revKey[128],abKey[128],ipaKey[128];
  342. Dictionary networkRecord,memberRecord,nc;
  343. std::string revision;
  344. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  345. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  346. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.toInt());
  347. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  348. Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
  349. Utils::snprintf(abKey,sizeof(revKey),"zt1:network:%s:activeBridges",nwids);
  350. Utils::snprintf(ipaKey,sizeof(revKey),"zt1:network:%s:ipAssignments",nwids);
  351. if (!_hgetall(nwKey,networkRecord)) {
  352. LOG("netconf: Redis error retrieving %s",nwKey);
  353. return false;
  354. }
  355. if (networkRecord.get("id","") != nwids) {
  356. TRACE("netconf: network %s not found (generateNetconf)",nwids);
  357. return false;
  358. }
  359. if (!_hgetall(memberKey,memberRecord)) {
  360. LOG("netconf: Redis error retrieving %s",memberKey);
  361. return false;
  362. }
  363. if (!_get(revKey,revision)) {
  364. LOG("netconf: Redis error retrieving %s",revKey);
  365. return false;
  366. }
  367. if (!revision.length())
  368. revision = "0";
  369. bool isPrivate = networkRecord.getBoolean("private",true);
  370. ts = Utils::now();
  371. Utils::snprintf(tss,sizeof(tss),"%llx",ts);
  372. // Core configuration
  373. nc[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = tss;
  374. nc[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = nwids;
  375. nc[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = addrs;
  376. nc[ZT_NETWORKCONFIG_DICT_KEY_PRIVATE] = isPrivate ? "1" : "0";
  377. nc[ZT_NETWORKCONFIG_DICT_KEY_NAME] = networkRecord.get("name",nwids);
  378. nc[ZT_NETWORKCONFIG_DICT_KEY_DESC] = networkRecord.get("desc","");
  379. nc[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = networkRecord.getBoolean("enableBroadcast",true) ? "1" : "0";
  380. nc[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = networkRecord.getBoolean("allowPassiveBridging",false) ? "1" : "0";
  381. nc[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = networkRecord.get("etherTypes","");
  382. // Multicast options
  383. nc[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = networkRecord.get("multicastRates","");
  384. uint64_t ml = networkRecord.getHexUInt("multicastLimit",0);
  385. if (ml > 0)
  386. nc.setHex(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,ml);
  387. // Active bridge configuration
  388. {
  389. std::string activeBridgeList;
  390. std::vector<std::string> activeBridgeSet;
  391. if (!_smembers(abKey,activeBridgeSet)) {
  392. LOG("netconf: Redis error retrieving set %s",abKey);
  393. return false;
  394. }
  395. std::sort(activeBridgeSet.begin(),activeBridgeSet.end());
  396. for(std::vector<std::string>::const_iterator i(activeBridgeSet.begin());i!=activeBridgeSet.end();++i) {
  397. if (i->length() == 10) {
  398. if (activeBridgeList.length() > 0)
  399. activeBridgeList.push_back(',');
  400. activeBridgeList.append(*i);
  401. }
  402. }
  403. if (activeBridgeList.length() > 0)
  404. nc[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridgeList;
  405. }
  406. // IP address assignment and auto-assign using the ZeroTier-internal mechanism (not DHCP, etc.)
  407. {
  408. std::string ipAssignments(memberRecord.get("ipAssignments",""));
  409. // Get sorted, separated lists of IPv4 and IPv6 IP address assignments already present
  410. std::vector<InetAddress> ip4s,ip6s;
  411. {
  412. std::vector<std::string> ips(Utils::split(ipAssignments.c_str(),",","",""));
  413. for(std::vector<std::string>::iterator i(ips.begin());i!=ips.end();++i) {
  414. InetAddress a(*i);
  415. if (a.isV4())
  416. ip4s.push_back(a);
  417. else if (a.isV6())
  418. ip6s.push_back(a);
  419. }
  420. }
  421. std::sort(ip4s.begin(),ip4s.end());
  422. std::unique(ip4s.begin(),ip4s.end());
  423. std::sort(ip6s.begin(),ip6s.end());
  424. std::unique(ip6s.begin(),ip6s.end());
  425. // If IPv4 assignment mode is 'zt', send them to the client
  426. if (networkRecord.get("v4AssignMode","") == "zt") {
  427. // If we have no IPv4 addresses and we have an assignment pool, auto-assign
  428. if (ip4s.empty()) {
  429. InetAddress v4AssignPool(networkRecord.get("v4AssignPool",""));
  430. uint32_t pnet = Utils::ntoh(*((const uint32_t *)v4AssignPool.rawIpData()));
  431. unsigned int pbits = v4AssignPool.netmaskBits();
  432. if ((v4AssignPool.isV4())&&(pbits > 0)&&(pbits < 32)&&(pnet != 0)) {
  433. uint32_t pmask = 0xffffffff << (32 - pbits); // netmask over network part
  434. uint32_t invmask = ~pmask; // netmask over "random" part
  435. // Begin exploring the IP space by generating an IP from the ZeroTier address
  436. uint32_t first = (((uint32_t)(member.toInt() & 0xffffffffULL)) & invmask) | (pnet & pmask);
  437. if ((first & 0xff) == 0)
  438. first |= 1;
  439. else if ((first & 0xff) == 0xff)
  440. first &= 0xfe;
  441. // Start by trying this first IP
  442. uint32_t abcd = first;
  443. InetAddress ip;
  444. bool gotone = false;
  445. unsigned long sanityCounter = 0;
  446. do {
  447. // Convert to IPv4 InetAddress
  448. uint32_t abcdNetworkByteOrder = Utils::hton(abcd);
  449. ip.set(&abcdNetworkByteOrder,4,pbits);
  450. // Is 'ip' already assigned to another node?
  451. std::string assignment;
  452. if (!_hget(ipaKey,ip.toString().c_str(),assignment)) {
  453. LOG("netconf: Redis error checking IP allocation in %s",ipaKey);
  454. return false;
  455. }
  456. if ((assignment.length() != 10)||(assignment == member.toString())) {
  457. gotone = true;
  458. break; // not taken!
  459. }
  460. // If we made it here, the IP was taken so increment and mask and try again
  461. ++abcd;
  462. abcd &= invmask;
  463. abcd |= (pnet & pmask);
  464. if ((abcd & 0xff) == 0)
  465. abcd |= 1;
  466. else if ((abcd & 0xff) == 0xff)
  467. abcd &= 0xfe;
  468. // Don't spend insane amounts of time here -- if we have to try this hard, the user
  469. // needs to allocate a larger IP block.
  470. if (++sanityCounter >= 65535)
  471. break;
  472. } while (abcd != first); // keep going until we loop back around to 'first'
  473. // If we got one, add to IP list and claim in database
  474. if (gotone) {
  475. ip4s.push_back(ip);
  476. _hset(ipaKey,ip.toString().c_str(),member.toString().c_str());
  477. if (ipAssignments.length() > 0)
  478. ipAssignments.push_back(',');
  479. ipAssignments.append(ip.toString());
  480. _hset(memberKey,"ipAssignments",ipAssignments.c_str());
  481. } else {
  482. LOG("netconf: failed to allocate IP in %s for %s in network %s, need a larger pool!",v4AssignPool.toString().c_str(),addrs,nwids);
  483. }
  484. }
  485. }
  486. // Create comma-delimited list to send to client
  487. std::string v4s;
  488. for(std::vector<InetAddress>::iterator i(ip4s.begin());i!=ip4s.end();++i) {
  489. if (v4s.length() > 0)
  490. v4s.push_back(',');
  491. v4s.append(i->toString());
  492. }
  493. if (v4s.length())
  494. nc[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
  495. }
  496. if (networkRecord.get("v6AssignMode","") == "zt") {
  497. // TODO: IPv6 auto-assign ... not quite baked yet. :)
  498. std::string v6s;
  499. for(std::vector<InetAddress>::iterator i(ip6s.begin());i!=ip6s.end();++i) {
  500. if (v6s.length() > 0)
  501. v6s.push_back(',');
  502. v6s.append(i->toString());
  503. }
  504. if (v6s.length())
  505. nc[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = v6s;
  506. }
  507. }
  508. // If this is a private network, generate a signed certificate of membership
  509. if (isPrivate) {
  510. CertificateOfMembership com(Utils::strToU64(revision.c_str()),1,nwid,member);
  511. if (com.sign(RR->identity)) // basically can't fail unless our identity is invalid
  512. nc[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
  513. else {
  514. LOG("netconf: failure signing certificate (identity problem?)");
  515. return false;
  516. }
  517. }
  518. // Sign netconf dictionary itself
  519. if (!nc.sign(RR->identity)) {
  520. LOG("netconf: failure signing dictionary (identity problem?)");
  521. return false;
  522. }
  523. // Convert to string-serialized form into result paramter
  524. netconf = nc.toString();
  525. // Record new netconf in database for re-use on subsequent repeat queries
  526. {
  527. Dictionary upd;
  528. upd["netconf"] = netconf;
  529. upd["netconfTimestamp"] = tss;
  530. upd["netconfRevision"] = revision;
  531. if (!_hmset(memberKey,upd)) {
  532. LOG("netconf: Redis error writing to key %s",memberKey);
  533. return false;
  534. }
  535. }
  536. return true;
  537. }
  538. } // namespace ZeroTier
  539. #endif // ZT_ENABLE_NETCONF_MASTER