NetworkConfig.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #include "Buf.hpp"
  18. namespace ZeroTier {
  19. bool NetworkConfig::toDictionary(Dictionary &d,bool includeLegacy) const
  20. {
  21. uint8_t tmp[ZT_BUF_MEM_SIZE];
  22. try {
  23. d.clear();
  24. d.add(ZT_NETWORKCONFIG_DICT_KEY_NETWORK_ID,this->networkId);
  25. d.add(ZT_NETWORKCONFIG_DICT_KEY_TIMESTAMP,this->timestamp);
  26. d.add(ZT_NETWORKCONFIG_DICT_KEY_CREDENTIAL_TIME_MAX_DELTA,this->credentialTimeMaxDelta);
  27. d.add(ZT_NETWORKCONFIG_DICT_KEY_REVISION,this->revision);
  28. d.add(ZT_NETWORKCONFIG_DICT_KEY_ISSUED_TO,this->issuedTo.toString((char *)tmp));
  29. d.add(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,this->flags);
  30. d.add(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,(uint64_t)this->multicastLimit);
  31. d.add(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint16_t)this->type);
  32. d.add(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name);
  33. d.add(ZT_NETWORKCONFIG_DICT_KEY_MTU,this->mtu);
  34. if (this->com)
  35. d.add(ZT_NETWORKCONFIG_DICT_KEY_COM,tmp,this->com.marshal(tmp));
  36. std::vector<uint8_t> *blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES]);
  37. for (unsigned int i = 0; i < this->capabilityCount; ++i) {
  38. int l = this->capabilities[i].marshal(tmp);
  39. if (l < 0)
  40. return false;
  41. blob->insert(blob->end(),tmp,tmp + l);
  42. }
  43. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_TAGS]);
  44. for (unsigned int i = 0; i < this->tagCount; ++i) {
  45. int l = this->tags[i].marshal(tmp);
  46. if (l < 0)
  47. return false;
  48. blob->insert(blob->end(),tmp,tmp + l);
  49. }
  50. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP]);
  51. for (unsigned int i = 0; i < this->certificateOfOwnershipCount; ++i) {
  52. int l = this->certificatesOfOwnership[i].marshal(tmp);
  53. if (l < 0)
  54. return false;
  55. blob->insert(blob->end(),tmp,tmp + l);
  56. }
  57. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS]);
  58. for (unsigned int i = 0; i < this->specialistCount; ++i) {
  59. Utils::storeBigEndian<uint64_t>(tmp,this->specialists[i]);
  60. blob->insert(blob->end(),tmp,tmp + 8);
  61. }
  62. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_ROUTES]);
  63. for (unsigned int i = 0; i < this->routeCount; ++i) {
  64. int l = asInetAddress(this->routes[i].target).marshal(tmp);
  65. if (l < 0)
  66. return false;
  67. blob->insert(blob->end(),tmp,tmp + l);
  68. l = asInetAddress(this->routes[i].via).marshal(tmp);
  69. if (l < 0)
  70. return false;
  71. blob->insert(blob->end(),tmp,tmp + l);
  72. }
  73. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS]);
  74. for (unsigned int i = 0; i < this->staticIpCount; ++i) {
  75. int l = this->staticIps[i].marshal(tmp);
  76. if (l < 0)
  77. return false;
  78. blob->insert(blob->end(),tmp,tmp + l);
  79. }
  80. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_RULES]);
  81. if (this->ruleCount) {
  82. blob->resize(ruleCount * ZT_VIRTUALNETWORKRULE_MARSHAL_SIZE_MAX);
  83. int l = Capability::marshalVirtualNetworkRules(blob->data(),rules,ruleCount);
  84. if (l > 0)
  85. blob->resize(l);
  86. }
  87. return true;
  88. } catch ( ... ) {}
  89. return false;
  90. }
  91. bool NetworkConfig::fromDictionary(const Dictionary &d)
  92. {
  93. static const NetworkConfig NIL_NC;
  94. try {
  95. *this = NIL_NC;
  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. if (this->timestamp <= 0)
  101. return false;
  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->multicastLimit = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MULTICAST_LIMIT,0);
  108. d.getS(ZT_NETWORKCONFIG_DICT_KEY_NAME,this->name,sizeof(this->name));
  109. this->mtu = (unsigned int)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_MTU,ZT_DEFAULT_MTU);
  110. if (this->mtu < 1280)
  111. this->mtu = 1280; // minimum MTU allowed by IPv6 standard and others
  112. else if (this->mtu > ZT_MAX_MTU)
  113. this->mtu = ZT_MAX_MTU;
  114. if (d.getUI(ZT_NETWORKCONFIG_DICT_KEY_VERSION,0) < 6) {
  115. return false;
  116. } else {
  117. this->flags = d.getUI(ZT_NETWORKCONFIG_DICT_KEY_FLAGS,0);
  118. this->type = (ZT_VirtualNetworkType)d.getUI(ZT_NETWORKCONFIG_DICT_KEY_TYPE,(uint64_t)ZT_NETWORK_TYPE_PRIVATE);
  119. const std::vector<uint8_t> *blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_COM]);
  120. if (!blob->empty()) {
  121. if (this->com.unmarshal(blob->data(),(int)(blob->size()) < 0))
  122. return false;
  123. }
  124. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CAPABILITIES]);
  125. if (!blob->empty()) {
  126. try {
  127. unsigned int p = 0;
  128. while (p < blob->size()) {
  129. Capability cap;
  130. int l = cap.unmarshal(blob->data() + p,(int)(blob->size() - p));
  131. if (l < 0)
  132. return false;
  133. p += l;
  134. if (this->capabilityCount < ZT_MAX_NETWORK_CAPABILITIES)
  135. this->capabilities[this->capabilityCount++] = cap;
  136. }
  137. } catch ( ... ) {}
  138. std::sort(&(this->capabilities[0]),&(this->capabilities[this->capabilityCount]));
  139. }
  140. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_TAGS]);
  141. if (!blob->empty()) {
  142. try {
  143. unsigned int p = 0;
  144. while (p < blob->size()) {
  145. Tag tag;
  146. int l = tag.unmarshal(blob->data() + p,(int)(blob->size() - p));
  147. if (l < 0)
  148. return false;
  149. p += l;
  150. if (this->tagCount < ZT_MAX_NETWORK_TAGS)
  151. this->tags[this->tagCount++] = tag;
  152. }
  153. } catch ( ... ) {}
  154. std::sort(&(this->tags[0]),&(this->tags[this->tagCount]));
  155. }
  156. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_CERTIFICATES_OF_OWNERSHIP]);
  157. if (!blob->empty()) {
  158. try {
  159. unsigned int p = 0;
  160. while (p < blob->size()) {
  161. CertificateOfOwnership coo;
  162. int l = coo.unmarshal(blob->data() + p,(int)(blob->size() - p));
  163. if (l < 0)
  164. return false;
  165. p += l;
  166. if (this->certificateOfOwnershipCount < ZT_MAX_CERTIFICATES_OF_OWNERSHIP)
  167. this->certificatesOfOwnership[certificateOfOwnershipCount++] = coo;
  168. }
  169. } catch ( ... ) {}
  170. std::sort(&(this->certificatesOfOwnership[0]),&(this->certificatesOfOwnership[this->certificateOfOwnershipCount]));
  171. }
  172. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_SPECIALISTS]);
  173. if (!blob->empty()) {
  174. unsigned int p = 0;
  175. while (((p + 8) <= blob->size())&&(specialistCount < ZT_MAX_NETWORK_SPECIALISTS)) {
  176. this->specialists[this->specialistCount++] = Utils::loadBigEndian<uint64_t>(blob->data() + p);
  177. p += 8;
  178. }
  179. }
  180. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_ROUTES]);
  181. if (!blob->empty()) {
  182. unsigned int p = 0;
  183. while ((p < blob->size())&&(routeCount < ZT_MAX_NETWORK_ROUTES)) {
  184. int l = asInetAddress(this->routes[this->routeCount].target).unmarshal(blob->data(),(int)(blob->size() - p));
  185. if (l < 0)
  186. return false;
  187. p += l;
  188. if (p >= blob->size())
  189. return false;
  190. l = asInetAddress(this->routes[this->routeCount].via).unmarshal(blob->data(),(int)(blob->size() - p));
  191. if (l < 0)
  192. return false;
  193. p += l;
  194. if ((p + 4) > blob->size())
  195. return false;
  196. this->routes[this->routeCount].flags = Utils::loadBigEndian<uint16_t>(blob->data() + p); p += 2;
  197. this->routes[this->routeCount].metric = Utils::loadBigEndian<uint16_t>(blob->data() + p); p += 2;
  198. ++this->routeCount;
  199. }
  200. }
  201. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_STATIC_IPS]);
  202. if (!blob->empty()) {
  203. unsigned int p = 0;
  204. while ((p < blob->size())&&(staticIpCount < ZT_MAX_ZT_ASSIGNED_ADDRESSES)) {
  205. int l = this->staticIps[this->staticIpCount].unmarshal(blob->data() + p,(int)(blob->size() - p));
  206. if (l < 0)
  207. return false;
  208. p += l;
  209. ++this->staticIpCount;
  210. }
  211. }
  212. blob = &(d[ZT_NETWORKCONFIG_DICT_KEY_RULES]);
  213. if (!blob->empty()) {
  214. this->ruleCount = 0;
  215. if (Capability::unmarshalVirtualNetworkRules(blob->data(),(int)blob->size(),this->rules,this->ruleCount,ZT_MAX_NETWORK_RULES) < 0)
  216. return false;
  217. }
  218. }
  219. return true;
  220. } catch ( ... ) {}
  221. return false;
  222. }
  223. bool NetworkConfig::addSpecialist(const Address &a,const uint64_t f)
  224. {
  225. const uint64_t aint = a.toInt();
  226. for(unsigned int i=0;i<specialistCount;++i) {
  227. if ((specialists[i] & 0xffffffffffULL) == aint) {
  228. specialists[i] |= f;
  229. return true;
  230. }
  231. }
  232. if (specialistCount < ZT_MAX_NETWORK_SPECIALISTS) {
  233. specialists[specialistCount++] = f | aint;
  234. return true;
  235. }
  236. return false;
  237. }
  238. } // namespace ZeroTier