NetworkConfig.cpp 7.6 KB

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