ClusterDefinition.hpp 5.1 KB

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