ClusterDefinition.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_CLUSTERDEFINITION_HPP
  28. #define ZT_CLUSTERDEFINITION_HPP
  29. #ifdef ZT_ENABLE_CLUSTER
  30. #include <vector>
  31. #include <algorithm>
  32. #include "../node/Constants.hpp"
  33. #include "../node/Utils.hpp"
  34. #include "../osdep/OSUtils.hpp"
  35. namespace ZeroTier {
  36. /**
  37. * Parser for cluster definition file
  38. */
  39. class ClusterDefinition
  40. {
  41. public:
  42. struct MemberDefinition
  43. {
  44. MemberDefinition() : id(0),x(0),y(0),z(0) { name[0] = (char)0; }
  45. unsigned int id;
  46. int x,y,z;
  47. char name[256];
  48. InetAddress clusterEndpoint;
  49. std::vector<InetAddress> zeroTierEndpoints;
  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. int id = Utils::strToUInt(fields[1].c_str());
  64. if ((id < 0)||(id > ZT_CLUSTER_MAX_MEMBERS))
  65. continue;
  66. MemberDefinition &md = _md[id];
  67. md.id = (unsigned int)id;
  68. if (fields.size() >= 6) {
  69. std::vector<std::string> xyz(Utils::split(fields[5].c_str(),",","",""));
  70. md.x = (xyz.size() > 0) ? Utils::strToInt(xyz[0].c_str()) : 0;
  71. md.y = (xyz.size() > 1) ? Utils::strToInt(xyz[1].c_str()) : 0;
  72. md.z = (xyz.size() > 2) ? Utils::strToInt(xyz[2].c_str()) : 0;
  73. }
  74. Utils::scopy(md.name,sizeof(md.name),fields[2].c_str());
  75. md.clusterEndpoint.fromString(fields[3]);
  76. if (!md.clusterEndpoint)
  77. continue;
  78. std::vector<std::string> zips(Utils::split(fields[4].c_str(),",","",""));
  79. for(std::vector<std::string>::iterator zip(zips.begin());zip!=zips.end();++zip) {
  80. InetAddress i;
  81. i.fromString(*zip);
  82. if (i)
  83. md.zeroTierEndpoints.push_back(i);
  84. }
  85. _ids.push_back((unsigned int)id);
  86. }
  87. std::sort(_ids.begin(),_ids.end());
  88. }
  89. inline const MemberDefinition &operator[](unsigned int id) const throw() { return _md[id]; }
  90. inline unsigned int size() const throw() { return (unsigned int)_ids.size(); }
  91. inline const std::vector<unsigned int> &ids() const throw() { return _ids; }
  92. inline std::vector<MemberDefinition> members() const
  93. {
  94. std::vector<MemberDefinition> m;
  95. for(std::vector<unsigned int>::const_iterator i(_ids.begin());i!=_ids.end();++i)
  96. m.push_back(_md[*i]);
  97. return m;
  98. }
  99. private:
  100. MemberDefinition _md[ZT_CLUSTER_MAX_MEMBERS];
  101. std::vector<unsigned int> _ids;
  102. };
  103. } // namespace ZeroTier
  104. #endif // ZT_ENABLE_CLUSTER
  105. #endif