ClusterDefinition.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "../osdep/OSUtils.hpp"
  26. namespace ZeroTier {
  27. /**
  28. * Parser for cluster definition file
  29. */
  30. class ClusterDefinition
  31. {
  32. public:
  33. struct MemberDefinition
  34. {
  35. MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; }
  36. unsigned int id;
  37. int x,y,z;
  38. char name[256];
  39. InetAddress clusterEndpoint;
  40. std::vector<InetAddress> zeroTierEndpoints;
  41. };
  42. ClusterDefinition(uint64_t myAddress,const char *pathToClusterFile)
  43. {
  44. std::string cf;
  45. if (!OSUtils::readFile(pathToClusterFile,cf))
  46. return;
  47. char myAddressStr[64];
  48. Utils::snprintf(myAddressStr,sizeof(myAddressStr),"%.10llx",myAddress);
  49. std::vector<std::string> lines(Utils::split(cf.c_str(),"\r\n","",""));
  50. for(std::vector<std::string>::iterator l(lines.begin());l!=lines.end();++l) {
  51. std::vector<std::string> fields(Utils::split(l->c_str()," \t","",""));
  52. if ((fields.size() < 5)||(fields[0][0] == '#')||(fields[0] != myAddressStr))
  53. continue;
  54. int id = Utils::strToUInt(fields[1].c_str());
  55. if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS))
  56. continue;
  57. MemberDefinition &md = _md[id];
  58. md.id = (unsigned int)id;
  59. if (fields.size() >= 6) {
  60. std::vector<std::string> xyz(Utils::split(fields[5].c_str(),",","",""));
  61. md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0;
  62. md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0;
  63. md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0;
  64. }
  65. Utils::scopy(md.name,sizeof(md.name),fields[2].c_str());
  66. md.clusterEndpoint.fromString(fields[3]);
  67. if (!md.clusterEndpoint)
  68. continue;
  69. std::vector<std::string> zips(Utils::split(fields[4].c_str(),",","",""));
  70. for(std::vector<std::string>::iterator zip(zips.begin());zip!=zips.end();++zip) {
  71. InetAddress i;
  72. i.fromString(*zip);
  73. if (i)
  74. md.zeroTierEndpoints.push_back(i);
  75. }
  76. _ids.push_back((unsigned int)id);
  77. }
  78. std::sort(_ids.begin(),_ids.end());
  79. }
  80. inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; }
  81. inline unsigned int size() const throw() { return (unsigned int)_ids.size(); }
  82. inline const std::vector<unsigned int> &ids() const throw() { return _ids; }
  83. inline std::vector<MemberDefinition> members() const
  84. {
  85. std::vector<MemberDefinition> m;
  86. for(std::vector<unsigned int>::const_iterator i(_ids.begin());i!=_ids.end();++i)
  87. m.push_back(_md[*i]);
  88. return m;
  89. }
  90. private:
  91. MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS];
  92. std::vector<unsigned int> _ids;
  93. };
  94. } // namespace ZeroTier
  95. #endif // ZT_ENABLE_CLUSTER
  96. #endif