Locator.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2019 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_LOCATOR_HPP
  27. #define ZT_LOCATOR_HPP
  28. #include "Constants.hpp"
  29. #include "Identity.hpp"
  30. #include "InetAddress.hpp"
  31. #include <vector>
  32. #define ZT_LOCATOR_MAX_PHYSICAL_ENDPOINTS 32
  33. #define ZT_LOCATOR_MAX_VIRTUAL_ENDPOINTS 32
  34. namespace ZeroTier {
  35. /**
  36. * Signed information about a node's location on the network
  37. */
  38. class Locator
  39. {
  40. public:
  41. Locator() :
  42. _signatureLength(0),
  43. _orgSignatureLength(0) {}
  44. inline void addLocation(const InetAddress &phy) { if (_physical.size() < ZT_LOCATOR_MAX_PHYSICAL_ENDPOINTS) _physical.push_back(phy); }
  45. inline void addLocation(const Identity &v) { if (_virtual.size() < ZT_LOCATOR_MAX_VIRTUAL_ENDPOINTS) _virtual.push_back(v); }
  46. inline const std::vector<InetAddress> &physical() const { return _physical; }
  47. inline const std::vector<Identity> &virt() const { return _virtual; }
  48. void sign(const Identity &id,const Identity &organization,const int64_t timestamp);
  49. bool verify() const;
  50. void generateDNSRecords(char *buf,unsigned int buflen);
  51. template<unsigned int C>
  52. inline void serialize(Buffer<C> &b,const bool forSign = false) const
  53. {
  54. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  55. b.append((uint64_t)_ts);
  56. _id.serialize(b,false);
  57. _organization.serialize(b,false);
  58. b.append((uint16_t)_physical.size());
  59. for(std::vector<InetAddress>::const_iterator i(_physical.begin());i!=_physical.end();++i)
  60. i->serialize(b);
  61. b.append((uint16_t)_virtual.size());
  62. for(std::vector<InetAddress>::const_iterator i(_virtual.begin());i!=_virtual.end();++i)
  63. i->serialize(b,false);
  64. if (!forSign) {
  65. b.append((uint16_t)_signatureLength);
  66. b.append(_signature,_signatureLength);
  67. b.append((uint16_t)_orgSignatureLength);
  68. b.append(_orgSignature,_orgSignatureLength);
  69. }
  70. b.append((uint16_t)0); // length of additional fields, currently 0
  71. if (forSign) b.append((uint64_t)0x7f7f7f7f7f7f7f7fULL);
  72. }
  73. template<unsigned int C>
  74. inline unsigned int deserialize(const Buffer<C> &b,unsigned int startAt = 0)
  75. {
  76. unsigned int p = startAt;
  77. _ts = (uint64_t)b.template at<uint64_t>(p); p += 8;
  78. p += _id.deserialize(b,p);
  79. p += _organization.deserialize(b,p);
  80. unsigned int cnt = b.template at<uint16_t>(p); p += 2;
  81. if (cnt > ZT_LOCATOR_MAX_PHYSICAL_ENDPOINTS)
  82. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  83. _physical.resize(cnt);
  84. for(std::vector<InetAddress>::iterator i(_physical.begin());i!=_physical.end();++i)
  85. p += i->deserialize(b,p);
  86. cnt = b.template at<uint16_t>(p); p += 2;
  87. if (cnt > ZT_LOCATOR_MAX_VIRTUAL_ENDPOINTS)
  88. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  89. _virtual.resize(cnt);
  90. for(std::vector<Identity>::iterator i(_virtual.begin());i!=_virtual.end();++i)
  91. p += i->deserialize(b,p);
  92. p += 2 + b.template at<uint16_t>(p);
  93. if (p > b.size())
  94. throw ZT_EXCEPTION_INVALID_SERIALIZED_DATA_OVERFLOW;
  95. return (p - startAt);
  96. }
  97. private:
  98. int64_t _ts;
  99. Identity _id;
  100. Identity _organization;
  101. std::vector<InetAddress> _physical;
  102. std::vector<Identity> _virtual;
  103. unsigned int _signatureLength;
  104. unsigned int _orgSignatureLength;
  105. uint8_t _signature[ZT_SIGNATURE_BUFFER_SIZE];
  106. uint8_t _orgSignature[ZT_SIGNATURE_BUFFER_SIZE];
  107. };
  108. } // namespace ZeroTier
  109. #endif