2013-10-18 13:48:02 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Use of this software is governed by the Business Source License included
|
|
|
|
* in the LICENSE.TXT file in the project's root directory.
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2013-10-18 13:48:02 +00:00
|
|
|
*
|
2019-08-23 16:23:39 +00:00
|
|
|
* On the date above, in accordance with the Business Source License, use
|
|
|
|
* of this software will be governed by version 2.0 of the Apache License.
|
2013-10-18 13:48:02 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2013-10-18 13:48:02 +00:00
|
|
|
|
2014-10-07 21:34:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
#include "NetworkConfig.hpp"
|
|
|
|
|
|
|
|
namespace ZeroTier {
|
|
|
|
|
2016-06-21 14:32:58 +00:00
|
|
|
bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const
|
2013-10-18 15:01:41 +00:00
|
|
|
{
|
2016-08-09 15:32:42 +00:00
|
|
|
Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
|
2020-05-20 00:40:51 +00:00
|
|
|
char tmp2[128] = {0};
|
2016-04-26 15:40:26 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
try {
|
|
|
|
d.clear();
|
2016-04-12 19:11:34 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
// Try to put the more human-readable fields first
|
2013-10-18 15:01:41 +00:00
|
|
|
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,this->credentialTimeMaxDelta)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo.toString(tmp2))) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET,this->remoteTraceTarget.toString(tmp2))) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL,(uint64_t)this->remoteTraceLevel)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)this->type)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MTU,(uint64_t)this->mtu)) {
|
2023-05-03 17:14:18 +00:00
|
|
|
delete tmp;
|
2023-05-01 18:48:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-01-06 01:47:59 +00:00
|
|
|
|
2016-06-16 19:28:43 +00:00
|
|
|
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
|
2016-08-09 15:32:42 +00:00
|
|
|
if (includeLegacy) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD,this->enableBroadcast())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,this->isPrivate())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
|
|
|
|
std::string v4s;
|
|
|
|
for(unsigned int i=0;i<staticIpCount;++i) {
|
|
|
|
if (this->staticIps[i].ss_family == AF_INET) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (v4s.length() > 0) {
|
2016-08-09 15:32:42 +00:00
|
|
|
v4s.push_back(',');
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2017-07-06 23:11:11 +00:00
|
|
|
char buf[64];
|
|
|
|
v4s.append(this->staticIps[i].toString(buf));
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (v4s.length() > 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,v4s.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
std::string v6s;
|
|
|
|
for(unsigned int i=0;i<staticIpCount;++i) {
|
|
|
|
if (this->staticIps[i].ss_family == AF_INET6) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (v6s.length() > 0) {
|
2016-08-09 15:32:42 +00:00
|
|
|
v6s.push_back(',');
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2017-07-06 23:11:11 +00:00
|
|
|
char buf[64];
|
|
|
|
v6s.append(this->staticIps[i].toString(buf));
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (v6s.length() > 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,v6s.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
2016-04-12 19:11:34 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
std::string ets;
|
|
|
|
unsigned int et = 0;
|
|
|
|
ZT_VirtualNetworkRuleType lastrt = ZT_NETWORK_RULE_ACTION_ACCEPT;
|
|
|
|
for(unsigned int i=0;i<ruleCount;++i) {
|
|
|
|
ZT_VirtualNetworkRuleType rt = (ZT_VirtualNetworkRuleType)(rules[i].t & 0x7f);
|
|
|
|
if (rt == ZT_NETWORK_RULE_MATCH_ETHERTYPE) {
|
|
|
|
et = rules[i].v.etherType;
|
|
|
|
} else if (rt == ZT_NETWORK_RULE_ACTION_ACCEPT) {
|
|
|
|
if (((int)lastrt < 32)||(lastrt == ZT_NETWORK_RULE_MATCH_ETHERTYPE)) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (ets.length() > 0) {
|
2016-08-09 15:32:42 +00:00
|
|
|
ets.push_back(',');
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2020-05-20 00:40:51 +00:00
|
|
|
char tmp2[16] = {0};
|
2017-07-06 23:11:11 +00:00
|
|
|
ets.append(Utils::hex((uint16_t)et,tmp2));
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
et = 0;
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
lastrt = rt;
|
|
|
|
}
|
|
|
|
if (ets.length() > 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,ets.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
2016-04-26 15:40:26 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (this->com) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,this->com.toString().c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-04-12 19:11:34 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
std::string ab;
|
|
|
|
for(unsigned int i=0;i<this->specialistCount;++i) {
|
|
|
|
if ((this->specialists[i] & ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE) != 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (ab.length() > 0) {
|
2016-08-09 15:32:42 +00:00
|
|
|
ab.push_back(',');
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2020-05-20 00:40:51 +00:00
|
|
|
char tmp2[16] = {0};
|
2017-07-06 23:11:11 +00:00
|
|
|
ab.append(Address(this->specialists[i]).toString(tmp2));
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ab.length() > 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,ab.c_str())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // ZT_SUPPORT_OLD_STYLE_NETCONF
|
2013-10-18 15:01:41 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
// Then add binary blobs
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (this->com) {
|
|
|
|
tmp->clear();
|
|
|
|
this->com.serialize(*tmp);
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
tmp->clear();
|
2023-05-01 18:48:16 +00:00
|
|
|
for(unsigned int i=0;i<this->capabilityCount;++i) {
|
2016-08-09 15:32:42 +00:00
|
|
|
this->capabilities[i].serialize(*tmp);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
tmp->clear();
|
2023-05-01 18:48:16 +00:00
|
|
|
for(unsigned int i=0;i<this->tagCount;++i) {
|
2016-08-09 15:32:42 +00:00
|
|
|
this->tags[i].serialize(*tmp);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2017-02-23 19:47:36 +00:00
|
|
|
tmp->clear();
|
2023-05-01 18:48:16 +00:00
|
|
|
for(unsigned int i=0;i<this->certificateOfOwnershipCount;++i) {
|
2017-02-23 19:47:36 +00:00
|
|
|
this->certificatesOfOwnership[i].serialize(*tmp);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2017-02-23 19:47:36 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-23 19:47:36 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
tmp->clear();
|
2023-05-01 18:48:16 +00:00
|
|
|
for(unsigned int i=0;i<this->specialistCount;++i) {
|
2016-08-09 15:32:42 +00:00
|
|
|
tmp->append((uint64_t)this->specialists[i]);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
tmp->clear();
|
|
|
|
for(unsigned int i=0;i<this->routeCount;++i) {
|
|
|
|
reinterpret_cast<const InetAddress *>(&(this->routes[i].target))->serialize(*tmp);
|
|
|
|
reinterpret_cast<const InetAddress *>(&(this->routes[i].via))->serialize(*tmp);
|
|
|
|
tmp->append((uint16_t)this->routes[i].flags);
|
|
|
|
tmp->append((uint16_t)this->routes[i].metric);
|
2016-04-12 19:11:34 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmp->clear();
|
2023-05-01 18:48:16 +00:00
|
|
|
for(unsigned int i=0;i<this->staticIpCount;++i) {
|
2016-08-09 15:32:42 +00:00
|
|
|
this->staticIps[i].serialize(*tmp);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->ruleCount) {
|
|
|
|
tmp->clear();
|
|
|
|
Capability::serializeRules(*tmp,rules,ruleCount);
|
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 21:34:19 +00:00
|
|
|
tmp->clear();
|
2020-08-12 20:08:47 +00:00
|
|
|
DNS::serializeDNS(*tmp, &dns);
|
|
|
|
if (tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_DNS,*tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-07-20 21:34:19 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 22:40:08 +00:00
|
|
|
if (this->ssoVersion == 0) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, this->ssoVersion)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, this->ssoEnabled)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-11-04 22:40:08 +00:00
|
|
|
|
|
|
|
if (this->ssoEnabled) {
|
|
|
|
if (this->authenticationURL[0]) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_EXPIRY_TIME, this->authenticationExpiryTime)) {
|
|
|
|
return false;
|
2021-11-04 22:40:08 +00:00
|
|
|
}
|
2021-06-05 20:44:45 +00:00
|
|
|
}
|
2021-11-04 22:40:08 +00:00
|
|
|
} else if(this->ssoVersion == 1) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, this->ssoVersion)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, this->ssoEnabled)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-12-01 20:07:05 +00:00
|
|
|
//if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL)) return false;
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUER_URL, this->issuerURL)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CENTRAL_ENDPOINT_URL, this->centralAuthURL)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NONCE, this->ssoNonce)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATE, this->ssoState)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CLIENT_ID, this->ssoClientID)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SSO_PROVIDER, this->ssoProvider)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-05-25 02:58:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
delete tmp;
|
|
|
|
} catch ( ... ) {
|
|
|
|
delete tmp;
|
|
|
|
throw;
|
2015-06-13 11:06:49 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-08-23 18:52:10 +00:00
|
|
|
bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d)
|
2016-06-16 19:28:43 +00:00
|
|
|
{
|
2019-03-25 22:19:52 +00:00
|
|
|
static const NetworkConfig NIL_NC;
|
2016-08-09 15:32:42 +00:00
|
|
|
Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
try {
|
2019-03-25 22:19:52 +00:00
|
|
|
*this = NIL_NC;
|
2016-06-16 19:28:43 +00:00
|
|
|
|
|
|
|
// Fields that are always present, new or old
|
|
|
|
this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
|
2016-08-09 15:32:42 +00:00
|
|
|
if (!this->networkId) {
|
|
|
|
delete tmp;
|
2016-06-16 19:28:43 +00:00
|
|
|
return false;
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
|
2016-09-07 19:12:52 +00:00
|
|
|
this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,0);
|
2016-06-16 19:28:43 +00:00
|
|
|
this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
|
|
|
|
this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
|
2016-08-09 15:32:42 +00:00
|
|
|
if (!this->issuedTo) {
|
|
|
|
delete tmp;
|
2016-06-16 19:28:43 +00:00
|
|
|
return false;
|
2016-08-09 15:32:42 +00:00
|
|
|
}
|
2017-07-07 23:58:05 +00:00
|
|
|
this->remoteTraceTarget = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET);
|
2017-12-04 22:40:10 +00:00
|
|
|
this->remoteTraceLevel = (Trace::Level)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL);
|
2016-06-16 19:28:43 +00:00
|
|
|
this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
|
|
|
|
d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
|
|
|
|
|
2017-05-05 00:12:02 +00:00
|
|
|
this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU,ZT_DEFAULT_MTU);
|
2023-05-01 18:48:16 +00:00
|
|
|
if (this->mtu < 1280) {
|
2017-05-05 00:12:02 +00:00
|
|
|
this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
|
2023-05-01 18:48:16 +00:00
|
|
|
} else if (this->mtu > ZT_MAX_MTU) {
|
2017-05-05 00:12:02 +00:00
|
|
|
this->mtu = ZT_MAX_MTU;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2017-05-05 00:12:02 +00:00
|
|
|
|
2016-06-16 22:48:58 +00:00
|
|
|
if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
|
2016-06-16 19:28:43 +00:00
|
|
|
#ifdef ZT_SUPPORT_OLD_STYLE_NETCONF
|
2020-05-20 00:40:51 +00:00
|
|
|
char tmp2[1024] = {0};
|
2016-08-09 15:32:42 +00:00
|
|
|
|
2016-06-16 19:28:43 +00:00
|
|
|
// Decode legacy fields if version is old
|
2023-05-01 18:48:16 +00:00
|
|
|
if (d.getB(ZT_NETWORKCONFIG_DICT_KEY_ENABLE_BROADCAST_OLD)) {
|
2016-06-16 19:28:43 +00:00
|
|
|
this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_BROADCAST;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-06-24 05:41:14 +00:00
|
|
|
this->flags |= ZT_NETWORKCONFIG_FLAG_ENABLE_IPV6_NDP_EMULATION; // always enable for old-style netconf
|
2016-06-16 19:28:43 +00:00
|
|
|
this->type = (d.getB(ZT_NETWORKCONFIG_DICT_KEY_PRIVATE_OLD,true)) ? ZT_NETWORK_TYPE_PRIVATE : ZT_NETWORK_TYPE_PUBLIC;
|
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV4_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
|
|
|
|
char *saveptr = (char *)0;
|
|
|
|
for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
|
|
|
|
break;
|
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
InetAddress ip(f);
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!ip.isNetwork()) {
|
2016-06-16 22:48:58 +00:00
|
|
|
this->staticIps[this->staticIpCount++] = ip;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_IPV6_STATIC_OLD,tmp2,sizeof(tmp2)) > 0) {
|
|
|
|
char *saveptr = (char *)0;
|
|
|
|
for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (this->staticIpCount >= ZT_MAX_ZT_ASSIGNED_ADDRESSES) {
|
|
|
|
break;
|
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
InetAddress ip(f);
|
2023-05-01 18:48:16 +00:00
|
|
|
if (!ip.isNetwork()) {
|
2016-06-16 22:48:58 +00:00
|
|
|
this->staticIps[this->staticIpCount++] = ip;
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATE_OF_MEMBERSHIP_OLD,tmp2,sizeof(tmp2)) > 0) {
|
|
|
|
this->com.fromString(tmp2);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ALLOWED_ETHERNET_TYPES_OLD,tmp2,sizeof(tmp2)) > 0) {
|
|
|
|
char *saveptr = (char *)0;
|
|
|
|
for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
|
|
|
unsigned int et = Utils::hexStrToUInt(f) & 0xffff;
|
2023-05-01 18:48:16 +00:00
|
|
|
if ((this->ruleCount + 2) > ZT_MAX_NETWORK_RULES) {
|
|
|
|
break;
|
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
if (et > 0) {
|
|
|
|
this->rules[this->ruleCount].t = (uint8_t)ZT_NETWORK_RULE_MATCH_ETHERTYPE;
|
|
|
|
this->rules[this->ruleCount].v.etherType = (uint16_t)et;
|
|
|
|
++this->ruleCount;
|
|
|
|
}
|
|
|
|
this->rules[this->ruleCount++].t = (uint8_t)ZT_NETWORK_RULE_ACTION_ACCEPT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this->rules[0].t = ZT_NETWORK_RULE_ACTION_ACCEPT;
|
|
|
|
this->ruleCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ACTIVE_BRIDGES_OLD,tmp2,sizeof(tmp2)) > 0) {
|
|
|
|
char *saveptr = (char *)0;
|
|
|
|
for(char *f=Utils::stok(tmp2,",",&saveptr);(f);f=Utils::stok((char *)0,",",&saveptr)) {
|
2017-02-04 08:23:31 +00:00
|
|
|
this->addSpecialist(Address(Utils::hexStrToU64(f)),ZT_NETWORKCONFIG_SPECIALIST_TYPE_ACTIVE_BRIDGE);
|
2016-04-12 19:11:34 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
#else
|
2016-08-09 15:32:42 +00:00
|
|
|
delete tmp;
|
2016-06-16 19:28:43 +00:00
|
|
|
return false;
|
|
|
|
#endif // ZT_SUPPORT_OLD_STYLE_NETCONF
|
|
|
|
} else {
|
|
|
|
// Otherwise we can use the new fields
|
|
|
|
this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
|
|
|
|
this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
|
2016-04-27 00:11:25 +00:00
|
|
|
|
2023-05-01 18:48:16 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp)) {
|
2016-08-09 15:32:42 +00:00
|
|
|
this->com.deserialize(*tmp,0);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-08-09 15:32:42 +00:00
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) {
|
|
|
|
try {
|
|
|
|
unsigned int p = 0;
|
|
|
|
while (p < tmp->size()) {
|
|
|
|
Capability cap;
|
|
|
|
p += cap.deserialize(*tmp,p);
|
|
|
|
this->capabilities[this->capabilityCount++] = cap;
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) {
|
|
|
|
try {
|
|
|
|
unsigned int p = 0;
|
|
|
|
while (p < tmp->size()) {
|
|
|
|
Tag tag;
|
|
|
|
p += tag.deserialize(*tmp,p);
|
|
|
|
this->tags[this->tagCount++] = tag;
|
|
|
|
}
|
|
|
|
} catch ( ... ) {}
|
|
|
|
std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
2016-04-27 00:11:25 +00:00
|
|
|
|
2017-02-23 19:47:36 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) {
|
|
|
|
unsigned int p = 0;
|
|
|
|
while (p < tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP) {
|
2017-02-23 19:47:36 +00:00
|
|
|
p += certificatesOfOwnership[certificateOfOwnershipCount++].deserialize(*tmp,p);
|
2023-05-01 18:48:16 +00:00
|
|
|
} else {
|
2017-02-23 19:47:36 +00:00
|
|
|
CertificateOfOwnership foo;
|
|
|
|
p += foo.deserialize(*tmp,p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) {
|
2016-06-16 19:28:43 +00:00
|
|
|
unsigned int p = 0;
|
2017-02-23 19:47:36 +00:00
|
|
|
while ((p + 8) <= tmp->size()) {
|
2023-05-01 18:48:16 +00:00
|
|
|
if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
|
2017-02-23 19:47:36 +00:00
|
|
|
this->specialists[this->specialistCount++] = tmp->at<uint64_t>(p);
|
2023-05-01 18:48:16 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
p += 8;
|
|
|
|
}
|
2016-04-27 00:11:25 +00:00
|
|
|
}
|
2016-06-16 19:28:43 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) {
|
2016-06-16 19:28:43 +00:00
|
|
|
unsigned int p = 0;
|
2016-08-09 15:32:42 +00:00
|
|
|
while ((p < tmp->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
|
|
|
|
p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(*tmp,p);
|
|
|
|
p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(*tmp,p);
|
2023-05-01 18:48:16 +00:00
|
|
|
this->routes[this->routeCount].flags = tmp->at<uint16_t>(p);
|
|
|
|
p += 2;
|
|
|
|
this->routes[this->routeCount].metric = tmp->at<uint16_t>(p);
|
|
|
|
p += 2;
|
2016-06-16 19:28:43 +00:00
|
|
|
++this->routeCount;
|
|
|
|
}
|
2016-04-12 19:11:34 +00:00
|
|
|
}
|
2014-06-10 22:47:20 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) {
|
2016-06-16 19:28:43 +00:00
|
|
|
unsigned int p = 0;
|
2016-08-09 15:32:42 +00:00
|
|
|
while ((p < tmp->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
|
|
|
|
p += this->staticIps[this->staticIpCount++].deserialize(*tmp,p);
|
2016-06-16 19:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) {
|
|
|
|
this->ruleCount = 0;
|
2016-06-16 19:28:43 +00:00
|
|
|
unsigned int p = 0;
|
2016-08-09 15:32:42 +00:00
|
|
|
Capability::deserializeRules(*tmp,p,this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES);
|
2016-04-27 00:11:25 +00:00
|
|
|
}
|
2020-07-20 21:34:19 +00:00
|
|
|
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_DNS, *tmp)) {
|
|
|
|
unsigned int p = 0;
|
2020-08-12 20:08:47 +00:00
|
|
|
DNS::deserializeDNS(*tmp, p, &dns);
|
2020-07-20 21:34:19 +00:00
|
|
|
}
|
2021-05-25 02:58:17 +00:00
|
|
|
|
2021-11-04 22:40:08 +00:00
|
|
|
this->ssoVersion = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_SSO_VERSION, 0ULL);
|
2021-06-05 20:44:45 +00:00
|
|
|
this->ssoEnabled = d.getB(ZT_NETWORKCONFIG_DICT_KEY_SSO_ENABLED, false);
|
2021-11-04 22:40:08 +00:00
|
|
|
|
|
|
|
if (this->ssoVersion == 0) {
|
|
|
|
// implicit flow
|
|
|
|
if (this->ssoEnabled) {
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL, (unsigned int)sizeof(this->authenticationURL)) > 0) {
|
|
|
|
this->authenticationURL[sizeof(this->authenticationURL) - 1] = 0; // ensure null terminated
|
|
|
|
} else {
|
|
|
|
this->authenticationURL[0] = 0;
|
|
|
|
}
|
|
|
|
this->authenticationExpiryTime = d.getI(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_EXPIRY_TIME, 0);
|
2021-06-05 20:44:45 +00:00
|
|
|
} else {
|
|
|
|
this->authenticationURL[0] = 0;
|
2021-11-04 22:40:08 +00:00
|
|
|
this->authenticationExpiryTime = 0;
|
|
|
|
}
|
|
|
|
} else if (this->ssoVersion == 1) {
|
|
|
|
// full flow
|
|
|
|
if (this->ssoEnabled) {
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_AUTHENTICATION_URL, this->authenticationURL, (unsigned int)sizeof(this->authenticationURL)) > 0) {
|
|
|
|
this->authenticationURL[sizeof(this->authenticationURL) - 1] = 0;
|
|
|
|
}
|
2021-11-12 00:19:26 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ISSUER_URL, this->issuerURL, (unsigned int)sizeof(this->issuerURL)) > 0) {
|
|
|
|
this->issuerURL[sizeof(this->issuerURL) - 1] = 0;
|
|
|
|
}
|
2021-11-04 22:40:08 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CENTRAL_ENDPOINT_URL, this->centralAuthURL, (unsigned int)sizeof(this->centralAuthURL)) > 0) {
|
|
|
|
this->centralAuthURL[sizeof(this->centralAuthURL) - 1] = 0;
|
|
|
|
}
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_NONCE, this->ssoNonce, (unsigned int)sizeof(this->ssoNonce)) > 0) {
|
|
|
|
this->ssoNonce[sizeof(this->ssoNonce) - 1] = 0;
|
|
|
|
}
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATE, this->ssoState, (unsigned int)sizeof(this->ssoState)) > 0) {
|
|
|
|
this->ssoState[sizeof(this->ssoState) - 1] = 0;
|
|
|
|
}
|
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CLIENT_ID, this->ssoClientID, (unsigned int)sizeof(this->ssoClientID)) > 0) {
|
|
|
|
this->ssoClientID[sizeof(this->ssoClientID) - 1] = 0;
|
|
|
|
}
|
2023-01-19 23:39:15 +00:00
|
|
|
if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SSO_PROVIDER, this->ssoProvider, (unsigned int)(sizeof(this->ssoProvider))) > 0) {
|
|
|
|
this->ssoProvider[sizeof(this->ssoProvider) - 1] = 0;
|
|
|
|
} else {
|
|
|
|
strncpy(this->ssoProvider, "default", sizeof(this->ssoProvider));
|
|
|
|
this->ssoProvider[sizeof(this->ssoProvider) - 1] = 0;
|
|
|
|
}
|
2021-11-04 22:40:08 +00:00
|
|
|
} else {
|
|
|
|
this->authenticationURL[0] = 0;
|
|
|
|
this->authenticationExpiryTime = 0;
|
|
|
|
this->centralAuthURL[0] = 0;
|
|
|
|
this->ssoNonce[0] = 0;
|
|
|
|
this->ssoState[0] = 0;
|
|
|
|
this->ssoClientID[0] = 0;
|
2021-12-01 18:44:29 +00:00
|
|
|
this->issuerURL[0] = 0;
|
2023-01-19 23:39:15 +00:00
|
|
|
this->ssoProvider[0] = 0;
|
2021-06-05 20:44:45 +00:00
|
|
|
}
|
2021-05-25 02:58:17 +00:00
|
|
|
}
|
2015-04-21 00:14:16 +00:00
|
|
|
}
|
2016-06-16 22:48:58 +00:00
|
|
|
|
2016-06-17 00:21:58 +00:00
|
|
|
//printf("~~~\n%s\n~~~\n",d.data());
|
|
|
|
//dump();
|
|
|
|
//printf("~~~\n");
|
2016-06-16 22:48:58 +00:00
|
|
|
|
2016-08-09 15:32:42 +00:00
|
|
|
delete tmp;
|
2016-06-16 19:28:43 +00:00
|
|
|
return true;
|
|
|
|
} catch ( ... ) {
|
2016-08-09 15:32:42 +00:00
|
|
|
delete tmp;
|
2016-06-16 19:28:43 +00:00
|
|
|
return false;
|
2015-04-21 00:14:16 +00:00
|
|
|
}
|
2013-10-18 15:01:41 +00:00
|
|
|
}
|
|
|
|
|
2013-10-18 13:48:02 +00:00
|
|
|
} // namespace ZeroTier
|