NetworkConfig.cpp 7.4 KB

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