RedisNetworkConfigMaster.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 ID field";
  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::_sadd(const char *key,const char *value)
  269. {
  270. if (!_rc) {
  271. if (!_reconnect())
  272. return false;
  273. }
  274. redisReply *reply = (redisReply *)redisCommand(_rc,"SADD %s %s",key,value);
  275. if (!reply) {
  276. if (_reconnect())
  277. return _sadd(key,value);
  278. return false;
  279. }
  280. if (reply->type == REDIS_REPLY_ERROR) {
  281. freeReplyObject(reply);
  282. return false;
  283. }
  284. freeReplyObject(reply);
  285. return true;
  286. }
  287. bool RedisNetworkConfigMaster::_smembers(const char *key,std::vector<std::string> &sdata)
  288. {
  289. if (!_rc) {
  290. if (!_reconnect())
  291. return false;
  292. }
  293. redisReply *reply = (redisReply *)redisCommand(_rc,"SMEMBERS %s",key);
  294. if (!reply) {
  295. if (_reconnect())
  296. return _smembers(key,sdata);
  297. return false;
  298. }
  299. sdata.clear();
  300. if (reply->type == REDIS_REPLY_ARRAY) {
  301. for(long i=0;i<reply->elements;++i) {
  302. if (reply->element[i]->str)
  303. sdata.push_back(reply->element[i]->str);
  304. }
  305. }
  306. return true;
  307. }
  308. bool RedisNetworkConfigMaster::_initNewMember(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &memberRecord)
  309. {
  310. char memberKey[128],nwids[24],addrs[16],nwKey[128],membersKey[128];
  311. Dictionary networkRecord;
  312. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  313. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
  314. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  315. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  316. Utils::snprintf(membersKey,sizeof(membersKey),"zt1:network:%s:members",nwids);
  317. if (!_hgetall(nwKey,networkRecord)) {
  318. //LOG("netconf: Redis error retrieving %s",nwKey);
  319. return false;
  320. }
  321. if (networkRecord.get("id","") != nwids) {
  322. //TRACE("netconf: network %s not found (initNewMember)",nwids);
  323. return false;
  324. }
  325. memberRecord.clear();
  326. memberRecord["id"] = addrs;
  327. memberRecord["nwid"] = nwids;
  328. memberRecord["authorized"] = ((networkRecord.get("private","1") == "0") ? "1" : "0"); // auto-authorize on public networks
  329. memberRecord.set("firstSeen",Utils::now());
  330. memberRecord["identity"] = member.toString(false);
  331. if (!_hmset(memberKey,memberRecord))
  332. return false;
  333. if (!_sadd(membersKey,addrs))
  334. return false;
  335. return true;
  336. }
  337. bool RedisNetworkConfigMaster::_generateNetconf(uint64_t nwid,const Identity &member,const Dictionary &metaData,Dictionary &netconf,uint64_t &ts,std::string &errorMessage)
  338. {
  339. char memberKey[256],nwids[24],addrs[16],tss[24],nwKey[256],revKey[128],abKey[128],ipaKey[128];
  340. Dictionary networkRecord,memberRecord;
  341. std::string revision;
  342. Utils::snprintf(memberKey,sizeof(memberKey),"zt1:network:%s:member:%s:~",nwids,addrs);
  343. Utils::snprintf(nwids,sizeof(nwids),"%.16llx",(unsigned long long)nwid);
  344. Utils::snprintf(addrs,sizeof(addrs),"%.10llx",(unsigned long long)member.address().toInt());
  345. Utils::snprintf(nwKey,sizeof(nwKey),"zt1:network:%s:~",nwids);
  346. Utils::snprintf(revKey,sizeof(revKey),"zt1:network:%s:revision",nwids);
  347. Utils::snprintf(abKey,sizeof(revKey),"zt1:network:%s:activeBridges",nwids);
  348. Utils::snprintf(ipaKey,sizeof(revKey),"zt1:network:%s:ipAssignments",nwids);
  349. if (!_hgetall(nwKey,networkRecord)) {
  350. errorMessage = "Redis error retrieving network record";
  351. return false;
  352. }
  353. if (networkRecord.get("id","") != nwids) {
  354. errorMessage = "network IDs do not match in database";
  355. return false;
  356. }
  357. if (!_hgetall(memberKey,memberRecord)) {
  358. errorMessage = "Redis error retrieving member record";
  359. return false;
  360. }
  361. if (!_get(revKey,revision)) {
  362. errorMessage = "Redis error retrieving network revision";
  363. return false;
  364. }
  365. if (!revision.length())
  366. revision = "0";
  367. bool isPrivate = networkRecord.getBoolean("private",true);
  368. ts = Utils::now();
  369. Utils::snprintf(tss,sizeof(tss),"%llx",ts);
  370. // Core configuration
  371. netconf[ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP] = tss;
  372. netconf[ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID] = nwids;
  373. netconf[ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO] = addrs;
  374. netconf[ZT_NETWORKCONFIG_DICT_KEY_PRIVATE] = isPrivate ? "1" : "0";
  375. netconf[ZT_NETWORKCONFIG_DICT_KEY_NAME] = networkRecord.get("name",nwids);
  376. netconf[ZT_NETWORKCONFIG_DICT_KEY_DESC] = networkRecord.get("desc","");
  377. netconf[ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST] = networkRecord.getBoolean("enableBroadcast",true) ? "1" : "0";
  378. netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOW_PASSIVE_BRIDGING] = networkRecord.getBoolean("allowPassiveBridging",false) ? "1" : "0";
  379. netconf[ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES] = networkRecord.get("etherTypes",""); // these are stored as hex comma-delimited list
  380. // Multicast options
  381. netconf[ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_RATES] = networkRecord.get("multicastRates","");
  382. uint64_t ml = networkRecord.getHexUInt("multicastLimit",0);
  383. if (ml > 0)
  384. netconf.setHex(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,ml);
  385. // Active bridge configuration
  386. {
  387. std::string activeBridgeList;
  388. std::vector<std::string> activeBridgeSet;
  389. if (!_smembers(abKey,activeBridgeSet)) {
  390. errorMessage = "Redis error retrieving active bridge set";
  391. return false;
  392. }
  393. std::sort(activeBridgeSet.begin(),activeBridgeSet.end());
  394. for(std::vector<std::string>::const_iterator i(activeBridgeSet.begin());i!=activeBridgeSet.end();++i) {
  395. if (i->length() == 10) {
  396. if (activeBridgeList.length() > 0)
  397. activeBridgeList.push_back(',');
  398. activeBridgeList.append(*i);
  399. }
  400. }
  401. if (activeBridgeList.length() > 0)
  402. netconf[ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES] = activeBridgeList;
  403. }
  404. // IP address assignment and auto-assign using the ZeroTier-internal mechanism (not DHCP, etc.)
  405. {
  406. std::string ipAssignments(memberRecord.get("ipAssignments",""));
  407. // Get sorted, separated lists of IPv4 and IPv6 IP address assignments already present
  408. std::vector<InetAddress> ip4s,ip6s;
  409. {
  410. std::vector<std::string> ips(Utils::split(ipAssignments.c_str(),",","",""));
  411. for(std::vector<std::string>::iterator i(ips.begin());i!=ips.end();++i) {
  412. InetAddress a(*i);
  413. if (a.isV4())
  414. ip4s.push_back(a);
  415. else if (a.isV6())
  416. ip6s.push_back(a);
  417. }
  418. }
  419. std::sort(ip4s.begin(),ip4s.end());
  420. std::unique(ip4s.begin(),ip4s.end());
  421. std::sort(ip6s.begin(),ip6s.end());
  422. std::unique(ip6s.begin(),ip6s.end());
  423. // If IPv4 assignment mode is 'zt', send them to the client
  424. if (networkRecord.get("v4AssignMode","") == "zt") {
  425. // If we have no IPv4 addresses and we have an assignment pool, auto-assign
  426. if (ip4s.empty()) {
  427. InetAddress v4AssignPool(networkRecord.get("v4AssignPool",""));
  428. uint32_t pnet = Utils::ntoh(*((const uint32_t *)v4AssignPool.rawIpData()));
  429. unsigned int pbits = v4AssignPool.netmaskBits();
  430. if ((v4AssignPool.isV4())&&(pbits > 0)&&(pbits < 32)&&(pnet != 0)) {
  431. uint32_t pmask = 0xffffffff << (32 - pbits); // netmask over network part
  432. uint32_t invmask = ~pmask; // netmask over "random" part
  433. // Begin exploring the IP space by generating an IP from the ZeroTier address
  434. uint32_t first = (((uint32_t)(member.address().toInt() & 0xffffffffULL)) & invmask) | (pnet & pmask);
  435. if ((first & 0xff) == 0)
  436. first |= 1;
  437. else if ((first & 0xff) == 0xff)
  438. first &= 0xfe;
  439. // Start by trying this first IP
  440. uint32_t abcd = first;
  441. InetAddress ip;
  442. bool gotone = false;
  443. unsigned long sanityCounter = 0;
  444. do {
  445. // Convert to IPv4 InetAddress
  446. uint32_t abcdNetworkByteOrder = Utils::hton(abcd);
  447. ip.set(&abcdNetworkByteOrder,4,pbits);
  448. // Is 'ip' already assigned to another node?
  449. std::string assignment;
  450. if (!_hget(ipaKey,ip.toString().c_str(),assignment)) {
  451. errorMessage = "Redis error while checking IP allocation";
  452. return false;
  453. }
  454. if ((assignment.length() != 10)||(assignment == member.address().toString())) {
  455. gotone = true;
  456. break; // not taken!
  457. }
  458. // If we made it here, the IP was taken so increment and mask and try again
  459. ++abcd;
  460. abcd &= invmask;
  461. abcd |= (pnet & pmask);
  462. if ((abcd & 0xff) == 0)
  463. abcd |= 1;
  464. else if ((abcd & 0xff) == 0xff)
  465. abcd &= 0xfe;
  466. // Don't spend insane amounts of time here -- if we have to try this hard, the user
  467. // needs to allocate a larger IP block.
  468. if (++sanityCounter >= 65535)
  469. break;
  470. } while (abcd != first); // keep going until we loop back around to 'first'
  471. // If we got one, add to IP list and claim in database
  472. if (gotone) {
  473. ip4s.push_back(ip);
  474. _hset(ipaKey,ip.toString().c_str(),member.address().toString().c_str());
  475. if (ipAssignments.length() > 0)
  476. ipAssignments.push_back(',');
  477. ipAssignments.append(ip.toString());
  478. _hset(memberKey,"ipAssignments",ipAssignments.c_str());
  479. } else {
  480. char tmp[1024];
  481. 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);
  482. errorMessage = tmp;
  483. return false;
  484. }
  485. }
  486. }
  487. // Create comma-delimited list to send to client
  488. std::string v4s;
  489. for(std::vector<InetAddress>::iterator i(ip4s.begin());i!=ip4s.end();++i) {
  490. if (v4s.length() > 0)
  491. v4s.push_back(',');
  492. v4s.append(i->toString());
  493. }
  494. if (v4s.length())
  495. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC] = v4s;
  496. }
  497. if (networkRecord.get("v6AssignMode","") == "zt") {
  498. // TODO: IPv6 auto-assign ... not quite baked yet. :)
  499. std::string v6s;
  500. for(std::vector<InetAddress>::iterator i(ip6s.begin());i!=ip6s.end();++i) {
  501. if (v6s.length() > 0)
  502. v6s.push_back(',');
  503. v6s.append(i->toString());
  504. }
  505. if (v6s.length())
  506. netconf[ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC] = v6s;
  507. }
  508. }
  509. // If this is a private network, generate a signed certificate of membership
  510. if (isPrivate) {
  511. CertificateOfMembership com(Utils::strToU64(revision.c_str()),1,nwid,member.address());
  512. if (com.sign(_signingId)) // basically can't fail unless our identity is invalid
  513. netconf[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP] = com.toString();
  514. else {
  515. errorMessage = "unable to sign COM";
  516. return false;
  517. }
  518. }
  519. // Sign netconf dictionary itself
  520. if (!netconf.sign(_signingId)) {
  521. errorMessage = "unable to sign netconf dictionary";
  522. return false;
  523. }
  524. // Record new netconf in database for re-use on subsequent repeat queries
  525. {
  526. Dictionary upd;
  527. upd["netconf"] = netconf.toString();
  528. upd.set("netconfTimestamp",ts);
  529. upd["netconfRevision"] = revision;
  530. if (!_hmset(memberKey,upd)) {
  531. errorMessage = "Redis error updating network record with new netconf dictionary";
  532. return false;
  533. }
  534. }
  535. return true;
  536. }
  537. } // namespace ZeroTier