Locator.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. explicit Locator(const char *const str) noexcept;
  41. ZT_INLINE Locator(const Locator &loc) noexcept :
  42. m_ts(loc.m_ts),
  43. m_signer(loc.m_signer),
  44. m_endpoints(loc.m_endpoints),
  45. m_signature(loc.m_signature),
  46. __refCount(0)
  47. {}
  48. /**
  49. * @return Timestamp (a.k.a. revision number) set by Location signer
  50. */
  51. ZT_INLINE int64_t timestamp() const noexcept
  52. { return m_ts; }
  53. /**
  54. * @return Fingerprint of identity that signed this locator
  55. */
  56. ZT_INLINE const Fingerprint &signer() const noexcept
  57. { return m_signer; }
  58. /**
  59. * @return Endpoints specified in locator
  60. */
  61. ZT_INLINE const Vector<Endpoint> &endpoints() const noexcept
  62. { return m_endpoints; }
  63. /**
  64. * @return Signature data
  65. */
  66. ZT_INLINE const FCV<uint8_t, ZT_SIGNATURE_BUFFER_SIZE> &signature() const noexcept
  67. { return m_signature; }
  68. /**
  69. * Add an endpoint to this locator
  70. *
  71. * This doesn't check for the presence of the endpoint, so take
  72. * care not to add duplicates.
  73. *
  74. * @param ep Endpoint to add
  75. * @return True if endpoint was added (or already present), false if locator is full
  76. */
  77. bool add(const Endpoint &ep);
  78. /**
  79. * Sign this locator
  80. *
  81. * This sets timestamp, sorts endpoints so that the same set of endpoints
  82. * will always produce the same locator, and signs.
  83. *
  84. * @param id Identity that includes private key
  85. * @return True if signature successful
  86. */
  87. bool sign(int64_t ts, const Identity &id) noexcept;
  88. /**
  89. * Verify this Locator's validity and signature
  90. *
  91. * @param id Identity corresponding to hash
  92. * @return True if valid and signature checks out
  93. */
  94. bool verify(const Identity &id) const noexcept;
  95. /**
  96. * Convert this locator to a string
  97. *
  98. * @param s String buffer
  99. * @return Pointer to buffer
  100. */
  101. char *toString(char s[ZT_LOCATOR_STRING_SIZE_MAX]) const noexcept;
  102. ZT_INLINE String toString() const
  103. { char tmp[ZT_LOCATOR_STRING_SIZE_MAX]; return String(toString(tmp)); }
  104. /**
  105. * Decode a string format locator
  106. *
  107. * @param s Locator from toString()
  108. * @return True if format was valid
  109. */
  110. bool fromString(const char *s) noexcept;
  111. explicit ZT_INLINE operator bool() const noexcept
  112. { return m_ts > 0; }
  113. static constexpr int marshalSizeMax() noexcept { return ZT_LOCATOR_MARSHAL_SIZE_MAX; }
  114. int marshal(uint8_t data[ZT_LOCATOR_MARSHAL_SIZE_MAX], bool excludeSignature = false) const noexcept;
  115. int unmarshal(const uint8_t *data, int len) noexcept;
  116. private:
  117. int64_t m_ts;
  118. Fingerprint m_signer;
  119. Vector<Endpoint> m_endpoints;
  120. FCV<uint8_t, ZT_SIGNATURE_BUFFER_SIZE> m_signature;
  121. std::atomic<int> __refCount;
  122. };
  123. } // namespace ZeroTier
  124. #endif