ClusterDefinition.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 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. #ifndef ZT_CLUSTERDEFINITION_HPP
  19. #define ZT_CLUSTERDEFINITION_HPP
  20. #ifdef ZT_ENABLE_CLUSTER
  21. #include <vector>
  22. #include <algorithm>
  23. #include "../node/Constants.hpp"
  24. #include "../node/Utils.hpp"
  25. #include "../node/NonCopyable.hpp"
  26. #include "../osdep/OSUtils.hpp"
  27. #include "ClusterGeoIpService.hpp"
  28. namespace ZeroTier {
  29. /**
  30. * Parser for cluster definition file
  31. */
  32. class ClusterDefinition : NonCopyable
  33. {
  34. public:
  35. struct MemberDefinition
  36. {
  37. MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; }
  38. unsigned int id;
  39. int x,y,z;
  40. char name[256];
  41. InetAddress clusterEndpoint;
  42. std::vector<InetAddress> zeroTierEndpoints;
  43. };
  44. /**
  45. * Load and initialize cluster definition and GeoIP data if any
  46. *
  47. * @param myAddress My ZeroTier address
  48. * @param pathToClusterFile Path to cluster definition file
  49. * @throws std::runtime_error Invalid cluster definition or unable to load data
  50. */
  51. ClusterDefinition(uint64_t myAddress,const char *pathToClusterFile)
  52. {
  53. std::string cf;
  54. if (!OSUtils::readFile(pathToClusterFile,cf))
  55. return;
  56. char myAddressStr[64];
  57. Utils::snprintf(myAddressStr,sizeof(myAddressStr),"%.10llx",myAddress);
  58. std::vector<std::string> lines(Utils::split(cf.c_str(),"\r\n","",""));
  59. for(std::vector<std::string>::iterator l(lines.begin());l!=lines.end();++l) {
  60. std::vector<std::string> fields(Utils::split(l->c_str()," \t","",""));
  61. if ((fields.size() < 5)||(fields[0][0] == '#')||(fields[0] != myAddressStr))
  62. continue;
  63. // <address> geo <CSV path> <ip start column> <ip end column> <latitutde column> <longitude column>
  64. if (fields[1] == "geo") {
  65. if ((fields.size() >= 7)&&(OSUtils::fileExists(fields[2].c_str()))) {
  66. int ipStartColumn = Utils::strToInt(fields[3].c_str());
  67. int ipEndColumn = Utils::strToInt(fields[4].c_str());
  68. int latitudeColumn = Utils::strToInt(fields[5].c_str());
  69. int longitudeColumn = Utils::strToInt(fields[6].c_str());
  70. if (_geo.load(fields[2].c_str(),ipStartColumn,ipEndColumn,latitudeColumn,longitudeColumn) <= 0)
  71. throw std::runtime_error(std::string("failed to load geo-ip data from ")+fields[2]);
  72. }
  73. continue;
  74. }
  75. // <address> <ID> <name> <backplane IP/port(s)> <ZT frontplane IP/port(s)> <x,y,z>
  76. int id = Utils::strToUInt(fields[1].c_str());
  77. if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS))
  78. throw std::runtime_error(std::string("invalid cluster member ID: ")+fields[1]);
  79. MemberDefinition &md = _md[id];
  80. md.id = (unsigned int)id;
  81. if (fields.size() >= 6) {
  82. std::vector<std::string> xyz(Utils::split(fields[5].c_str(),",","",""));
  83. md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0;
  84. md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0;
  85. md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0;
  86. }
  87. Utils::scopy(md.name,sizeof(md.name),fields[2].c_str());
  88. md.clusterEndpoint.fromString(fields[3]);
  89. if (!md.clusterEndpoint)
  90. continue;
  91. std::vector<std::string> zips(Utils::split(fields[4].c_str(),",","",""));
  92. for(std::vector<std::string>::iterator zip(zips.begin());zip!=zips.end();++zip) {
  93. InetAddress i;
  94. i.fromString(*zip);
  95. if (i)
  96. md.zeroTierEndpoints.push_back(i);
  97. }
  98. _ids.push_back((unsigned int)id);
  99. }
  100. std::sort(_ids.begin(),_ids.end());
  101. }
  102. /**
  103. * @return All member definitions in this cluster by ID (ID is array index)
  104. */
  105. inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; }
  106. /**
  107. * @return Number of members in this cluster
  108. */
  109. inline unsigned int size() const throw() { return (unsigned int)_ids.size(); }
  110. /**
  111. * @return IDs of members in this cluster sorted by ID
  112. */
  113. inline const std::vector<unsigned int> &ids() const throw() { return _ids; }
  114. /**
  115. * @return GeoIP service for this cluster
  116. */
  117. inline ClusterGeoIpService &geo() throw() { return _geo; }
  118. /**
  119. * @return A vector (new copy) containing all cluster members
  120. */
  121. inline std::vector<MemberDefinition> members() const
  122. {
  123. std::vector<MemberDefinition> m;
  124. for(std::vector<unsigned int>::const_iterator i(_ids.begin());i!=_ids.end();++i)
  125. m.push_back(_md[*i]);
  126. return m;
  127. }
  128. private:
  129. MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS];
  130. std::vector<unsigned int> _ids;
  131. ClusterGeoIpService _geo;
  132. };
  133. } // namespace ZeroTier
  134. #endif // ZT_ENABLE_CLUSTER
  135. #endif