NetworkConfig.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. namespace ZeroTier {
  17. bool NetworkConfig::toDictionary(Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d,bool includeLegacy) const
  18. {
  19. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  20. char tmp2[128];
  21. try {
  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. delete tmp;
  91. } catch ( ... ) {
  92. delete tmp;
  93. throw;
  94. }
  95. return true;
  96. }
  97. bool NetworkConfig::fromDictionary(const Dictionary<ZT_NETWORKCONFIG_DICT_CAPACITY> &d)
  98. {
  99. static const NetworkConfig NIL_NC;
  100. Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY> *const tmp = new Buffer<ZT_NETWORKCONFIG_DICT_CAPACITY>();
  101. try {
  102. *this = NIL_NC;
  103. // Fields that are always present, new or old
  104. this->networkId = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,0);
  105. if (!this->networkId) {
  106. delete tmp;
  107. return false;
  108. }
  109. this->timestamp = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,0);
  110. this->credentialTimeMaxDelta = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,0);
  111. this->revision = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REVISION,0);
  112. this->issuedTo = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,0);
  113. if (!this->issuedTo) {
  114. delete tmp;
  115. return false;
  116. }
  117. this->remoteTraceTarget = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_TARGET);
  118. this->remoteTraceLevel = (Trace::Level)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_REMOTE_TRACE_LEVEL);
  119. this->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  120. d.get(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  121. this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU,ZT_DEFAULT_MTU);
  122. if (this->mtu < 1280)
  123. this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
  124. else if (this->mtu > ZT_MAX_MTU)
  125. this->mtu = ZT_MAX_MTU;
  126. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  127. delete tmp;
  128. return false;
  129. } else {
  130. // Otherwise we can use the new fields
  131. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  132. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  133. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_COM,*tmp))
  134. this->com.deserialize(*tmp,0);
  135. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES,*tmp)) {
  136. try {
  137. unsigned int p = 0;
  138. while (p < tmp->size()) {
  139. Capability cap;
  140. p += cap.deserialize(*tmp,p);
  141. this->capabilities[this->capabilityCount++] = cap;
  142. }
  143. } catch ( ... ) {}
  144. std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
  145. }
  146. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_TAGS,*tmp)) {
  147. try {
  148. unsigned int p = 0;
  149. while (p < tmp->size()) {
  150. Tag tag;
  151. p += tag.deserialize(*tmp,p);
  152. this->tags[this->tagCount++] = tag;
  153. }
  154. } catch ( ... ) {}
  155. std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
  156. }
  157. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP,*tmp)) {
  158. unsigned int p = 0;
  159. while (p < tmp->size()) {
  160. if (certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP)
  161. p += certificatesOfOwnership[certificateOfOwnershipCount++].deserialize(*tmp,p);
  162. else {
  163. CertificateOfOwnership foo;
  164. p += foo.deserialize(*tmp,p);
  165. }
  166. }
  167. }
  168. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS,*tmp)) {
  169. unsigned int p = 0;
  170. while ((p + 8) <= tmp->size()) {
  171. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS)
  172. this->specialists[this->specialistCount++] = tmp->at<uint64_t>(p);
  173. p += 8;
  174. }
  175. }
  176. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_ROUTES,*tmp)) {
  177. unsigned int p = 0;
  178. while ((p < tmp->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  179. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].target))->deserialize(*tmp,p);
  180. p += reinterpret_cast<InetAddress *>(&(this->routes[this->routeCount].via))->deserialize(*tmp,p);
  181. this->routes[this->routeCount].flags = tmp->at<uint16_t>(p); p += 2;
  182. this->routes[this->routeCount].metric = tmp->at<uint16_t>(p); p += 2;
  183. ++this->routeCount;
  184. }
  185. }
  186. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS,*tmp)) {
  187. unsigned int p = 0;
  188. while ((p < tmp->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  189. p += this->staticIps[this->staticIpCount++].deserialize(*tmp,p);
  190. }
  191. }
  192. if (d.get(ZT_NETWORKCONFIG_DICT_KEY_RULES,*tmp)) {
  193. this->ruleCount = 0;
  194. unsigned int p = 0;
  195. Capability::deserializeRules(*tmp,p,this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES);
  196. }
  197. }
  198. delete tmp;
  199. return true;
  200. } catch ( ... ) {
  201. delete tmp;
  202. return false;
  203. }
  204. }
  205. } // namespace ZeroTier