NetworkConfig.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2023-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdint.h>
  14. #include <algorithm>
  15. #include "NetworkConfig.hpp"
  16. #include "ScopedPtr.hpp"
  17. namespace ZeroTier {
  18. bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const
  19. {
  20. ScopedPtr< Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> > tmp(new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>());
  21. char tmp2[128];
  22. d.clear();
  23. // Try to put the more human-readable fields first
  24. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_VERSION,(uint64_t)ZT_NETWORKCONFIG_VERSION)) return false;
  25. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId)) return false;
  26. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp)) return false;
  27. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,this->credentialTimeMaxDelta)) return false;
  28. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision)) return false;
  29. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo.toString(tmp2))) return false;
  30. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET,this->remoteTraceTarget.toString(tmp2))) return false;
  31. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL,(uint64_t)this->remoteTraceLevel)) return false;
  32. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags)) return false;
  33. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit)) return false;
  34. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)this->type)) return false;
  35. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name)) return false;
  36. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_MTU,(uint64_t)this->mtu)) return false;
  37. // Then add binary blobs
  38. if (this->com) {
  39. tmp->clear();
  40. this->com.serialize(*tmp);
  41. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp)) return false;
  42. }
  43. tmp->clear();
  44. for(unsigned int i=0;i<this->capabilityCount;++i)
  45. this->capabilities[i].serialize(*tmp);
  46. if (tmp->size()) {
  47. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) return false;
  48. }
  49. tmp->clear();
  50. for(unsigned int i=0;i<this->tagCount;++i)
  51. this->tags[i].serialize(*tmp);
  52. if (tmp->size()) {
  53. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) return false;
  54. }
  55. tmp->clear();
  56. for(unsigned int i=0;i<this->certificateOfOwnershipCount;++i)
  57. this->certificatesOfOwnership[i].serialize(*tmp);
  58. if (tmp->size()) {
  59. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) return false;
  60. }
  61. tmp->clear();
  62. for(unsigned int i=0;i<this->specialistCount;++i)
  63. tmp->append((uint64_t)this->specialists[i]);
  64. if (tmp->size()) {
  65. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) return false;
  66. }
  67. tmp->clear();
  68. for(unsigned int i=0;i<this->routeCount;++i) {
  69. reinterpret_cast<const InetAddress *>(&(this->routes[i].target))->serialize(*tmp);
  70. reinterpret_cast<const InetAddress *>(&(this->routes[i].via))->serialize(*tmp);
  71. tmp->append((uint16_t)this->routes[i].flags);
  72. tmp->append((uint16_t)this->routes[i].metric);
  73. }
  74. if (tmp->size()) {
  75. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) return false;
  76. }
  77. tmp->clear();
  78. for(unsigned int i=0;i<this->staticIpCount;++i)
  79. this->staticIps[i].serialize(*tmp);
  80. if (tmp->size()) {
  81. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) return false;
  82. }
  83. if (this->ruleCount) {
  84. tmp->clear();
  85. Capability::serializeRules(*tmp,rules,ruleCount);
  86. if (tmp->size()) {
  87. if (!d.add(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) return false;
  88. }
  89. }
  90. return true;
  91. }
  92. bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d)
  93. {
  94. static const NetworkConfig NIL_NC;
  95. ScopedPtr< Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> > tmp(new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>());
  96. try {
  97. *this = NIL_NC;
  98. // Fields that are always present, new or old
  99. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  100. if (!this->networkId)
  101. return false;
  102. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  103. this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,0);
  104. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  105. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  106. if (!this->issuedTo)
  107. return false;
  108. this->remoteTraceTarget = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET);
  109. this->remoteTraceLevel = (Trace::Level)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL);
  110. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  111. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  112. this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU,ZT_DEFAULT_MTU);
  113. if (this->mtu < 1280)
  114. this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
  115. else if (this->mtu > ZT_MAX_MTU)
  116. this->mtu = ZT_MAX_MTU;
  117. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  118. return false;
  119. } else {
  120. // Otherwise we can use the new fields
  121. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  122. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  123. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp))
  124. this->com.deserialize(*tmp,0);
  125. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) {
  126. try {
  127. unsigned int p = 0;
  128. while (p < tmp->size()) {
  129. Capability cap;
  130. p += cap.deserialize(*tmp,p);
  131. this->capabilities[this->capabilityCount++] = cap;
  132. }
  133. } catch ( ... ) {}
  134. std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
  135. }
  136. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) {
  137. try {
  138. unsigned int p = 0;
  139. while (p < tmp->size()) {
  140. Tag tag;
  141. p += tag.deserialize(*tmp,p);
  142. this->tags[this->tagCount++] = tag;
  143. }
  144. } catch ( ... ) {}
  145. std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
  146. }
  147. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) {
  148. unsigned int p = 0;
  149. while (p < tmp->size()) {
  150. if (certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP)
  151. p += certificatesOfOwnership[certificateOfOwnershipCount++].deserialize(*tmp,p);
  152. else {
  153. CertificateOfOwnership foo;
  154. p += foo.deserialize(*tmp,p);
  155. }
  156. }
  157. }
  158. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) {
  159. unsigned int p = 0;
  160. while ((p + 8) <= tmp->size()) {
  161. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS)
  162. this->specialists[this->specialistCount++] = tmp->at<uint64_t>(p);
  163. p += 8;
  164. }
  165. }
  166. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) {
  167. unsigned int p = 0;
  168. while ((p < tmp->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  169. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(*tmp,p);
  170. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(*tmp,p);
  171. this->routes[this->routeCount].flags = tmp->at<uint16_t>(p); p += 2;
  172. this->routes[this->routeCount].metric = tmp->at<uint16_t>(p); p += 2;
  173. ++this->routeCount;
  174. }
  175. }
  176. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) {
  177. unsigned int p = 0;
  178. while ((p < tmp->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  179. p += this->staticIps[this->staticIpCount++].deserialize(*tmp,p);
  180. }
  181. }
  182. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) {
  183. this->ruleCount = 0;
  184. unsigned int p = 0;
  185. Capability::deserializeRules(*tmp,p,this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES);
  186. }
  187. }
  188. return true;
  189. } catch ( ... ) {
  190. return false;
  191. }
  192. }
  193. } // namespace ZeroTier