Locator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #ifndef ZT_LOCATOR_HPP
  14. #define ZT_LOCATOR_HPP
  15. #include "Constants.hpp"
  16. #include "Endpoint.hpp"
  17. #include "Identity.hpp"
  18. #include "TriviallyCopyable.hpp"
  19. #include "SharedPtr.hpp"
  20. #include "FCV.hpp"
  21. #include "Containers.hpp"
  22. #define ZT_LOCATOR_MAX_ENDPOINTS 8
  23. #define ZT_LOCATOR_MARSHAL_SIZE_MAX (8 + ZT_FINGERPRINT_MARSHAL_SIZE + 2 + (ZT_LOCATOR_MAX_ENDPOINTS * ZT_ENDPOINT_MARSHAL_SIZE_MAX) + 2 + 2 + ZT_SIGNATURE_BUFFER_SIZE)
  24. #define ZT_LOCATOR_STRING_SIZE_MAX 4096
  25. namespace ZeroTier {
  26. /**
  27. * Signed information about a node's location on the network
  28. *
  29. * A locator contains long-lived endpoints for a node such as IP/port pairs,
  30. * URLs, or other nodes, and is signed by the node it describes.
  31. */
  32. class Locator
  33. {
  34. friend class SharedPtr<Locator>;
  35. friend class SharedPtr<const Locator>;
  36. public:
  37. ZT_INLINE Locator() noexcept :
  38. m_ts(0)
  39. {}
  40. ZT_INLINE Locator(const Locator &loc) noexcept :
  41. m_ts(loc.m_ts),
  42. m_signer(loc.m_signer),
  43. m_endpoints(loc.m_endpoints),
  44. m_signature(loc.m_signature),
  45. __refCount(0)
  46. {}
  47. /**
  48. * @return Timestamp (a.k.a. revision number) set by Location signer
  49. */
  50. ZT_INLINE int64_t timestamp() const noexcept
  51. { return m_ts; }
  52. /**
  53. * @return Fingerprint of identity that signed this locator
  54. */
  55. ZT_INLINE const Fingerprint &signer() const noexcept
  56. { return m_signer; }
  57. /**
  58. * @return Endpoints specified in locator
  59. */
  60. ZT_INLINE const Vector<Endpoint> &endpoints() const noexcept
  61. { return m_endpoints; }
  62. /**
  63. * @return Signature data
  64. */
  65. ZT_INLINE const FCV<uint8_t, ZT_SIGNATURE_BUFFER_SIZE> &signature() const noexcept
  66. { return m_signature; }
  67. /**
  68. * Add an endpoint to this locator
  69. *
  70. * This doesn't check for the presence of the endpoint, so take
  71. * care not to add duplicates.
  72. *
  73. * @param ep Endpoint to add
  74. * @return True if endpoint was added (or already present), false if locator is full
  75. */
  76. bool add(const Endpoint &ep);
  77. /**
  78. * Sign this locator
  79. *
  80. * This sets timestamp, sorts endpoints so that the same set of endpoints
  81. * will always produce the same locator, and signs.
  82. *
  83. * @param id Identity that includes private key
  84. * @return True if signature successful
  85. */
  86. bool sign(int64_t ts, const Identity &id) noexcept;
  87. /**
  88. * Verify this Locator's validity and signature
  89. *
  90. * @param id Identity corresponding to hash
  91. * @return True if valid and signature checks out
  92. */
  93. bool verify(const Identity &id) const noexcept;
  94. /**
  95. * Convert this locator to a string
  96. *
  97. * @param s String buffer
  98. * @return Pointer to buffer
  99. */
  100. char *toString(char s[ZT_LOCATOR_STRING_SIZE_MAX]) const noexcept;
  101. ZT_INLINE String toString() const { char tmp[ZT_LOCATOR_STRING_SIZE_MAX]; return String(toString(tmp)); }
  102. /**
  103. * Decode a string format locator
  104. *
  105. * @param s Locator from toString()
  106. * @return True if format was valid
  107. */
  108. bool fromString(const char *s) noexcept;
  109. explicit ZT_INLINE operator bool() const noexcept
  110. { return m_ts > 0; }
  111. static constexpr int marshalSizeMax() noexcept { return ZT_LOCATOR_MARSHAL_SIZE_MAX; }
  112. int marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX], bool excludeSignature = false) const noexcept;
  113. int unmarshal(const uint8_t *data, int len) noexcept;
  114. private:
  115. int64_t m_ts;
  116. Fingerprint m_signer;
  117. Vector<Endpoint> m_endpoints;
  118. FCV<uint8_t, ZT_SIGNATURE_BUFFER_SIZE> m_signature;
  119. std::atomic<int> __refCount;
  120. };
  121. } // namespace ZeroTier
  122. #endif