NetworkConfig.cpp 7.9 KB

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