ClusterDefinition.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * You can be released from the requirements of the license by purchasing
  21. * a commercial license. Buying such a license is mandatory as soon as you
  22. * develop commercial closed-source software that incorporates or links
  23. * directly against ZeroTier software without disclosing the source code
  24. * of your own application.
  25. */
  26. #ifndef ZT_CLUSTERDEFINITION_HPP
  27. #define ZT_CLUSTERDEFINITION_HPP
  28. #ifdef ZT_ENABLE_CLUSTER
  29. #include <vector>
  30. #include <algorithm>
  31. #include "../node/Constants.hpp"
  32. #include "../node/Utils.hpp"
  33. #include "../node/NonCopyable.hpp"
  34. #include "../osdep/OSUtils.hpp"
  35. #include "ClusterGeoIpService.hpp"
  36. namespace ZeroTier {
  37. /**
  38. * Parser for cluster definition file
  39. */
  40. class ClusterDefinition : NonCopyable
  41. {
  42. public:
  43. struct MemberDefinition
  44. {
  45. MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; }
  46. unsigned int id;
  47. int x,y,z;
  48. char name[256];
  49. InetAddress clusterEndpoint;
  50. std::vector<InetAddress> zeroTierEndpoints;
  51. };
  52. /**
  53. * Load and initialize cluster definition and GeoIP data if any
  54. *
  55. * @param myAddress My ZeroTier address
  56. * @param pathToClusterFile Path to cluster definition file
  57. * @throws std::runtime_error Invalid cluster definition or unable to load data
  58. */
  59. ClusterDefinition(uint64_t myAddress,const char *pathToClusterFile)
  60. {
  61. std::string cf;
  62. if (!OSUtils::readFile(pathToClusterFile,cf))
  63. return;
  64. char myAddressStr[64];
  65. Utils::snprintf(myAddressStr,sizeof(myAddressStr),"%.10llx",myAddress);
  66. std::vector<std::string> lines(OSUtils::split(cf.c_str(),"\r\n","",""));
  67. for(std::vector<std::string>::iterator l(lines.begin());l!=lines.end();++l) {
  68. std::vector<std::string> fields(OSUtils::split(l->c_str()," \t","",""));
  69. if ((fields.size() < 5)||(fields[0][0] == '#')||(fields[0] != myAddressStr))
  70. continue;
  71. // <address> geo <CSV path> <ip start column> <ip end column> <latitutde column> <longitude column>
  72. if (fields[1] == "geo") {
  73. if ((fields.size() >= 7)&&(OSUtils::fileExists(fields[2].c_str()))) {
  74. int ipStartColumn = Utils::strToInt(fields[3].c_str());
  75. int ipEndColumn = Utils::strToInt(fields[4].c_str());
  76. int latitudeColumn = Utils::strToInt(fields[5].c_str());
  77. int longitudeColumn = Utils::strToInt(fields[6].c_str());
  78. if (_geo.load(fields[2].c_str(),ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn) <= 0)
  79. throw std::runtime_error(std::string("failed to load geo-ip data from ")+fields[2]);
  80. }
  81. continue;
  82. }
  83. // <address> <ID> <name> <backplane IP/port(s)> <ZT frontplane IP/port(s)> <x,y,z>
  84. int id = Utils::strToUInt(fields[1].c_str());
  85. if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS))
  86. throw std::runtime_error(std::string("invalid cluster member ID: ")+fields[1]);
  87. MemberDefinition &md = _md[id];
  88. md.id = (unsigned int)id;
  89. if (fields.size() >= 6) {
  90. std::vector<std::string> xyz(OSUtils::split(fields[5].c_str(),",","",""));
  91. md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0;
  92. md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0;
  93. md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0;
  94. }
  95. Utils::scopy(md.name,sizeof(md.name),fields[2].c_str());
  96. md.clusterEndpoint.fromString(fields[3]);
  97. if (!md.clusterEndpoint)
  98. continue;
  99. std::vector<std::string> zips(OSUtils::split(fields[4].c_str(),",","",""));
  100. for(std::vector<std::string>::iterator zip(zips.begin());zip!=zips.end();++zip) {
  101. InetAddress i;
  102. i.fromString(*zip);
  103. if (i)
  104. md.zeroTierEndpoints.push_back(i);
  105. }
  106. _ids.push_back((unsigned int)id);
  107. }
  108. std::sort(_ids.begin(),_ids.end());
  109. }
  110. /**
  111. * @return All member definitions in this cluster by ID (ID is array index)
  112. */
  113. inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; }
  114. /**
  115. * @return Number of members in this cluster
  116. */
  117. inline unsigned int size() const throw() { return (unsigned int)_ids.size(); }
  118. /**
  119. * @return IDs of members in this cluster sorted by ID
  120. */
  121. inline const std::vector<unsigned int> &ids() const throw() { return _ids; }
  122. /**
  123. * @return GeoIP service for this cluster
  124. */
  125. inline ClusterGeoIpService &geo() throw() { return _geo; }
  126. /**
  127. * @return A vector (new copy) containing all cluster members
  128. */
  129. inline std::vector<MemberDefinition> members() const
  130. {
  131. std::vector<MemberDefinition> m;
  132. for(std::vector<unsigned int>::const_iterator i(_ids.begin());i!=_ids.end();++i)
  133. m.push_back(_md[*i]);
  134. return m;
  135. }
  136. private:
  137. MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS];
  138. std::vector<unsigned int> _ids;
  139. ClusterGeoIpService _geo;
  140. };
  141. } // namespace ZeroTier
  142. #endif // ZT_ENABLE_CLUSTER
  143. #endif