NetworkConfig.cpp 7.2 KB

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